diff --git a/rust/main.rs b/rust/main.rs index 71c853b..f13bccc 100644 --- a/rust/main.rs +++ b/rust/main.rs @@ -3,7 +3,6 @@ mod puzzles; use crate::puzzle::Puzzle; use puzzles::day1; use puzzles::day2; -use std::fmt::Display; use std::fs; const PUZZLES: [&dyn SomePuzzle; 2] = [&day1::PUZZLE, &day2::PUZZLE]; @@ -39,12 +38,12 @@ pub trait SomePuzzle { fn number(&self) -> u32; fn run(&self, input: &str) -> Vec; } -impl SomePuzzle for Puzzle { +impl SomePuzzle for Puzzle { fn number(&self) -> u32 { self.number } fn run(&self, s: &str) -> Vec { let input = (self.parser)(s); - self.parts.map(|p| p(&input).to_string() + "\n").to_vec() + self.parts.map(|p| p(&input) + "\n").to_vec() } } diff --git a/rust/puzzle.rs b/rust/puzzle.rs index 8f212de..8f01d12 100644 --- a/rust/puzzle.rs +++ b/rust/puzzle.rs @@ -1,5 +1,5 @@ -pub struct Puzzle { +pub struct Puzzle { pub number: u32, pub parser: fn(&str) -> Input, - pub parts: [fn(&Input) -> Output; N], + pub parts: [fn(&Input) -> String; N], } diff --git a/rust/puzzles/day1.rs b/rust/puzzles/day1.rs index 858d5a3..2d00a62 100644 --- a/rust/puzzles/day1.rs +++ b/rust/puzzles/day1.rs @@ -9,7 +9,7 @@ use nom::{ sequence::{pair, terminated}, }; -pub const PUZZLE: Puzzle, i32, 2> = Puzzle { +pub const PUZZLE: Puzzle, 2> = Puzzle { number: 1, parser: |input| { terminated( @@ -43,7 +43,7 @@ pub const PUZZLE: Puzzle, i32, 2> = Puzzle { } p = p1; } - r + r.to_string() }, |instructions| { let mut p = 50; @@ -65,7 +65,7 @@ pub const PUZZLE: Puzzle, i32, 2> = Puzzle { r += c1; p = p1; } - r + r.to_string() }, ], }; diff --git a/rust/puzzles/day2.rs b/rust/puzzles/day2.rs index e315ec9..01abcec 100644 --- a/rust/puzzles/day2.rs +++ b/rust/puzzles/day2.rs @@ -8,7 +8,7 @@ use nom::{ sequence::{separated_pair, terminated}, }; -pub const PUZZLE: Puzzle, usize, 2> = Puzzle { +pub const PUZZLE: Puzzle, 2> = Puzzle { number: 2, parser: |input| { terminated( @@ -32,7 +32,8 @@ pub const PUZZLE: Puzzle, usize, 2> = Puzzle { .flat_map(|(l, u)| { (*l..(u + 1)).flat_map(|n| if is_repetition_2(n) { Some(n) } else { None }) }) - .sum() + .sum::() + .to_string() }, |input| { input @@ -40,7 +41,8 @@ pub const PUZZLE: Puzzle, usize, 2> = Puzzle { .flat_map(|(l, u)| { (*l..(u + 1)).flat_map(|n| if is_repetition_n(n) { Some(n) } else { None }) }) - .sum() + .sum::() + .to_string() }, ], };