Progress on day 10

This commit is contained in:
Timothy Warren 2022-12-13 18:32:44 -05:00
parent 68a9f5896e
commit 4ad9f14894

View File

@ -1,5 +1,80 @@
use std::collections::HashMap;
#[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 }
}
fn noop(&self) -> (usize, Option<isize>) {
(1, None)
}
fn addx(&mut self, i: isize) -> (usize, Option<isize>) {
self.x += i;
(2, Some(self.x))
}
pub fn run(&mut self, command: Instruction) -> (usize, Option<isize>) {
match command {
Addx(i) => self.addx(i),
Noop => self.noop(),
}
}
}
// -----------------------------------------------------------------------------
#[derive(Debug)]
struct CycleCounter {
cpu: CPU,
log: HashMap<usize, isize>,
cycle: usize,
}
impl CycleCounter {
pub fn new() -> Self {
Self {
cpu: CPU::new(),
log: HashMap::new(),
cycle: 1,
}
}
}
// -----------------------------------------------------------------------------
fn main() { fn main() {
let file_str = include_str!("input.txt"); let file_str = include_str!("input.txt");
let instructions: Vec<Instruction> = file_str.lines().map(Instruction::from_line).collect();
} }
#[cfg(test)] #[cfg(test)]