From 1b9ea6972c94433c9bcde5551a5437a26671db2f Mon Sep 17 00:00:00 2001 From: Jeremy Kaplan Date: Fri, 9 Dec 2022 21:55:07 -0800 Subject: [PATCH] 2022 Day 10 --- 2022/src/bin/day10.rs | 81 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 2022/src/bin/day10.rs diff --git a/2022/src/bin/day10.rs b/2022/src/bin/day10.rs new file mode 100644 index 0000000..3d7c108 --- /dev/null +++ b/2022/src/bin/day10.rs @@ -0,0 +1,81 @@ +const INPUT: &str = include_str!("../../input/day10.txt"); + +fn main() { + let instructions = parse(INPUT); + + let mut crt = Crt::new(); + + for i in instructions { + crt.run(i); + } + + println!("Part 1: {}", crt.strength); + println!("{}", crt.image); +} + +#[derive(Debug, Copy, Clone)] +enum Instruction { + Noop, + Addx(i64), +} + +fn parse(input: &str) -> Vec { + aoc::split_lines(input) + .iter() + .map(|line| { + let w = aoc::split_words(line); + match w[0] { + "noop" => Instruction::Noop, + "addx" => Instruction::Addx(w[1].parse().unwrap()), + other => panic!("{}", other), + } + }) + .collect() +} + +#[derive(Debug, Clone)] +struct Crt { + cycle: usize, + x: i64, + + strength: i64, + image: String, +} + +impl Crt { + fn new() -> Self { + Self { + cycle: 0, + x: 1, + + strength: 0, + image: String::new(), + } + } + + fn run(&mut self, i: Instruction) { + let dxs = match i { + Instruction::Noop => vec![0], + Instruction::Addx(dx) => vec![0, dx], + }; + + for dx in dxs { + let scanx = (self.cycle as i64) % 40; + if scanx == 0 { + self.image += "\n"; + } + + self.cycle += 1; + + let pixel = if (self.x - scanx).abs() < 2 { "#" } else { "." }; + self.image += pixel; + + let signal = (self.cycle as i64) * self.x; + if (self.cycle + 20) % 40 == 0 { + self.strength += signal; + } + + self.x += dx; + } + } +}