2022-12-13 18:32:44 -05:00
|
|
|
#[derive(Debug)]
|
|
|
|
enum Instruction {
|
|
|
|
Addx(isize),
|
|
|
|
Noop,
|
|
|
|
}
|
|
|
|
use Instruction::*;
|
|
|
|
|
|
|
|
impl Instruction {
|
|
|
|
pub fn from_line(line: &str) -> Self {
|
|
|
|
let parts: Vec<&str> = line.split_ascii_whitespace().collect();
|
|
|
|
|
|
|
|
if parts.len() == 2 && parts[0] == "addx" {
|
|
|
|
let value = parts[1].parse::<isize>().unwrap();
|
|
|
|
|
|
|
|
return Addx(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Noop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct CPU {
|
|
|
|
x: isize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CPU {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
CPU { x: 1 }
|
|
|
|
}
|
|
|
|
|
2022-12-13 21:22:56 -05:00
|
|
|
fn noop(&self) -> usize {
|
|
|
|
1
|
2022-12-13 18:32:44 -05:00
|
|
|
}
|
|
|
|
|
2022-12-13 21:22:56 -05:00
|
|
|
fn addx(&mut self, i: isize) -> usize {
|
2022-12-13 18:32:44 -05:00
|
|
|
self.x += i;
|
|
|
|
|
2022-12-13 21:22:56 -05:00
|
|
|
2
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_x(&self) -> isize {
|
|
|
|
self.x
|
2022-12-13 18:32:44 -05:00
|
|
|
}
|
|
|
|
|
2022-12-13 21:22:56 -05:00
|
|
|
pub fn run(&mut self, command: Instruction) -> usize {
|
2022-12-13 18:32:44 -05:00
|
|
|
match command {
|
|
|
|
Addx(i) => self.addx(i),
|
|
|
|
Noop => self.noop(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct CycleCounter {
|
|
|
|
cpu: CPU,
|
2022-12-13 21:22:56 -05:00
|
|
|
log: Vec<isize>,
|
2022-12-13 18:32:44 -05:00
|
|
|
cycle: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CycleCounter {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
cpu: CPU::new(),
|
2022-12-13 21:22:56 -05:00
|
|
|
log: vec![1, 1],
|
2022-12-13 18:32:44 -05:00
|
|
|
cycle: 1,
|
|
|
|
}
|
|
|
|
}
|
2022-12-13 21:22:56 -05:00
|
|
|
|
|
|
|
fn run_line(&mut self, line: &str) {
|
|
|
|
let x = self.cpu.get_x();
|
|
|
|
let cycles = self.cpu.run(Instruction::from_line(line));
|
|
|
|
|
|
|
|
for _ in 0..(cycles - 1) {
|
|
|
|
self.cycle += 1;
|
|
|
|
self.log.push(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.cycle += 1;
|
|
|
|
self.log.push(self.cpu.get_x());
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_signal_strength(&self, cycle: usize) -> usize {
|
|
|
|
let x = self.log.get(cycle).unwrap();
|
|
|
|
|
|
|
|
(*x as usize) * cycle
|
|
|
|
}
|
2022-12-13 18:32:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2022-12-13 17:19:53 -05:00
|
|
|
fn main() {
|
|
|
|
let file_str = include_str!("input.txt");
|
2022-12-13 21:22:56 -05:00
|
|
|
let mut cc = CycleCounter::new();
|
|
|
|
|
|
|
|
file_str.lines().for_each(|line| cc.run_line(line));
|
|
|
|
|
|
|
|
let sum: usize = [20usize, 60, 100, 140, 180, 220]
|
|
|
|
.into_iter()
|
|
|
|
.map(|n| cc.get_signal_strength(n))
|
|
|
|
.sum();
|
|
|
|
|
|
|
|
println!("Part 1: sum of signal strength: {}", sum);
|
2022-12-13 17:19:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2022-12-13 21:22:56 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_signal_strength() {
|
|
|
|
let file_str = include_str!("test-input.txt");
|
|
|
|
let mut cc = CycleCounter::new();
|
|
|
|
|
|
|
|
file_str.lines().for_each(|line| cc.run_line(line));
|
|
|
|
|
|
|
|
assert_eq!(cc.get_signal_strength(20), 420);
|
|
|
|
assert_eq!(cc.get_signal_strength(60), 1140);
|
|
|
|
assert_eq!(cc.get_signal_strength(100), 1800);
|
|
|
|
assert_eq!(cc.get_signal_strength(140), 2940);
|
|
|
|
assert_eq!(cc.get_signal_strength(180), 2880);
|
|
|
|
assert_eq!(cc.get_signal_strength(220), 3960);
|
|
|
|
}
|
2022-12-13 17:19:53 -05:00
|
|
|
}
|