Refactor output of days to String

This commit is contained in:
Dennis Brentjes 2024-12-26 19:43:08 +01:00
parent 036a033548
commit 311cc0509a
11 changed files with 59 additions and 58 deletions

View File

@ -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<u64, AdventError> {
fn solve(&mut self) -> Result<String, AdventError> {
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<u64, AdventError> {
fn solve(&mut self) -> Result<String, AdventError> {
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<u64, AdventError> {
fn puzzle1(&mut self) -> Result<String, AdventError> {
self.part1.read_input()?;
self.part1.solve()
}
fn puzzle2(&mut self) -> Result<u64, AdventError> {
fn puzzle2(&mut self) -> Result<String, AdventError> {
self.part2.read_input()?;
self.part2.solve()
}

View File

@ -29,12 +29,12 @@ impl Day1 {
}
impl AdventDay for Day1 {
fn puzzle1(&mut self) -> Result<u64, AdventError> {
fn puzzle1(&mut self) -> Result<String, AdventError> {
self.part1.read_input()?;
self.part1.solve()
}
fn puzzle2(&mut self) -> Result<u64, AdventError> {
fn puzzle2(&mut self) -> Result<String, AdventError> {
self.part2.read_input()?;
self.part2.solve()
}
@ -72,17 +72,18 @@ impl AdventDayPart2 for Day1Part2 {
Ok(())
}
fn solve(&mut self) -> Result<u64, AdventError> {
fn solve(&mut self) -> Result<String, AdventError> {
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::<u64>().to_string()
)
}
}
@ -106,7 +107,7 @@ impl AdventDayPart1 for Day1Part1 {
Ok(())
}
fn solve(&mut self) -> Result<u64, AdventError> {
fn solve(&mut self) -> Result<String, AdventError> {
self.list1.sort();
self.list2.sort();
@ -114,6 +115,6 @@ impl AdventDayPart1 for Day1Part1 {
.map(|(l, r)| {
(l - r).abs() as u64
})
.sum::<u64>())
.sum::<u64>().to_string())
}
}

View File

@ -71,7 +71,7 @@ fn read_input_part1(part1: &mut Day14Part1, path: &Path) -> Result<(), AdventErr
Ok(())
}
fn solve_part1(part1: &mut Day14Part1) -> Result<u64, AdventError> {
fn solve_part1(part1: &mut Day14Part1) -> Result<String, AdventError> {
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<u64, AdventError> {
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<u64, AdventError> {
fn solve_part2(part2: &mut Day14Part2) -> Result<String, AdventError> {
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);

View File

@ -64,12 +64,12 @@ impl Day2 {
}
impl AdventDay for Day2 {
fn puzzle1(&mut self) -> Result<u64, AdventError> {
fn puzzle1(&mut self) -> Result<String, AdventError> {
self.part1.read_input()?;
self.part1.solve()
}
fn puzzle2(&mut self) -> Result<u64, AdventError> {
fn puzzle2(&mut self) -> Result<String, AdventError> {
self.part2.read_input()?;
self.part2.solve()
}
@ -88,8 +88,8 @@ impl AdventDayPart1 for Day2Part1 {
Ok(self.reports = read_into_vec::<Report>(Path::new("resources/input2.txt"))?)
}
fn solve(&mut self) -> Result<u64, AdventError> {
Ok(self.reports.iter().filter(|x| x.safe1()).count().try_into().unwrap())
fn solve(&mut self) -> Result<String, AdventError> {
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::<Report>(Path::new("resources/input2.txt"))?)
}
fn solve(&mut self) -> Result<u64, AdventError> {
Ok(self.reports.iter().filter(|x| x.safe2()).count().try_into().unwrap())
fn solve(&mut self) -> Result<String, AdventError> {
Ok(self.reports.iter().filter(|x| x.safe2()).count().to_string())
}
}

View File

@ -30,8 +30,8 @@ impl AdventDayPart1 for Day3Part1 {
Ok(())
}
fn solve(&mut self) -> Result<u64, AdventError> {
Ok(self.0.iter().fold(Ok(0), |acc, mul| -> Result<u64,AdventError> {Ok(acc? + TryInto::<u64>::try_into(mul.l * mul.r)?)})?)
fn solve(&mut self) -> Result<String, AdventError> {
Ok(self.0.iter().fold(Ok(0), |acc, mul| -> Result<u64,AdventError> {Ok(acc? + TryInto::<u64>::try_into(mul.l * mul.r)?)})?.to_string())
}
}
@ -68,8 +68,8 @@ impl AdventDayPart2 for Day3Part2 {
Ok(())
}
fn solve(&mut self) -> Result<u64, AdventError> {
Ok(self.0.iter().fold(Ok(0), |acc, mul| -> Result<u64,AdventError> {Ok(acc? + TryInto::<u64>::try_into(mul.l * mul.r)?)})?)
fn solve(&mut self) -> Result<String, AdventError> {
Ok(self.0.iter().fold(Ok(0), |acc, mul| -> Result<u64,AdventError> {Ok(acc? + TryInto::<u64>::try_into(mul.l * mul.r)?)})?.to_string())
}
}
@ -88,12 +88,12 @@ impl Day3 {
}
impl AdventDay for Day3 {
fn puzzle1(&mut self) -> Result<u64, crate::error::AdventError> {
fn puzzle1(&mut self) -> Result<String, crate::error::AdventError> {
self.part1.read_input()?;
self.part1.solve()
}
fn puzzle2(&mut self) -> Result<u64, crate::error::AdventError> {
fn puzzle2(&mut self) -> Result<String, crate::error::AdventError> {
self.part2.read_input()?;
self.part2.solve()
}

View File

@ -38,7 +38,7 @@ fn read_input_part1(part1: &mut Day4Part1, path: &Path) -> Result<(), AdventErro
Ok(())
}
fn solve_part1(part1: &mut Day4Part1) -> Result<u64, AdventError> {
fn solve_part1(part1: &mut Day4Part1) -> Result<String, AdventError> {
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<u64, AdventError> {
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<u64, AdventError> {
fn solve_part2(part2: &mut Day4Part2) -> Result<String, AdventError> {
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<u64, AdventError> {
acc
}
})
})
}).to_string()
)
}

View File

@ -75,14 +75,14 @@ fn is_correct(part1: &mut Day5Part1, q: Vec<u32>) -> bool {
})
}
fn solve_part1(part1: &mut Day5Part1) -> Result<u64, AdventError> {
fn solve_part1(part1: &mut Day5Part1) -> Result<String, AdventError> {
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<u32>) -> bool {
}
fn solve_part2(part2: &mut Day5Part2) -> Result<u64, AdventError> {
fn solve_part2(part2: &mut Day5Part2) -> Result<String, AdventError> {
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<u64, AdventError> {
}
count += queue[queue.len() / 2] as u64;
}
Ok(count)
Ok(count.to_string())
}

View File

@ -116,7 +116,7 @@ fn read_input_part1(part1: &mut Day6Part1, path: &Path) -> Result<(), AdventErro
Ok(())
}
fn solve_part1(part1: &mut Day6Part1) -> Result<u64, AdventError> {
fn solve_part1(part1: &mut Day6Part1) -> Result<String, AdventError> {
let mut spaces = vec![];
spaces.push(part1.guard.position);
loop {
@ -126,7 +126,7 @@ fn solve_part1(part1: &mut Day6Part1) -> Result<u64, AdventError> {
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<u64, AdventError> {
fn solve_part2(part2: &mut Day6Part2) -> Result<String, AdventError> {
let start_pos = part2.guard.position;
let start_dir = part2.guard.direction;
@ -250,7 +250,7 @@ fn solve_part2(part2: &mut Day6Part2) -> Result<u64, AdventError> {
part2.obstacles.remove(remove_index);
}
Ok(loops)
Ok(loops.to_string())
}

View File

@ -64,10 +64,10 @@ fn possible(equation: &&Equation) -> bool {
possible_answers.contains(&equation.result)
}
fn solve_part1(part1: &mut Day7Part1) -> Result<u64, AdventError> {
fn solve_part1(part1: &mut Day7Part1) -> Result<String, AdventError> {
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<u64, AdventError> {
fn solve_part2(part2: &mut Day7Part2) -> Result<String, AdventError> {
Ok(part2.equations.iter().filter(possible2).fold(0, |acc, equation| {
acc + equation.result
}))
}).to_string())
}
#[advent_day(Day7Part1, Day7Part2)]

View File

@ -2,17 +2,17 @@ use crate::error::AdventError;
pub trait AdventDayPart1 {
fn read_input(&mut self) -> Result<(),AdventError>;
fn solve(&mut self) -> Result<u64, AdventError>;
fn solve(&mut self) -> Result<String, AdventError>;
}
pub trait AdventDayPart2 {
fn read_input(&mut self) -> Result<(),AdventError>;
fn solve(&mut self) -> Result<u64, AdventError>;
fn solve(&mut self) -> Result<String, AdventError>;
}
pub trait AdventDay {
fn puzzle1(&mut self) -> Result<u64, AdventError>;
fn puzzle2(&mut self) -> Result<u64, AdventError>;
fn puzzle1(&mut self) -> Result<String, AdventError>;
fn puzzle2(&mut self) -> Result<String, AdventError>;
}
pub mod day1;

View File

@ -27,12 +27,12 @@ impl DummyDay {
}
impl AdventDay for DummyDay {
fn puzzle1(&mut self) -> Result<u64, AdventError> {
Ok(0)
fn puzzle1(&mut self) -> Result<String, AdventError> {
Ok("0".to_string())
}
fn puzzle2(&mut self) -> Result<u64, AdventError> {
Ok(0)
fn puzzle2(&mut self) -> Result<String, AdventError> {
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)