Skip to content

Commit

Permalink
advent_of_code 2022-10: part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
msyfls123 committed Sep 19, 2023
1 parent 847245c commit b9956f7
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 1 deletion.
6 changes: 5 additions & 1 deletion advent_of_code/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,8 @@ path = "src/aoc2022/8.rs"

[[bin]]
name = "2022-9"
path = "src/aoc2022/9.rs"
path = "src/aoc2022/9.rs"

[[bin]]
name = "2022-10"
path = "src/aoc2022/10.rs"
85 changes: 85 additions & 0 deletions advent_of_code/src/aoc2022/10.rs
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);
}
141 changes: 141 additions & 0 deletions advent_of_code/src/aoc2022/data/10.txt
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

0 comments on commit b9956f7

Please sign in to comment.