From 70f6149889511f61f91f525eba5a82e938cb41ec Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 11 Dec 2022 22:09:16 -0500 Subject: support string solutions --- src/bin/2021/day13.rs | 7 +++---- src/bin/2022/day10.rs | 16 ++++++++++------ src/bin/2022/day5.rs | 18 ++++-------------- 3 files changed, 17 insertions(+), 24 deletions(-) (limited to 'src/bin') diff --git a/src/bin/2021/day13.rs b/src/bin/2021/day13.rs index 75e547b..b21071f 100644 --- a/src/bin/2021/day13.rs +++ b/src/bin/2021/day13.rs @@ -122,13 +122,12 @@ pub fn part1( pub fn part2( (mut paper, folds): (Paper, Vec<(bool, usize)>), -) -> Result { +) -> Result { for fold in folds { paper.fold(fold.0, fold.1); } - println!("{}", paper); - Ok(paper.total()) + Ok(ocr(&paper.to_string())) } #[test] @@ -139,6 +138,6 @@ fn test() { ); assert_eq!( part2(parse(parse::data(2021, 13).unwrap()).unwrap()).unwrap(), - 95 + "ECFHLHZF" ); } 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) -> Result { Ok(total) } -pub fn part2(ops: impl Iterator) -> Result { +pub fn part2(ops: impl Iterator) -> Result { 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 { }) } -fn part1_str(mut crates: Crates) -> Result { +pub fn part1(mut crates: Crates) -> Result { 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 { .collect()) } -pub fn part1(crates: Crates) -> Result { - println!("{}", part1_str(crates)?); - Ok(0) -} - -fn part2_str(mut crates: Crates) -> Result { +pub fn part2(mut crates: Crates) -> Result { 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 { .collect()) } -pub fn part2(crates: Crates) -> Result { - 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" ); } -- cgit v1.2.3-54-g00ecf