-
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
3 changed files
with
231 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,85 @@ | ||
use advent_of_code::get_str_array_from_file; | ||
|
||
const PERIODS: [usize; 6] = [ | ||
20, | ||
60, | ||
100, | ||
140, | ||
180, | ||
220 | ||
]; | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
enum Instruction { | ||
Addx(isize), | ||
Noop | ||
} | ||
|
||
impl Instruction { | ||
fn from(text: &str) -> Self { | ||
match &text[0..4] { | ||
"noop" => Self::Noop, | ||
"addx" => { | ||
let value = text[5..].parse::<isize>().unwrap(); | ||
Self::Addx(value) | ||
}, | ||
_ => panic!("can not convert to Instruction") | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_instruction_from_text() { | ||
assert_eq!(Instruction::Addx(5), Instruction::from("addx 5")); | ||
assert_eq!(Instruction::Addx(-6), Instruction::from("addx -6")); | ||
assert_eq!(Instruction::Noop, Instruction::from("noop")); | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
struct Register { | ||
value: isize, | ||
cycles: usize, | ||
} | ||
|
||
impl Register { | ||
fn run(&mut self, instruction: &Instruction) { | ||
match instruction { | ||
Instruction::Noop => { | ||
self.cycles += 1; | ||
}, | ||
Instruction::Addx(x) => { | ||
self.cycles += 2; | ||
self.value += x; | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
let data = get_str_array_from_file(&vec!{"aoc2022", "data", "10.txt"}); | ||
let instructions: Vec<Instruction> = data.iter().map(|text| Instruction::from(text)).collect(); | ||
|
||
let mut register = Register { | ||
value: 1, | ||
cycles: 0, | ||
}; | ||
let mut sum = 0; | ||
let mut index = 0; | ||
while register.cycles < PERIODS[5] { | ||
let prev_register = register.to_owned(); | ||
let instruction = &instructions[index % instructions.len()]; | ||
register.run(&instruction); | ||
|
||
for period in PERIODS { | ||
let prev_period = period - 1; | ||
if register.cycles == prev_period { | ||
sum += register.value * (period as isize) | ||
} else if register.cycles > prev_period && prev_register.cycles < prev_period { | ||
sum += prev_register.value * (period as isize) | ||
} | ||
} | ||
|
||
index += 1; | ||
} | ||
println!("Part 1: {}", sum); | ||
} |
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,141 @@ | ||
addx 2 | ||
addx 3 | ||
addx 3 | ||
addx -2 | ||
addx 4 | ||
noop | ||
addx 1 | ||
addx 4 | ||
addx 1 | ||
noop | ||
addx 4 | ||
addx 1 | ||
noop | ||
addx 2 | ||
addx 5 | ||
addx -28 | ||
addx 30 | ||
noop | ||
addx 5 | ||
addx 1 | ||
noop | ||
addx -38 | ||
noop | ||
noop | ||
noop | ||
noop | ||
addx 5 | ||
addx 5 | ||
addx 3 | ||
addx 2 | ||
addx -2 | ||
addx 2 | ||
noop | ||
noop | ||
addx -2 | ||
addx 12 | ||
noop | ||
addx 2 | ||
addx 3 | ||
noop | ||
addx 2 | ||
addx -31 | ||
addx 32 | ||
addx 7 | ||
noop | ||
addx -2 | ||
addx -37 | ||
addx 1 | ||
addx 5 | ||
addx 1 | ||
noop | ||
addx 31 | ||
addx -25 | ||
addx -10 | ||
addx 13 | ||
noop | ||
noop | ||
addx 18 | ||
addx -11 | ||
addx 3 | ||
noop | ||
noop | ||
addx 1 | ||
addx 4 | ||
addx -32 | ||
addx 15 | ||
addx 24 | ||
addx -2 | ||
noop | ||
addx -37 | ||
noop | ||
noop | ||
noop | ||
addx 5 | ||
addx 5 | ||
addx 21 | ||
addx -20 | ||
noop | ||
addx 6 | ||
addx 19 | ||
addx -5 | ||
addx -8 | ||
addx -22 | ||
addx 26 | ||
addx -22 | ||
addx 23 | ||
addx 2 | ||
noop | ||
noop | ||
noop | ||
addx 8 | ||
addx -10 | ||
addx -27 | ||
addx 33 | ||
addx -27 | ||
noop | ||
addx 34 | ||
addx -33 | ||
addx 2 | ||
addx 19 | ||
addx -12 | ||
addx 11 | ||
addx -20 | ||
addx 12 | ||
addx 18 | ||
addx -11 | ||
addx -14 | ||
addx 15 | ||
addx 2 | ||
noop | ||
addx 3 | ||
addx 2 | ||
noop | ||
noop | ||
noop | ||
addx -33 | ||
noop | ||
addx 1 | ||
addx 2 | ||
noop | ||
addx 3 | ||
addx 4 | ||
noop | ||
addx 1 | ||
addx 2 | ||
noop | ||
noop | ||
addx 7 | ||
addx 1 | ||
noop | ||
addx 4 | ||
addx -17 | ||
addx 18 | ||
addx 5 | ||
addx -1 | ||
addx 5 | ||
addx 1 | ||
noop | ||
noop | ||
noop | ||
noop |