Skip to content

Commit

Permalink
2022_10: rust solution(part 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
MellKam committed Dec 17, 2022
1 parent 4dca86e commit 92e2c2a
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 1 deletion.
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
}
]
}
142 changes: 142 additions & 0 deletions 2022/day_10/input.txt
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
8 changes: 8 additions & 0 deletions 2022/day_10/rust/Cargo.toml
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]
80 changes: 80 additions & 0 deletions 2022/day_10/rust/src/main.rs
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>());
}
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]

0 comments on commit 92e2c2a

Please sign in to comment.