1
0
Fork 0

day(01): factor out parsing

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2022-12-01 11:23:15 +01:00
parent 604bc5d6a9
commit 0f6c1af251
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -61,6 +61,13 @@ fn part_2(input: &[i32]) -> i32 {
calories.iter().rev().take(3).sum()
}
fn parse_input(pathname: &str) -> Vec<i32> {
file_to_lines(pathname)
.iter()
.map(|s| if s.is_empty() { -1 } else { s.parse().unwrap() })
.collect()
}
fn main() {
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
@ -71,14 +78,8 @@ fn main() {
.compact()
.init();
let sample: Vec<i32> = file_to_lines("samples/day01.txt")
.iter()
.map(|s| if s.is_empty() { -1 } else { s.parse().unwrap() })
.collect();
let input: Vec<i32> = file_to_lines("inputs/day01.txt")
.iter()
.map(|s| if s.is_empty() { -1 } else { s.parse().unwrap() })
.collect();
let sample = parse_input("samples/day01.txt");
let input = parse_input("inputs/day01.txt");
assert_eq!(part_1(&sample), 24000);
info!("Part 1: {}", part_1(&input));