diff --git a/.vscode/launch.json b/.vscode/launch.json index 07f6206..631e428 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -101,6 +101,20 @@ }, "args": [], "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug executable 'rust_2022_10'", + "cargo": { + "args": ["build", "--bin=rust_2022_10", "--package=rust_2022_10"], + "filter": { + "name": "rust_2022_10", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" } ] } diff --git a/2022/day_10/input.txt b/2022/day_10/input.txt new file mode 100644 index 0000000..ee52650 --- /dev/null +++ b/2022/day_10/input.txt @@ -0,0 +1,142 @@ +addx 1 +addx 5 +noop +addx -1 +noop +noop +addx 6 +addx 15 +addx -9 +noop +addx -1 +addx 4 +addx 2 +addx -22 +addx 27 +addx -1 +addx 4 +noop +addx 1 +addx 2 +noop +noop +noop +noop +addx 1 +addx -33 +addx 2 +addx 5 +addx 2 +addx 3 +addx -2 +addx 7 +noop +addx -2 +addx -8 +addx 15 +addx 5 +noop +noop +addx -2 +addx 2 +noop +noop +addx 7 +addx -14 +noop +addx -2 +addx -17 +addx 5 +addx -4 +noop +addx 23 +addx -18 +noop +noop +noop +addx 28 +addx -18 +addx 4 +noop +noop +addx -5 +addx 1 +addx 10 +addx 2 +noop +noop +addx -30 +addx 33 +addx -32 +noop +noop +addx -2 +addx 6 +addx -2 +addx 4 +addx 3 +addx 19 +addx 10 +addx -5 +addx -16 +addx 3 +addx -2 +addx 17 +addx -19 +addx 11 +addx 2 +addx 9 +noop +addx -4 +addx -6 +addx -7 +addx -24 +noop +addx 7 +addx -2 +addx 5 +addx 2 +addx 3 +addx -2 +addx 2 +addx 5 +addx 2 +addx 7 +addx -2 +noop +addx 3 +addx -2 +addx 2 +addx 7 +noop +addx -2 +addx -34 +addx 1 +addx 1 +noop +noop +noop +addx 5 +noop +noop +addx 5 +addx -1 +noop +addx 6 +addx -1 +noop +addx 4 +addx 3 +addx 4 +addx -1 +addx 5 +noop +addx 5 +noop +noop +noop +noop +noop +addx 1 +noop +noop \ No newline at end of file diff --git a/2022/day_10/rust/Cargo.toml b/2022/day_10/rust/Cargo.toml new file mode 100644 index 0000000..9fb1030 --- /dev/null +++ b/2022/day_10/rust/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "rust_2022_10" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2022/day_10/rust/src/main.rs b/2022/day_10/rust/src/main.rs new file mode 100644 index 0000000..6231e8c --- /dev/null +++ b/2022/day_10/rust/src/main.rs @@ -0,0 +1,80 @@ +use std::fmt::Debug; + +#[derive(Debug)] +enum Instruction { + Addx(i32), + Noop, +} + +impl From<&str> for Instruction { + fn from(string: &str) -> Self { + match string.split_once(" ") { + Some((_, n)) => Instruction::Addx(n.parse::().unwrap()), + None => Instruction::Noop, + } + } +} + +impl Instruction { + pub fn cycles(&self) -> u32 { + match self { + Instruction::Noop => 1, + Instruction::Addx(_) => 2, + } + } +} + +struct Computer<'a> { + x: i32, + cycle: u32, + on_cycle: Option<&'a mut dyn FnMut(u32, i32)>, +} + +impl<'a> Computer<'a> { + fn new(on_cycle: Option<&'a mut dyn FnMut(u32, i32)>) -> Self { + Self { + cycle: 0, + x: 1, + on_cycle, + } + } + + fn apply_instruction(&mut self, instruction: &Instruction) { + for _ in 0..instruction.cycles() { + self.cycle += 1; + if self.on_cycle.is_some() { + self.on_cycle.as_mut().unwrap()(self.cycle, self.x); + } + } + + match *instruction { + Instruction::Addx(n) => self.x += n, + Instruction::Noop => return, + } + } +} + +fn main() { + let data = include_str!("../../input.txt"); + + let instructions = data + .lines() + .map(|line| Instruction::from(line)) + .collect::>(); + + let connt_cycles: Vec = vec![20, 60, 100, 140, 180, 220]; + let mut sum: Vec = Vec::new(); + let mut on_cycle = |cycle: u32, x: i32| { + if let Some(index) = connt_cycles.iter().position(|&c| c == cycle) { + sum.push(x * connt_cycles[index] as i32); + } + }; + + let mut computer = Computer::new(Some(&mut on_cycle)); + + instructions.iter().for_each(|instruction| { + computer.apply_instruction(instruction); + }); + + println!("{}", sum.iter().sum::()); +} diff --git a/Cargo.lock b/Cargo.lock index 1f828d9..829c082 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -37,3 +37,7 @@ version = "0.1.0" [[package]] name = "rust_2022_09" version = "0.1.0" + +[[package]] +name = "rust_2022_10" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index aee68cb..4f3981c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ members = [ "2022/day_6/rust", "2022/day_7/rust", "2022/day_8/rust", - "2022/day_9/rust" + "2022/day_9/rust", + "2022/day_10/rust" ] exclude = [ "2022" ] \ No newline at end of file