Add day 10

This commit is contained in:
Sebastian Knackstedt 2022-12-11 09:32:36 +01:00
parent e76573b78c
commit c447b7fc71
Signed by: sebastian
GPG Key ID: CDCD9AF904D93EF7
4 changed files with 418 additions and 0 deletions

View File

@ -45,4 +45,8 @@ path = "src/day8/main.rs"
name = "day9" name = "day9"
path = "src/day9/main.rs" path = "src/day9/main.rs"
[[bin]]
name = "day10"
path = "src/day10/main.rs"
[dependencies] [dependencies]

137
day10.txt Normal file
View File

@ -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

146
day10_test.txt Normal file
View File

@ -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

131
src/day10/main.rs Normal file
View File

@ -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<Op>,
register: i32,
register_next: i32,
instruction_counter: usize,
instruction_left_counter: u32,
current_option: Option<Op>,
}
impl Machine {
fn new(program: Vec<Op>) -> 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::<Vec<_>>()[1].parse::<i32>().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::<i32>());
crt.display();
}