Move Day 10 to its own crate

This commit is contained in:
Eryn Wells 2022-12-19 09:45:52 -08:00
parent b423f76ddc
commit 098b10c431
6 changed files with 144 additions and 0 deletions

7
2022/day10/Cargo.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day10"
version = "0.1.0"

8
2022/day10/Cargo.toml Normal file
View file

@ -0,0 +1,8 @@
[package]
name = "day10"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

145
2022/day10/input.txt Normal file
View file

@ -0,0 +1,145 @@
addx 1
noop
noop
addx 4
addx 5
addx -2
addx 19
addx -12
addx 3
addx -2
addx 4
noop
noop
noop
addx 3
addx -8
addx 15
addx 1
noop
noop
addx 6
addx -1
noop
addx -38
noop
addx 10
addx -5
noop
addx 3
addx 2
addx 7
noop
noop
addx 3
noop
addx 2
addx 3
addx -2
addx 2
addx 7
noop
noop
addx 9
noop
addx -12
noop
addx 11
addx -38
noop
noop
noop
addx 5
addx 5
noop
noop
noop
addx 3
addx -12
addx 14
noop
addx 1
addx 3
addx 1
addx 5
addx 4
addx 1
noop
noop
noop
noop
noop
addx -9
addx 17
addx -39
addx 38
addx -8
addx -26
addx 3
addx 4
addx 16
noop
addx -11
addx 3
noop
addx 2
addx 3
addx -2
addx 2
noop
addx 13
addx -8
noop
addx 7
addx -5
addx 8
addx -40
addx 16
addx -9
noop
addx -7
addx 8
addx 2
addx 7
noop
noop
addx -15
addx 16
addx 2
addx 5
addx 2
addx -20
addx 12
addx 11
addx 8
addx -1
addx 3
noop
addx -39
addx 2
noop
addx 5
noop
noop
noop
addx 4
addx 1
noop
noop
addx 2
addx 5
addx 2
addx 1
addx 4
addx -1
addx 2
noop
addx 2
noop
addx 8
noop
noop
noop
addx -10
noop
noop

146
2022/day10/long-example.txt Normal file
View file

@ -0,0 +1,146 @@
addx 15
addx -11
addx 6
addx -3
addx 5
addx -1
addx -8
addx 13
addx 4
noop
addx -1
addx 5
addx -1
addx 5
addx -1
addx 5
addx -1
addx 5
addx -1
addx -35
addx 1
addx 24
addx -19
addx 1
addx 16
addx -11
noop
noop
addx 21
addx -15
noop
noop
addx -3
addx 9
addx 1
addx -3
addx 8
addx 1
addx 5
noop
noop
noop
noop
noop
addx -36
noop
addx 1
addx 7
noop
noop
noop
addx 2
addx 6
noop
noop
noop
noop
noop
addx 1
noop
noop
addx 7
addx 1
noop
addx -13
addx 13
addx 7
noop
addx 1
addx -33
noop
noop
noop
addx 2
noop
noop
noop
addx 8
noop
addx -1
addx 2
addx 1
noop
addx 17
addx -9
addx 1
addx 1
addx -3
addx 11
noop
noop
addx 1
noop
addx 1
noop
noop
addx -13
addx -19
addx 1
addx 3
addx 26
addx -30
addx 12
addx -1
addx 3
addx 1
noop
noop
noop
addx -9
addx 18
addx 1
addx 2
noop
noop
addx 9
noop
noop
noop
addx -1
addx 2
addx -37
addx 1
addx 3
noop
addx 15
addx -21
addx 22
addx -6
addx 1
noop
addx 2
addx 1
noop
addx -10
noop
noop
addx 20
addx 1
addx 2
addx 2
addx -6
addx -11
noop
noop
noop

View file

@ -0,0 +1,3 @@
noop
addx 3
addx -5

129
2022/day10/src/main.rs Normal file
View file

@ -0,0 +1,129 @@
use std::{env, fs};
#[derive(Debug)]
enum Instruction {
Noop,
AddX(i32),
}
impl Instruction {
fn number_of_cycles(&self) -> u32 {
match self {
Instruction::Noop => 1,
Instruction::AddX(_) => 2,
}
}
}
struct CPU<'a> {
instructions: Box<dyn Iterator<Item = Instruction> + 'a>,
current_instruction: Option<Instruction>,
cycle: u32,
x: i32,
}
impl<'a> CPU<'a> {
fn new<T: Iterator<Item = Instruction> + 'a>(instructions: T) -> CPU<'a> {
CPU {
instructions: Box::new(instructions),
current_instruction: None,
cycle: 0,
x: 1,
}
}
}
impl Iterator for CPU<'_> {
type Item = State;
fn next(&mut self) -> Option<Self::Item> {
if self.current_instruction.is_none() {
self.current_instruction = self.instructions.next();
self.cycle = 0;
}
if let Some(current_instruction) = &self.current_instruction {
let state = Some(State::from_cpu(&self));
self.cycle += 1;
if self.cycle >= current_instruction.number_of_cycles() {
match current_instruction {
Instruction::Noop => {}
Instruction::AddX(value) => {
self.x += value;
}
}
self.current_instruction = self.instructions.next();
self.cycle = 0;
}
state
} else {
None
}
}
}
#[derive(Clone, Debug)]
struct State {
x: i32,
}
impl State {
fn from_cpu(cpu: &CPU) -> State {
State { x: cpu.x }
}
}
fn main() {
let args: Vec<String> = env::args().collect();
let filename = args.get(1).expect("Missing filename argument");
let file_contents = fs::read_to_string(&filename).expect("Unable to read file");
let lines = file_contents.lines();
let cpu = CPU::new(lines.map(|line| -> Instruction {
if line.starts_with("addx") {
let value = i32::from_str_radix(line.split(" ").collect::<Vec<&str>>()[1], 10).unwrap();
Instruction::AddX(value)
} else {
Instruction::Noop
}
}));
let mut number_of_cycles = 0;
let mut signal_strengths = 0;
let mut cycles: Vec<State> = vec![];
for (i, cycle) in cpu.enumerate() {
cycles.push(cycle.clone());
let x = cycle.x;
let cycle_number = i as i32 + 1;
if cycle_number % 40 == 20 {
signal_strengths += x * cycle_number;
}
number_of_cycles = i;
let i = i as i32;
if i > 0 && i % 40 == 0 {
println!("");
}
let horizontal_beam_position = i % 40;
if horizontal_beam_position == (x - 1)
|| horizontal_beam_position == x
|| horizontal_beam_position == (x + 1)
{
print!("#");
} else {
print!(".");
}
}
println!("");
println!("There were {} instructions", number_of_cycles + 1);
println!("Part 1: sum of signal strengths: {}", signal_strengths);
}