move to include_str!() macro for all data
Move away from I/O to just including the data in the program via macro.
This commit is contained in:
5 files changed
+20
-31
No files matched your search
+4
-4
@@ -1,4 +1,4 @@
|
|||||||
use crate::utils::read_data;
|
use crate::utils::parse_data;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
fn count_incrementing_windows(window: usize, v: &[usize]) -> usize {
|
fn count_incrementing_windows(window: usize, v: &[usize]) -> usize {
|
||||||
@@ -6,7 +6,7 @@ fn count_incrementing_windows(window: usize, v: &[usize]) -> usize {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn run() -> Result<(usize, usize)> {
|
pub(crate) fn run() -> Result<(usize, usize)> {
|
||||||
let data = read_data::<usize>("data/day01.txt")?;
|
let data = parse_data::<usize>(include_str!("../data/day01.txt"))?;
|
||||||
Ok((
|
Ok((
|
||||||
count_incrementing_windows(1, &data),
|
count_incrementing_windows(1, &data),
|
||||||
count_incrementing_windows(3, &data),
|
count_incrementing_windows(3, &data),
|
||||||
@@ -19,14 +19,14 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_example() {
|
fn test_example() {
|
||||||
let data = read_data::<usize>("data/day01-test.txt").unwrap();
|
let data = parse_data::<usize>(include_str!("../data/day01-test.txt")).unwrap();
|
||||||
assert_eq!(count_incrementing_windows(1, &data), 7);
|
assert_eq!(count_incrementing_windows(1, &data), 7);
|
||||||
assert_eq!(count_incrementing_windows(3, &data), 5);
|
assert_eq!(count_incrementing_windows(3, &data), 5);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_my_data() {
|
fn test_my_data() {
|
||||||
let data = read_data::<usize>("data/day01.txt").unwrap();
|
let data = parse_data::<usize>(include_str!("../data/day01.txt")).unwrap();
|
||||||
assert_eq!(count_incrementing_windows(1, &data), 1298);
|
assert_eq!(count_incrementing_windows(1, &data), 1298);
|
||||||
assert_eq!(count_incrementing_windows(3, &data), 1248);
|
assert_eq!(count_incrementing_windows(3, &data), 1248);
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-8
@@ -1,4 +1,3 @@
|
|||||||
use crate::utils::read_file;
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
fn parse_line(s: &str) -> (&str, i32) {
|
fn parse_line(s: &str) -> (&str, i32) {
|
||||||
@@ -34,7 +33,7 @@ fn follow_course_better(s: &str) -> i32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn run() -> Result<(usize, usize)> {
|
pub(crate) fn run() -> Result<(usize, usize)> {
|
||||||
let input = read_file("data/day02.txt")?;
|
let input = include_str!("../data/day02.txt");
|
||||||
Ok((
|
Ok((
|
||||||
follow_course(&input) as usize,
|
follow_course(&input) as usize,
|
||||||
follow_course_better(&input) as usize,
|
follow_course_better(&input) as usize,
|
||||||
@@ -47,15 +46,15 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_example() {
|
fn test_example() {
|
||||||
let input = read_file("data/day02-test.txt").unwrap();
|
let input = include_str!("../data/day02-test.txt");
|
||||||
assert_eq!(follow_course(&input), 150);
|
assert_eq!(follow_course(input), 150);
|
||||||
assert_eq!(follow_course_better(&input), 900);
|
assert_eq!(follow_course_better(input), 900);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_my_data() {
|
fn test_my_data() {
|
||||||
let input = read_file("data/day02.txt").unwrap();
|
let input = include_str!("../data/day02.txt");
|
||||||
assert_eq!(follow_course(&input), 2070300);
|
assert_eq!(follow_course(input), 2070300);
|
||||||
assert_eq!(follow_course_better(&input), 2078985210);
|
assert_eq!(follow_course_better(input), 2078985210);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+3
-4
@@ -1,4 +1,3 @@
|
|||||||
use crate::utils::read_file;
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
fn strings_to_ints(s: &str) -> Vec<usize> {
|
fn strings_to_ints(s: &str) -> Vec<usize> {
|
||||||
@@ -89,7 +88,7 @@ fn diagnose_lifesupport(s: &str) -> usize {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn run() -> Result<(usize, usize)> {
|
pub(crate) fn run() -> Result<(usize, usize)> {
|
||||||
let input = read_file("data/day03.txt")?;
|
let input = include_str!("../data/day03.txt");
|
||||||
Ok((diagnose_power(&input), diagnose_lifesupport(&input)))
|
Ok((diagnose_power(&input), diagnose_lifesupport(&input)))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,14 +98,14 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_example() {
|
fn test_example() {
|
||||||
let input = read_file("data/day03-test.txt").unwrap();
|
let input = include_str!("../data/day03-test.txt");
|
||||||
assert_eq!(diagnose_power(&input), 198);
|
assert_eq!(diagnose_power(&input), 198);
|
||||||
assert_eq!(diagnose_lifesupport(&input), 230);
|
assert_eq!(diagnose_lifesupport(&input), 230);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_my_data() {
|
fn test_my_data() {
|
||||||
let input = read_file("data/day03.txt").unwrap();
|
let input = include_str!("../data/day03.txt");
|
||||||
assert_eq!(diagnose_power(&input), 4191876);
|
assert_eq!(diagnose_power(&input), 4191876);
|
||||||
// assert_eq!(follow_course(&input), 2070300);
|
// assert_eq!(follow_course(&input), 2070300);
|
||||||
// assert_eq!(follow_course_better(&input), 2078985210);
|
// assert_eq!(follow_course_better(&input), 2078985210);
|
||||||
|
|||||||
+5
-6
@@ -1,4 +1,3 @@
|
|||||||
use crate::utils::read_file;
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -70,10 +69,10 @@ fn find_winning_score(s: &str, first: bool) -> usize {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn run() -> Result<(usize, usize)> {
|
pub(crate) fn run() -> Result<(usize, usize)> {
|
||||||
let input = read_file("data/day04.txt")?;
|
let input = include_str!("../data/day04.txt");
|
||||||
Ok((
|
Ok((
|
||||||
find_winning_score(&input, true),
|
find_winning_score(input, true),
|
||||||
find_winning_score(&input, false),
|
find_winning_score(input, false),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -82,14 +81,14 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_example() {
|
fn test_example() {
|
||||||
let input = read_file("data/day04-test.txt").unwrap();
|
let input = include_str!("../data/day04-test.txt");
|
||||||
assert_eq!(find_winning_score(&input, true), 4512);
|
assert_eq!(find_winning_score(&input, true), 4512);
|
||||||
assert_eq!(find_winning_score(&input, false), 1924);
|
assert_eq!(find_winning_score(&input, false), 1924);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_my_data() {
|
fn test_my_data() {
|
||||||
let input = read_file("data/day04.txt").unwrap();
|
let input = include_str!("../data/day04.txt");
|
||||||
assert_eq!(find_winning_score(&input, true), 71708);
|
assert_eq!(find_winning_score(&input, true), 71708);
|
||||||
assert_eq!(find_winning_score(&input, false), 34726);
|
assert_eq!(find_winning_score(&input, false), 34726);
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-9
@@ -1,9 +1,5 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use std::{fs, str::FromStr};
|
use std::str::FromStr;
|
||||||
|
|
||||||
pub fn read_data<T: FromStr>(filename: &str) -> Result<Vec<T>> {
|
|
||||||
parse_data(&read_file(filename)?)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn parse_data<T: FromStr>(s: &str) -> Result<Vec<T>> {
|
pub fn parse_data<T: FromStr>(s: &str) -> Result<Vec<T>> {
|
||||||
let mut ret = Vec::new();
|
let mut ret = Vec::new();
|
||||||
@@ -16,7 +12,3 @@ pub fn parse_data<T: FromStr>(s: &str) -> Result<Vec<T>> {
|
|||||||
}
|
}
|
||||||
Ok(ret)
|
Ok(ret)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_file(filename: &str) -> Result<String> {
|
|
||||||
Ok(fs::read_to_string(filename)?)
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user