From 100be8c55f2e9e6a47480fae67f6f160c1e32455 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sat, 10 Dec 2022 16:30:42 -0800 Subject: [PATCH] done with part2 --- 2022-aoc/src/d10.rs | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/2022-aoc/src/d10.rs b/2022-aoc/src/d10.rs index 3d6d932..c9c2b8d 100644 --- a/2022-aoc/src/d10.rs +++ b/2022-aoc/src/d10.rs @@ -71,6 +71,12 @@ impl Vm { } } +fn cycle2beam(cycle: usize) -> (usize, usize) { + let col = (cycle - 1) % 40; + let row = (cycle - 1) / 40; + (row, col) +} + #[aoc_generator(day10)] fn parse_input(input: &str) -> Vm { let mut data = Vec::new(); @@ -102,8 +108,28 @@ fn part1(input: &Vm) -> i32 { } #[aoc_run(day10, part2)] -fn part2(input: &Vm) -> u32 { - 0 +fn part2(input: &Vm) -> String { + let mut vm = input.clone(); + let mut crt = vec![vec![' '; 40]; 6]; + + for _ in 0..240 { + let (cycle, x) = vm.run(); + let (row, col) = cycle2beam(cycle); + let diff = (x.abs() - col as i32).abs(); + if diff < 2 { + crt[row][col] = '#'; + } + } + + let mut out = String::with_capacity(248); + out.push('\n'); + for row in crt { + for char in row.iter() { + out.push(*char); + } + out.push('\n'); + } + out } #[cfg(test)] @@ -267,6 +293,15 @@ noop"; #[test] fn part2_test() { let vm = parse_input(INPUT); - assert_eq!(part2(&vm), 1); + let expected = " +##..##..##..##..##..##..##..##..##..##.. +###...###...###...###...###...###...###. +####....####....####....####....####.... +#####.....#####.....#####.....#####..... +######......######......######......#### +#######.......#######.......#######....."; + let out = part2(&vm); + println!("{}", &out); + assert_eq!(&out.trim(), &expected.trim()); } }