From c447b7fc71480f764765fd0763b4de66be2627f7 Mon Sep 17 00:00:00 2001 From: Sebastian Knackstedt Date: Sun, 11 Dec 2022 09:32:36 +0100 Subject: [PATCH] Add day 10 --- Cargo.toml | 4 ++ day10.txt | 137 +++++++++++++++++++++++++++++++++++++++++++ day10_test.txt | 146 ++++++++++++++++++++++++++++++++++++++++++++++ src/day10/main.rs | 131 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 418 insertions(+) create mode 100644 day10.txt create mode 100644 day10_test.txt create mode 100644 src/day10/main.rs diff --git a/Cargo.toml b/Cargo.toml index 57bc582..c644829 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,4 +45,8 @@ path = "src/day8/main.rs" name = "day9" path = "src/day9/main.rs" +[[bin]] +name = "day10" +path = "src/day10/main.rs" + [dependencies] diff --git a/day10.txt b/day10.txt new file mode 100644 index 0000000..8e16796 --- /dev/null +++ b/day10.txt @@ -0,0 +1,137 @@ +noop +noop +addx 5 +addx 3 +addx -2 +noop +addx 5 +addx 4 +noop +addx 3 +noop +addx 2 +addx -17 +addx 18 +addx 3 +addx 1 +noop +addx 5 +noop +addx 1 +addx 2 +addx 5 +addx -40 +noop +addx 5 +addx 2 +addx 3 +noop +addx 2 +addx 3 +addx -2 +addx 2 +addx 2 +noop +addx 3 +addx 5 +addx 2 +addx 3 +addx -2 +addx 2 +addx -24 +addx 31 +addx 2 +addx -33 +addx -6 +addx 5 +addx 2 +addx 3 +noop +addx 2 +addx 3 +noop +addx 2 +addx -1 +addx 6 +noop +noop +addx 1 +addx 4 +noop +noop +addx -15 +addx 20 +noop +addx -23 +addx 27 +noop +addx -35 +addx 1 +noop +noop +addx 5 +addx 11 +addx -10 +addx 4 +addx 1 +noop +addx 2 +addx 2 +noop +addx 3 +noop +addx 3 +addx 2 +noop +addx 3 +addx 2 +addx 11 +addx -4 +addx 2 +addx -38 +addx -1 +addx 2 +noop +addx 3 +addx 5 +addx 2 +addx -7 +addx 8 +addx 2 +addx 2 +noop +addx 3 +addx 5 +addx 2 +addx -25 +addx 26 +addx 2 +addx 8 +addx -1 +addx 2 +addx -2 +addx -37 +addx 5 +addx 3 +addx -1 +addx 5 +noop +addx 22 +addx -21 +addx 2 +addx 5 +addx 2 +addx 13 +addx -12 +addx 4 +noop +noop +addx 5 +addx 1 +noop +noop +addx 2 +noop +addx 3 +noop +noop \ No newline at end of file diff --git a/day10_test.txt b/day10_test.txt new file mode 100644 index 0000000..94cd0a8 --- /dev/null +++ b/day10_test.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 \ No newline at end of file diff --git a/src/day10/main.rs b/src/day10/main.rs new file mode 100644 index 0000000..2d4b591 --- /dev/null +++ b/src/day10/main.rs @@ -0,0 +1,131 @@ +use base::read_file; + +struct CRT { + display: [[char; 40]; 6], + row_idx: usize, + col_idx: usize, +} + +impl CRT { + fn new() -> Self { + Self { + display: [['\0'; 40]; 6], + col_idx: 0, + row_idx: 0, + } + } + + fn clock(&mut self, register: i32) { + if self.col_idx >= 6 || self.row_idx >= 40 { + return; + } + let range = ((register - 1).max(0))..=((register + 1).min(39)); + self.display[self.col_idx][self.row_idx] = if range.contains(&(self.row_idx as i32)) { '⭐' } else { '🌲' }; + + self.row_idx += 1; + if self.row_idx % 40 == 0 { + self.row_idx = 0; + self.col_idx += 1; + }; + } + + fn display(&self) { + for col in 0..6 { + for row in 0..40 { + print!("{}", self.display[col][row]); + } + println!(); + } + } +} + +#[derive(Debug, Copy, Clone)] +enum Op { + NOOP, + ADD { + amount: i32 + }, +} + +struct Machine { + program: Vec, + register: i32, + register_next: i32, + instruction_counter: usize, + instruction_left_counter: u32, + current_option: Option, +} + +impl Machine { + fn new(program: Vec) -> Self { + Self { + program, + register: 1, + register_next: 1, + instruction_counter: 0, + instruction_left_counter: 0, + current_option: None, + } + } + + fn clock(&mut self, crt: &mut CRT) { + self.register = self.register_next; + crt.clock(self.register); + if self.instruction_left_counter == 0 { + if self.instruction_counter >= self.program.len() { + self.current_option = None; + self.instruction_left_counter = 0; + return; + } + let next_op = self.program[self.instruction_counter]; + self.instruction_counter += 1; + match next_op { + Op::NOOP => { + self.instruction_left_counter = 0; + } + Op::ADD { .. } => { + self.instruction_left_counter = 1; + } + } + self.current_option = Some(next_op); + } else { + if let Some(current_op) = self.current_option { + match current_op { + Op::NOOP => {} + Op::ADD { amount } => { self.register_next = self.register + amount; } + } + } + self.instruction_left_counter -= 1; + } + } +} + +fn main() { + let lines = read_file("day10.txt"); + let mut ops = vec![]; + for line in lines { + if line.starts_with("noop") { ops.push(Op::NOOP); } else if line.starts_with("addx") { + let amount = line.split_whitespace().collect::>()[1].parse::().unwrap(); + ops.push(Op::ADD { amount }); + } + } + + let mut crt = CRT::new(); + let mut machine = Machine::new(ops); + + let mut signals = vec![]; + + for cycle in 0..2220 { + machine.clock(&mut crt); + let cycle = cycle + 1; + match cycle { + 20 | 60 | 100 | 140 | 180 | 220 => { + signals.push(cycle * machine.register); + } + _ => {} + } + } + + println!("Sum: {}", signals.iter().sum::()); + crt.display(); +} \ No newline at end of file