diff --git a/src/day1.rs b/src/day1.rs index d805e89..c3cd05a 100644 --- a/src/day1.rs +++ b/src/day1.rs @@ -2,6 +2,19 @@ use crate::day_solver::DaySolver; use super::util; +const NUMBERS: [[&'static str; 2]; 10] = [ + ["0", "zero"], + ["1", "one"], + ["2", "two"], + ["3", "three"], + ["4", "four"], + ["5", "five"], + ["6", "six"], + ["7", "seven"], + ["8", "eight"], + ["9", "nine"] +]; + pub struct Day1 { lines: Vec } @@ -9,7 +22,7 @@ pub struct Day1 { impl Day1 { pub fn create() -> Self { - let lines = util::read_file("input/day1_example2.txt"); + let lines = util::read_file("input/day1.txt"); // let lines = util::read_file("input/dayX.txt"); // Put the input into the day struct @@ -20,6 +33,7 @@ impl Day1 { fn solve(lines: &Vec) -> u32 { lines.iter() + .filter(|l| l.chars().any(|c| c.is_numeric())) .map(|l| { let nrs: Vec = l.chars() .filter(|c| c.is_numeric()) @@ -28,6 +42,38 @@ impl Day1 { u32::from(nrs.first().unwrap().to_owned()) * 10 + u32::from(nrs.last().unwrap().to_owned()) }).sum::() } + + fn find_first_number(line: &String) -> u32 { + let mut min_idx = line.len(); + let mut num_at_idx: u32 = 0; + for (n, nr_strings) in NUMBERS.iter().enumerate() { + for nr_string in nr_strings { + if let Some(i) = line.find(nr_string) { + if i < min_idx { + num_at_idx = n as u32; + min_idx = i; + } + } + } + } + num_at_idx + } + + fn find_last_number(line: &String) -> u32 { + let mut max_idx = 0usize; + let mut num_at_idx = 0u32; + for (n, nr_strings) in NUMBERS.iter().enumerate() { + for nr_string in nr_strings { + if let Some(i) = line.rfind(nr_string) { + if i >= max_idx { + num_at_idx = n as u32; + max_idx = i; + } + } + } + } + num_at_idx + } } impl DaySolver for Day1 { @@ -38,17 +84,8 @@ impl DaySolver for Day1 { } fn solve_part2(&mut self) -> String { - return Self::solve(&self.lines.iter().map( - |l| l - .replace("one", "1") - .replace("two", "2") - .replace("three", "3") - .replace("four", "4") - .replace("five", "5") - .replace("six", "6") - .replace("seven", "7") - .replace("eight", "8") - .replace("nine", "9") - ).collect()).to_string() + self.lines.iter().map( + |l| Self::find_first_number(l) * 10 + Self::find_last_number(l) + ).sum::().to_string() } } diff --git a/src/main.rs b/src/main.rs index 71d7667..6bae6e6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -87,13 +87,12 @@ fn solve(day: u8, silent: bool) -> AocBenchResult { match solver { Some(mut s) => { - // let(part1, pt1_time) = bench(|| s.solve_part1()); - // part1_time = pt1_time; - // - // if !silent { - // println!("Day {} Part 1: {}", day, part1); - // } - part1_time = 0; + let(part1, pt1_time) = bench(|| s.solve_part1()); + part1_time = pt1_time; + + if !silent { + println!("Day {} Part 1: {}", day, part1); + } let (part2, pt2_time) = bench(|| s.solve_part2()); part2_time = pt2_time; if !silent { diff --git a/src/util.rs b/src/util.rs index 45c4ec1..d62660a 100644 --- a/src/util.rs +++ b/src/util.rs @@ -54,6 +54,7 @@ impl Grid { &self.data[idx] } + #[allow(dead_code)] pub fn set(&mut self, x: &usize, y: &usize, v: T) { let idx = self.idx(x, y); self.data[idx] = v;