-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
250 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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::<i32>().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::<Vec<Instruction>>(); | ||
|
||
let connt_cycles: Vec<u32> = vec![20, 60, 100, 140, 180, 220]; | ||
let mut sum: Vec<i32> = 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::<i32>()); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters