AOC 2022 day 10

This commit is contained in:
Kienan Stewart 2022-12-10 09:12:22 -05:00
parent e79611a922
commit 59139d65d6
5 changed files with 447 additions and 0 deletions

14
2022/10/Cargo.lock generated Normal file
View File

@ -0,0 +1,14 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "common"
version = "0.1.0"
[[package]]
name = "day10"
version = "0.1.0"
dependencies = [
"common",
]

9
2022/10/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "day10"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
common = { path = "../common" }

144
2022/10/input Normal file
View File

@ -0,0 +1,144 @@
noop
noop
noop
addx 6
addx -1
addx 5
noop
noop
noop
addx 5
addx -8
addx 9
addx 3
addx 2
addx 4
addx 3
noop
addx 2
noop
addx 1
addx 6
noop
noop
noop
addx -39
noop
addx 5
addx 2
addx -2
addx 3
addx 2
addx 5
addx 2
addx 2
addx 13
addx -12
noop
addx 7
noop
addx 2
addx 3
noop
addx -25
addx 30
addx -10
addx 13
addx -40
noop
addx 5
addx 2
addx 3
noop
addx 2
addx 3
addx -2
addx 3
addx -1
addx 7
noop
noop
addx 5
addx -1
addx 6
noop
noop
noop
noop
addx 9
noop
addx -1
noop
addx -39
addx 2
addx 33
addx -29
addx 1
noop
addx 4
noop
noop
noop
addx 3
addx 2
noop
addx 3
noop
noop
addx 7
addx 2
addx 3
addx -2
noop
addx -30
noop
addx 40
addx -2
addx -38
noop
noop
noop
addx 5
addx 5
addx 2
addx -9
addx 5
addx 7
addx 2
addx 5
addx -18
addx 28
addx -7
addx 2
addx 5
addx -28
addx 34
addx -3
noop
addx 3
addx -38
addx 10
addx -3
addx 29
addx -28
addx 2
noop
noop
noop
addx 5
noop
addx 3
addx 2
addx 7
noop
addx -2
addx 5
addx 2
noop
addx 1
addx 5
noop
noop
addx -25
noop
noop

134
2022/10/src/main.rs Normal file
View File

@ -0,0 +1,134 @@
use std::str::FromStr;
fn main() {
let input_file = common::parse_args_input_file(&mut std::env::args());
let contents = std::fs::read_to_string(input_file).expect("Failed to read input file");
let program = parse_program(&contents);
let (result, screen) = run_program(&program);
println!("[PART 1]: {}", result);
println!("");
for (index, value) in screen.iter().enumerate() {
let c = match value {
true => '#',
false => '.',
};
print!("{}", c);
if (index + 1) % 40 == 0 {
println!("");
}
}
}
#[derive(Clone)]
enum Op {
Noop,
Addx,
}
#[derive(Clone)]
struct Instruction {
op: Op,
cycles: i32,
arg: Option<i32>,
}
impl Instruction {
fn from_str(s: &str) -> Instruction {
let mut i = Instruction {
op: Op::Noop,
cycles: 1,
arg: None,
};
let words: std::vec::Vec<&str> = s.split(' ').collect();
if words.len() > 1 {
match words[0] {
"addx" => {
i.cycles = 2;
i.op = Op::Addx;
i.arg = Some(i32::from_str(words[1]).expect("Failed to parse int"));
},
_ => {
unreachable!();
},
};
}
return i;
}
}
fn parse_program(lines: &String) -> std::vec::Vec<Instruction> {
let mut program = std::vec::Vec::<Instruction>::new();
for line in lines.lines() {
if line.eq("") {
continue;
}
program.push(Instruction::from_str(line));
}
return program;
}
fn run_program(program: &std::vec::Vec<Instruction>) -> (i32, std::vec::Vec<bool>) {
let mut p = program.clone();
p.reverse();
let mut cycles = 0;
let mut reg_x = 1;
let mut result = 0;
let mut screen = std::vec::Vec::<bool>::new();
loop {
cycles += 1;
let mut instruction = p.last_mut();
if instruction.is_none() {
break;
}
instruction.as_mut().unwrap().cycles -= 1;
// Check signal strength
// println!("Cycle {}: register x {}", cycles, reg_x);
if (cycles - 20) % 40 == 0 {
let v = cycles * reg_x;
println!("Cycle {}: signal value {} changes result from {} to {}",
cycles, v, result, result + v);
result += v;
}
// Draw to screen
// @BUG - The screen (or the screen printing routine) has an off by one error
// Theoutput of the program was
// ###...##..###..#..#.####.#..#.####...##.
// ...#.#..#.#..#.#.#..#....#.#..#.......#.
// ...#.#..#.#..#.##...###..##...###.....#.
// ###..####.###..#.#..#....#.#..#.......#.
// .....#..#.#....#.#..#....#.#..#....#..#.
// .....#..#.#....#..#.#....#..#.####..##..
// The screen is supposed to display 8 characters, and the first has a column
// cut off; however, given the shape I was able to deduce the answer for the
// purpose of the puzzle and I don't feel like fixing the routines at this
// time.
if reg_x < 0 {
// Sprite is not visible, draw dark
screen.push(false);
}
else {
// Sprite may be visible
let sprite_pos = reg_x % 40;
let pixel_number = (screen.len() % 40) as i32;
if pixel_number >= sprite_pos - 1 && pixel_number <= sprite_pos + 1 {
screen.push(true);
}
else {
screen.push(false);
}
}
if instruction.as_ref().unwrap().cycles <= 0 {
match instruction.as_ref().unwrap().op {
Op::Noop => {},
Op::Addx => {
reg_x += instruction.unwrap().arg.unwrap();
},
};
_ = p.pop();
}
}
return (result, screen);
}

146
2022/10/test_input 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