From d4fcb8a4a815ce8c888c3e06330e9cff71e3c312 Mon Sep 17 00:00:00 2001 From: mathiasmagnusson Date: Mon, 12 Dec 2022 22:33:07 +0100 Subject: Day 10 --- aoc22/day10/src/main.rs | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 aoc22/day10/src/main.rs (limited to 'aoc22/day10/src/main.rs') diff --git a/aoc22/day10/src/main.rs b/aoc22/day10/src/main.rs new file mode 100644 index 0000000..42d6490 --- /dev/null +++ b/aoc22/day10/src/main.rs @@ -0,0 +1,62 @@ +fn main() { + let input = lib::read_input(10); + + part1(&input); + part2(&input); +} + +fn part1(input: &str) { + let addx = "addx "; + + let mut x = 1; + let mut cycle = 1; + let mut ans = 0; + for line in input.lines() { + if line.starts_with(addx) { + cycle += 1; + if [20, 60, 100, 140, 180, 220].contains(&cycle) { + ans += cycle * x; + } + x += line[addx.len()..].parse::().unwrap(); + cycle += 1; + if [20, 60, 100, 140, 180, 220].contains(&cycle) { + ans += cycle * x; + } + } else { + cycle += 1; + if [20, 60, 100, 140, 180, 220].contains(&cycle) { + ans += cycle * x; + } + } + } + println!("{}", ans); +} + +fn part2(input: &str) { + let addx = "addx "; + + let mut x = 1; + let mut cycle = 1i32; + let mut ans = ['.'; 40 * 6]; + for line in input.lines() { + if line.starts_with(addx) { + if [x - 1, x, x + 1].contains(&(cycle % 40)) { + ans[cycle as usize - 1] = '#'; + } + cycle += 1; + x += line[addx.len()..].parse::().unwrap(); + if [x - 1, x, x + 1].contains(&(cycle % 40)) { + ans[cycle as usize - 1] = '#'; + } + cycle += 1; + } else { + if [x - 1, x, x + 1].contains(&(cycle % 40)) { + ans[cycle as usize - 1] = '#'; + } + cycle += 1; + } + } + for i in 0..6 { + println!("{}", &ans[i * 40..(i + 1) * 40].iter().collect::()); + } +} -- cgit v1.2.3