diff --git a/advent_derive/src/lib.rs b/advent_derive/src/lib.rs index 29e636f..25a0619 100644 --- a/advent_derive/src/lib.rs +++ b/advent_derive/src/lib.rs @@ -40,7 +40,7 @@ pub fn advent_day(attrs: TokenStream, item: TokenStream, ) -> TokenStream { read_input_part1(self, Path::new(#input_str)) } - fn solve(&mut self) -> Result { + fn solve(&mut self) -> Result { solve_part1(self) } } @@ -50,7 +50,7 @@ pub fn advent_day(attrs: TokenStream, item: TokenStream, ) -> TokenStream { read_input_part2(self, Path::new(#input_str)) } - fn solve(&mut self) -> Result { + fn solve(&mut self) -> Result { solve_part2(self) } } @@ -70,12 +70,12 @@ pub fn advent_day(attrs: TokenStream, item: TokenStream, ) -> TokenStream { } impl AdventDay for #ident { - fn puzzle1(&mut self) -> Result { + fn puzzle1(&mut self) -> Result { self.part1.read_input()?; self.part1.solve() } - fn puzzle2(&mut self) -> Result { + fn puzzle2(&mut self) -> Result { self.part2.read_input()?; self.part2.solve() } diff --git a/src/days/day1.rs b/src/days/day1.rs index 1b7738c..3b1ab56 100644 --- a/src/days/day1.rs +++ b/src/days/day1.rs @@ -29,12 +29,12 @@ impl Day1 { } impl AdventDay for Day1 { - fn puzzle1(&mut self) -> Result { + fn puzzle1(&mut self) -> Result { self.part1.read_input()?; self.part1.solve() } - fn puzzle2(&mut self) -> Result { + fn puzzle2(&mut self) -> Result { self.part2.read_input()?; self.part2.solve() } @@ -72,17 +72,18 @@ impl AdventDayPart2 for Day1Part2 { Ok(()) } - fn solve(&mut self) -> Result { + fn solve(&mut self) -> Result { Ok(self.count1.keys().into_iter().map( |k| { - let s = *k as u64; - if self.count2.contains_key(k) { - s * self.count1[k] * self.count2[k] - } - else { - 0 - } - }).sum()) + let s = *k as u64; + if self.count2.contains_key(k) { + s * self.count1[k] * self.count2[k] + } + else { + 0 + } + }).sum::().to_string() + ) } } @@ -106,7 +107,7 @@ impl AdventDayPart1 for Day1Part1 { Ok(()) } - fn solve(&mut self) -> Result { + fn solve(&mut self) -> Result { self.list1.sort(); self.list2.sort(); @@ -114,6 +115,6 @@ impl AdventDayPart1 for Day1Part1 { .map(|(l, r)| { (l - r).abs() as u64 }) - .sum::()) + .sum::().to_string()) } } \ No newline at end of file diff --git a/src/days/day14.rs b/src/days/day14.rs index 863ac6f..551a291 100644 --- a/src/days/day14.rs +++ b/src/days/day14.rs @@ -71,7 +71,7 @@ fn read_input_part1(part1: &mut Day14Part1, path: &Path) -> Result<(), AdventErr Ok(()) } -fn solve_part1(part1: &mut Day14Part1) -> Result { +fn solve_part1(part1: &mut Day14Part1) -> Result { let mut counts : (u64, u64, u64 ,u64) = (0, 0, 0, 0); let left_half: u32 = (part1.width / 2) as u32; @@ -100,7 +100,7 @@ fn solve_part1(part1: &mut Day14Part1) -> Result { counts.3 += 1; } } - Ok(counts.0 * counts.1 * counts.2 * counts.3) + Ok((counts.0 * counts.1 * counts.2 * counts.3).to_string()) } fn read_input_part2(part2: &mut Day14Part2, path: &Path) -> Result<(), AdventError> { @@ -136,10 +136,10 @@ fn suspected_image(part2: &mut Day14Part2, required_cons: u32) -> bool { false } -fn solve_part2(part2: &mut Day14Part2) -> Result { +fn solve_part2(part2: &mut Day14Part2) -> Result { for i in 0 .. u64::MAX { if suspected_image(part2, 10) { - return Ok(i); + return Ok(i.to_string()); } for robot in part2.robots.iter_mut() { robot.apply(part2.width, part2.height); diff --git a/src/days/day2.rs b/src/days/day2.rs index a63c3c0..5b584af 100644 --- a/src/days/day2.rs +++ b/src/days/day2.rs @@ -64,12 +64,12 @@ impl Day2 { } impl AdventDay for Day2 { - fn puzzle1(&mut self) -> Result { + fn puzzle1(&mut self) -> Result { self.part1.read_input()?; self.part1.solve() } - fn puzzle2(&mut self) -> Result { + fn puzzle2(&mut self) -> Result { self.part2.read_input()?; self.part2.solve() } @@ -88,8 +88,8 @@ impl AdventDayPart1 for Day2Part1 { Ok(self.reports = read_into_vec::(Path::new("resources/input2.txt"))?) } - fn solve(&mut self) -> Result { - Ok(self.reports.iter().filter(|x| x.safe1()).count().try_into().unwrap()) + fn solve(&mut self) -> Result { + Ok(self.reports.iter().filter(|x| x.safe1()).count().to_string()) } } @@ -106,7 +106,7 @@ impl AdventDayPart2 for Day2Part2 { Ok(self.reports = read_into_vec::(Path::new("resources/input2.txt"))?) } - fn solve(&mut self) -> Result { - Ok(self.reports.iter().filter(|x| x.safe2()).count().try_into().unwrap()) + fn solve(&mut self) -> Result { + Ok(self.reports.iter().filter(|x| x.safe2()).count().to_string()) } } diff --git a/src/days/day3.rs b/src/days/day3.rs index 43b66d5..2ec8b59 100644 --- a/src/days/day3.rs +++ b/src/days/day3.rs @@ -30,8 +30,8 @@ impl AdventDayPart1 for Day3Part1 { Ok(()) } - fn solve(&mut self) -> Result { - Ok(self.0.iter().fold(Ok(0), |acc, mul| -> Result {Ok(acc? + TryInto::::try_into(mul.l * mul.r)?)})?) + fn solve(&mut self) -> Result { + Ok(self.0.iter().fold(Ok(0), |acc, mul| -> Result {Ok(acc? + TryInto::::try_into(mul.l * mul.r)?)})?.to_string()) } } @@ -68,8 +68,8 @@ impl AdventDayPart2 for Day3Part2 { Ok(()) } - fn solve(&mut self) -> Result { - Ok(self.0.iter().fold(Ok(0), |acc, mul| -> Result {Ok(acc? + TryInto::::try_into(mul.l * mul.r)?)})?) + fn solve(&mut self) -> Result { + Ok(self.0.iter().fold(Ok(0), |acc, mul| -> Result {Ok(acc? + TryInto::::try_into(mul.l * mul.r)?)})?.to_string()) } } @@ -88,12 +88,12 @@ impl Day3 { } impl AdventDay for Day3 { - fn puzzle1(&mut self) -> Result { + fn puzzle1(&mut self) -> Result { self.part1.read_input()?; self.part1.solve() } - fn puzzle2(&mut self) -> Result { + fn puzzle2(&mut self) -> Result { self.part2.read_input()?; self.part2.solve() } diff --git a/src/days/day4.rs b/src/days/day4.rs index 1dad281..34b7326 100644 --- a/src/days/day4.rs +++ b/src/days/day4.rs @@ -38,7 +38,7 @@ fn read_input_part1(part1: &mut Day4Part1, path: &Path) -> Result<(), AdventErro Ok(()) } -fn solve_part1(part1: &mut Day4Part1) -> Result { +fn solve_part1(part1: &mut Day4Part1) -> Result { let lr = part1.text.clone(); let mut ud = vec![]; let grid_size = part1.text.len(); @@ -123,7 +123,7 @@ fn solve_part1(part1: &mut Day4Part1) -> Result { sum += xmas_r.find_iter(s.as_str()).count() as u64; sum += samx_r.find_iter(s.as_str()).count() as u64; Ok(sum) - })? + })?.to_string() ) } @@ -154,7 +154,7 @@ fn is_xmas(part2: &mut Day4Part2, (x, y): (usize, usize)) -> bool { } -fn solve_part2(part2: &mut Day4Part2) -> Result { +fn solve_part2(part2: &mut Day4Part2) -> Result { let text = part2.text.clone(); Ok( text.iter().enumerate().fold(0, |acc, (y, s)| { @@ -165,7 +165,7 @@ fn solve_part2(part2: &mut Day4Part2) -> Result { acc } }) - }) + }).to_string() ) } diff --git a/src/days/day5.rs b/src/days/day5.rs index 0a6b07d..2902901 100644 --- a/src/days/day5.rs +++ b/src/days/day5.rs @@ -75,14 +75,14 @@ fn is_correct(part1: &mut Day5Part1, q: Vec) -> bool { }) } -fn solve_part1(part1: &mut Day5Part1) -> Result { +fn solve_part1(part1: &mut Day5Part1) -> Result { Ok(part1.queues.clone().into_iter().filter( |q| { is_correct(part1, q.clone()) } ).fold(0u64, | acc, item| { acc + item[item.len() / 2] as u64 - })) + }).to_string()) } fn read_input_part2(part2: &mut Day5Part2, path: &Path) -> Result<(), AdventError> { @@ -119,7 +119,7 @@ fn is_correct2(part1: &mut Day5Part2, q: Vec) -> bool { } -fn solve_part2(part2: &mut Day5Part2) -> Result { +fn solve_part2(part2: &mut Day5Part2) -> Result { let mut faulty = part2.queues.clone().into_iter().filter( |q| { !is_correct2(part2, q.clone()) @@ -148,7 +148,7 @@ fn solve_part2(part2: &mut Day5Part2) -> Result { } count += queue[queue.len() / 2] as u64; } - Ok(count) + Ok(count.to_string()) } diff --git a/src/days/day6.rs b/src/days/day6.rs index 92ca854..6d0e713 100644 --- a/src/days/day6.rs +++ b/src/days/day6.rs @@ -116,7 +116,7 @@ fn read_input_part1(part1: &mut Day6Part1, path: &Path) -> Result<(), AdventErro Ok(()) } -fn solve_part1(part1: &mut Day6Part1) -> Result { +fn solve_part1(part1: &mut Day6Part1) -> Result { let mut spaces = vec![]; spaces.push(part1.guard.position); loop { @@ -126,7 +126,7 @@ fn solve_part1(part1: &mut Day6Part1) -> Result { break; } } - Ok(spaces.iter().unique().count() as u64) + Ok(spaces.iter().unique().count().to_string()) } fn step(part1: &mut Day6Part1) -> Option<(i32, i32)> { @@ -197,7 +197,7 @@ fn step2(part2: &mut Day6Part2) -> Option<(i32, i32)> { } } -fn solve_part2(part2: &mut Day6Part2) -> Result { +fn solve_part2(part2: &mut Day6Part2) -> Result { let start_pos = part2.guard.position; let start_dir = part2.guard.direction; @@ -250,7 +250,7 @@ fn solve_part2(part2: &mut Day6Part2) -> Result { part2.obstacles.remove(remove_index); } - Ok(loops) + Ok(loops.to_string()) } diff --git a/src/days/day7.rs b/src/days/day7.rs index 5861599..7d3062c 100644 --- a/src/days/day7.rs +++ b/src/days/day7.rs @@ -64,10 +64,10 @@ fn possible(equation: &&Equation) -> bool { possible_answers.contains(&equation.result) } -fn solve_part1(part1: &mut Day7Part1) -> Result { +fn solve_part1(part1: &mut Day7Part1) -> Result { Ok(part1.equations.iter().filter(possible).fold(0, |acc, equation| { acc + equation.result - })) + }).to_string()) } fn read_input_part2(part2: &mut Day7Part2, path: &Path) -> Result<(), AdventError> { @@ -101,10 +101,10 @@ fn possible2(equation: &&Equation) -> bool { possible_answers.contains(&equation.result) } -fn solve_part2(part2: &mut Day7Part2) -> Result { +fn solve_part2(part2: &mut Day7Part2) -> Result { Ok(part2.equations.iter().filter(possible2).fold(0, |acc, equation| { acc + equation.result - })) + }).to_string()) } #[advent_day(Day7Part1, Day7Part2)] diff --git a/src/days/mod.rs b/src/days/mod.rs index 1ed6243..b3fbafc 100644 --- a/src/days/mod.rs +++ b/src/days/mod.rs @@ -2,17 +2,17 @@ use crate::error::AdventError; pub trait AdventDayPart1 { fn read_input(&mut self) -> Result<(),AdventError>; - fn solve(&mut self) -> Result; + fn solve(&mut self) -> Result; } pub trait AdventDayPart2 { fn read_input(&mut self) -> Result<(),AdventError>; - fn solve(&mut self) -> Result; + fn solve(&mut self) -> Result; } pub trait AdventDay { - fn puzzle1(&mut self) -> Result; - fn puzzle2(&mut self) -> Result; + fn puzzle1(&mut self) -> Result; + fn puzzle2(&mut self) -> Result; } pub mod day1; diff --git a/src/main.rs b/src/main.rs index 07e0c38..a18ebcd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,12 +27,12 @@ impl DummyDay { } impl AdventDay for DummyDay { - fn puzzle1(&mut self) -> Result { - Ok(0) + fn puzzle1(&mut self) -> Result { + Ok("0".to_string()) } - fn puzzle2(&mut self) -> Result { - Ok(0) + fn puzzle2(&mut self) -> Result { + Ok("0".to_string()) } } @@ -60,7 +60,7 @@ fn main() -> Result<(), AdventError> { let one_result = day.puzzle1()?; let two_result = day.puzzle2()?; Ok((one_result, two_result)) - }).enumerate().map(|(index, r): (usize, Result<(u64, u64), AdventError>)| { + }).enumerate().map(|(index, r): (usize, Result<(String, String), AdventError>)| { match r { Ok((res1, res2)) => println!("{}: ({}, {})", index + 1, res1, res2), Err(e) => println!("Error: {}", e.0)