From 59139d65d62b852a8b68c8874ce847ce8b1ee4f4 Mon Sep 17 00:00:00 2001 From: Kienan Stewart Date: Sat, 10 Dec 2022 09:12:22 -0500 Subject: [PATCH] AOC 2022 day 10 --- 2022/10/Cargo.lock | 14 +++++ 2022/10/Cargo.toml | 9 +++ 2022/10/input | 144 +++++++++++++++++++++++++++++++++++++++++++ 2022/10/src/main.rs | 134 ++++++++++++++++++++++++++++++++++++++++ 2022/10/test_input | 146 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 447 insertions(+) create mode 100644 2022/10/Cargo.lock create mode 100644 2022/10/Cargo.toml create mode 100644 2022/10/input create mode 100644 2022/10/src/main.rs create mode 100644 2022/10/test_input diff --git a/2022/10/Cargo.lock b/2022/10/Cargo.lock new file mode 100644 index 0000000..fc44da5 --- /dev/null +++ b/2022/10/Cargo.lock @@ -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", +] diff --git a/2022/10/Cargo.toml b/2022/10/Cargo.toml new file mode 100644 index 0000000..eebcc4e --- /dev/null +++ b/2022/10/Cargo.toml @@ -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" } diff --git a/2022/10/input b/2022/10/input new file mode 100644 index 0000000..3d9acd7 --- /dev/null +++ b/2022/10/input @@ -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 diff --git a/2022/10/src/main.rs b/2022/10/src/main.rs new file mode 100644 index 0000000..a38f132 --- /dev/null +++ b/2022/10/src/main.rs @@ -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, +} + +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 { + let mut program = std::vec::Vec::::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) -> (i32, std::vec::Vec) { + 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::::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); +} diff --git a/2022/10/test_input b/2022/10/test_input new file mode 100644 index 0000000..94cd0a8 --- /dev/null +++ b/2022/10/test_input @@ -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