From b6eaa973c40e000a5a3f33aa145c9dfa915934c1 Mon Sep 17 00:00:00 2001 From: mat ess Date: Sat, 10 Dec 2022 12:14:00 -0500 Subject: [PATCH] Day 10 --- inputs/10.small.txt | 146 ++++++++++++++++++++++++++++++++++++++++++++ inputs/10.txt | 145 +++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 101 ++++++++++++++++++++++++++++++ 3 files changed, 392 insertions(+) create mode 100644 inputs/10.small.txt create mode 100644 inputs/10.txt diff --git a/inputs/10.small.txt b/inputs/10.small.txt new file mode 100644 index 0000000..37ee8ee --- /dev/null +++ b/inputs/10.small.txt @@ -0,0 +1,146 @@ +addx 15 +addx -11 +addx 6 +addx -3 +addx 5 +addx -1 +addx -8 +addx 13 +addx 4 +noop +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx -35 +addx 1 +addx 24 +addx -19 +addx 1 +addx 16 +addx -11 +noop +noop +addx 21 +addx -15 +noop +noop +addx -3 +addx 9 +addx 1 +addx -3 +addx 8 +addx 1 +addx 5 +noop +noop +noop +noop +noop +addx -36 +noop +addx 1 +addx 7 +noop +noop +noop +addx 2 +addx 6 +noop +noop +noop +noop +noop +addx 1 +noop +noop +addx 7 +addx 1 +noop +addx -13 +addx 13 +addx 7 +noop +addx 1 +addx -33 +noop +noop +noop +addx 2 +noop +noop +noop +addx 8 +noop +addx -1 +addx 2 +addx 1 +noop +addx 17 +addx -9 +addx 1 +addx 1 +addx -3 +addx 11 +noop +noop +addx 1 +noop +addx 1 +noop +noop +addx -13 +addx -19 +addx 1 +addx 3 +addx 26 +addx -30 +addx 12 +addx -1 +addx 3 +addx 1 +noop +noop +noop +addx -9 +addx 18 +addx 1 +addx 2 +noop +noop +addx 9 +noop +noop +noop +addx -1 +addx 2 +addx -37 +addx 1 +addx 3 +noop +addx 15 +addx -21 +addx 22 +addx -6 +addx 1 +noop +addx 2 +addx 1 +noop +addx -10 +noop +noop +addx 20 +addx 1 +addx 2 +addx 2 +addx -6 +addx -11 +noop +noop +noop diff --git a/inputs/10.txt b/inputs/10.txt new file mode 100644 index 0000000..6402c1a --- /dev/null +++ b/inputs/10.txt @@ -0,0 +1,145 @@ +noop +noop +noop +addx 3 +addx 7 +noop +noop +noop +noop +addx 6 +noop +addx -1 +noop +addx 5 +addx 1 +noop +addx 4 +noop +noop +noop +noop +addx 6 +addx -1 +noop +addx 3 +addx -13 +addx -22 +noop +noop +addx 3 +addx 2 +addx 11 +addx -4 +addx 11 +addx -10 +addx 2 +addx 5 +addx 2 +addx -2 +noop +addx 7 +addx 3 +addx -2 +addx 2 +addx 5 +addx 2 +addx -2 +addx -8 +addx -27 +addx 5 +addx 2 +addx 21 +addx -21 +addx 3 +addx 5 +addx 2 +addx -3 +addx 4 +addx 3 +addx 1 +addx 5 +noop +noop +noop +noop +addx 3 +addx 1 +addx 6 +addx -31 +noop +addx -4 +noop +noop +noop +noop +addx 3 +addx 7 +noop +addx -1 +addx 1 +addx 5 +noop +addx 1 +noop +addx 2 +addx -8 +addx 15 +addx 3 +noop +addx 2 +addx 5 +noop +noop +noop +addx -28 +addx 11 +addx -20 +noop +addx 7 +addx -2 +addx 7 +noop +addx -2 +noop +addx -6 +addx 11 +noop +addx 3 +addx 2 +noop +noop +addx 7 +addx 3 +addx -2 +addx 2 +addx 5 +addx 2 +addx -16 +addx -10 +addx -11 +addx 27 +addx -20 +noop +addx 2 +addx 3 +addx 5 +noop +noop +noop +addx 3 +addx -2 +addx 2 +noop +addx -14 +addx 21 +noop +addx -6 +addx 12 +noop +addx -21 +addx 24 +addx 2 +noop +noop +noop diff --git a/src/main.rs b/src/main.rs index f2f5551..e891f58 100644 --- a/src/main.rs +++ b/src/main.rs @@ -250,9 +250,110 @@ solutions! { let visits = motion_visits(motions, 10); (visits.len() as u64).into() }, + ], + [ + // day 10 part 1 + |input| { + let cmds = input.lines().map(Cmd::from_str).collect(); + Screen::new().get_signal_strength(cmds).into() + }, + // day 10 part 2 + |input| { + let cmds = input.lines().map(Cmd::from_str).collect(); + Screen::new().draw(cmds).to_string().into() + } ] } +struct Screen { + x: isize, + cycle: isize, + strength: isize, + output: String, +} + +impl Screen { + fn new() -> Self { + Screen { + x: 1, + cycle: 1, + strength: 0, + output: String::new(), + } + } + + fn step_cycle(&mut self) { + println!("cycle {}, x={}", self.cycle, self.x); + self.cycle += 1; + if (self.cycle - 20) % 40 == 0 { + println!( + "cycle {}, x={}, signal={}", + self.cycle, + self.x, + self.x * self.cycle + ); + self.strength += self.x * self.cycle; + } + } + + fn get_signal_strength(&mut self, cmds: Vec) -> u64 { + for cmd in cmds { + match cmd { + Cmd::Noop => self.step_cycle(), + Cmd::Addx(n) => { + self.step_cycle(); + self.x += n; + self.step_cycle(); + } + } + } + self.strength as u64 + } + + fn draw_cycle(&mut self) { + let coord = (self.cycle - 1) % 40; + let sprite = self.x - 1..=self.x + 1; + if sprite.contains(&coord) { + self.output.push('#'); + } else { + self.output.push('.'); + } + if self.cycle % 40 == 0 { + self.output.push('\n') + } + self.cycle += 1 + } + + fn draw(&mut self, cmds: Vec) -> &str { + for cmd in cmds { + match cmd { + Cmd::Noop => self.draw_cycle(), + Cmd::Addx(n) => { + self.draw_cycle(); + self.draw_cycle(); + self.x += n; + } + } + } + &self.output + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +enum Cmd { + Noop, + Addx(isize), +} + +impl Cmd { + fn from_str(s: &str) -> Self { + match s { + "noop" => Cmd::Noop, + _ => Cmd::Addx(s[5..].parse::().unwrap()), + } + } +} + #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] struct Pos { x: isize,