diff options
Diffstat (limited to 'src/bin/2022')
-rw-r--r-- | src/bin/2022/day10.rs | 16 | ||||
-rw-r--r-- | src/bin/2022/day5.rs | 18 |
2 files changed, 14 insertions, 20 deletions
diff --git a/src/bin/2022/day10.rs b/src/bin/2022/day10.rs index 8f7b617..ff9ab11 100644 --- a/src/bin/2022/day10.rs +++ b/src/bin/2022/day10.rs @@ -62,22 +62,26 @@ pub fn part1(ops: impl Iterator<Item = Op>) -> Result<i64> { Ok(total) } -pub fn part2(ops: impl Iterator<Item = Op>) -> Result<i64> { +pub fn part2(ops: impl Iterator<Item = Op>) -> Result<String> { let mut cpu = Cpu::new(); for op in ops { cpu.step(op); } + let mut s = String::new(); for (y, row) in cpu.history.chunks(40).enumerate() { + if row.len() < 40 { + break; + } for (x, pos) in row.iter().enumerate() { if i64::try_from(x).unwrap().abs_diff(*pos) <= 1 { - print!("#"); + s.push('#'); } else { - print!("."); + s.push('.'); } } - println!(); + s.push('\n'); } - Ok(0) + Ok(ocr(&s)) } #[test] @@ -88,6 +92,6 @@ fn test() { ); assert_eq!( part2(parse(parse::data(2022, 10).unwrap()).unwrap()).unwrap(), - 0 + "ZFBFHGUP" ); } diff --git a/src/bin/2022/day5.rs b/src/bin/2022/day5.rs index da1e5a2..b09e2e0 100644 --- a/src/bin/2022/day5.rs +++ b/src/bin/2022/day5.rs @@ -86,7 +86,7 @@ pub fn parse(fh: File) -> Result<Crates> { }) } -fn part1_str(mut crates: Crates) -> Result<String> { +pub fn part1(mut crates: Crates) -> Result<String> { for Instruction { count, from, to } in crates.instructions { for _ in 0..count { let c = crates.piles[from - 1].pop().unwrap(); @@ -100,12 +100,7 @@ fn part1_str(mut crates: Crates) -> Result<String> { .collect()) } -pub fn part1(crates: Crates) -> Result<i64> { - println!("{}", part1_str(crates)?); - Ok(0) -} - -fn part2_str(mut crates: Crates) -> Result<String> { +pub fn part2(mut crates: Crates) -> Result<String> { for Instruction { count, from, to } in crates.instructions { let mut tmp = vec![]; for _ in 0..count { @@ -123,19 +118,14 @@ fn part2_str(mut crates: Crates) -> Result<String> { .collect()) } -pub fn part2(crates: Crates) -> Result<i64> { - println!("{}", part2_str(crates)?); - Ok(0) -} - #[test] fn test() { assert_eq!( - part1_str(parse(parse::data(2022, 5).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2022, 5).unwrap()).unwrap()).unwrap(), "PSNRGBTFT" ); assert_eq!( - part2_str(parse(parse::data(2022, 5).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2022, 5).unwrap()).unwrap()).unwrap(), "BNTZFPMMW" ); } |