porting over AOC from previous years to a monorepo.
This commit is contained in:
commit
84c4cf9991
194 changed files with 30104 additions and 0 deletions
10
2022/days/10/Cargo.toml
Normal file
10
2022/days/10/Cargo.toml
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
[package]
|
||||
name = "day10"
|
||||
authors.workspace = true
|
||||
description.workspace = true
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
|
||||
[dependencies]
|
||||
aoc_libs.workspace = true
|
||||
nom.workspace = true
|
||||
118
2022/days/10/src/crt.rs
Normal file
118
2022/days/10/src/crt.rs
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
use std::fmt::Display;
|
||||
|
||||
use crate::machine::Machine;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct Crt {
|
||||
pub screen: [[bool; 40]; 6],
|
||||
}
|
||||
|
||||
impl Crt {
|
||||
pub fn draw_pixel(&mut self, sprite_pos: i32, cycle: usize) {
|
||||
let x_coord = (cycle - 1) % self.screen[0].len();
|
||||
if sprite_pos.abs_diff(x_coord as i32) <= 1 {
|
||||
self[cycle] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Crt {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
screen: [[false; 40]; 6],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Crt {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
for line in self.screen {
|
||||
for char in line {
|
||||
if char {
|
||||
write!(f, "#")?;
|
||||
} else {
|
||||
write!(f, ".")?;
|
||||
}
|
||||
}
|
||||
writeln!(f)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Index<usize> for Crt {
|
||||
type Output = bool;
|
||||
|
||||
fn index(&self, index: usize) -> &Self::Output {
|
||||
let index = index - 1;
|
||||
let x = index % self.screen[0].len();
|
||||
let y = index / self.screen[0].len();
|
||||
&self.screen[y][x]
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::IndexMut<usize> for Crt {
|
||||
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
|
||||
let index = index - 1;
|
||||
let x = index % self.screen[0].len();
|
||||
let y = index / self.screen[0].len();
|
||||
&mut self.screen[y][x]
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_index() {
|
||||
let mut input = Crt::default();
|
||||
input[1] = true;
|
||||
input[40] = true;
|
||||
input[41] = true;
|
||||
println!("{}", input);
|
||||
assert_eq!(
|
||||
input,
|
||||
Crt {
|
||||
screen: [
|
||||
[
|
||||
true, false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, true
|
||||
],
|
||||
[
|
||||
true, false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false
|
||||
],
|
||||
[
|
||||
false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false, false
|
||||
],
|
||||
[
|
||||
false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false, false
|
||||
],
|
||||
[
|
||||
false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false, false
|
||||
],
|
||||
[
|
||||
false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false, false
|
||||
]
|
||||
]
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
139
2022/days/10/src/input.txt
Normal file
139
2022/days/10/src/input.txt
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
noop
|
||||
addx 12
|
||||
addx -5
|
||||
addx -1
|
||||
noop
|
||||
addx 4
|
||||
noop
|
||||
addx 1
|
||||
addx 4
|
||||
noop
|
||||
addx 13
|
||||
addx -8
|
||||
noop
|
||||
addx -19
|
||||
addx 24
|
||||
addx 1
|
||||
noop
|
||||
addx 4
|
||||
noop
|
||||
addx 1
|
||||
addx 5
|
||||
addx -1
|
||||
addx -37
|
||||
addx 16
|
||||
addx -13
|
||||
addx 18
|
||||
addx -11
|
||||
addx 2
|
||||
addx 23
|
||||
noop
|
||||
addx -18
|
||||
addx 9
|
||||
addx -8
|
||||
addx 2
|
||||
addx 5
|
||||
addx 2
|
||||
addx -21
|
||||
addx 26
|
||||
noop
|
||||
addx -15
|
||||
addx 20
|
||||
noop
|
||||
addx 3
|
||||
noop
|
||||
addx -38
|
||||
addx 3
|
||||
noop
|
||||
addx 26
|
||||
addx -4
|
||||
addx -19
|
||||
addx 3
|
||||
addx 1
|
||||
addx 5
|
||||
addx 3
|
||||
noop
|
||||
addx 2
|
||||
addx 3
|
||||
noop
|
||||
addx 2
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 3
|
||||
noop
|
||||
addx -30
|
||||
addx -4
|
||||
addx 1
|
||||
addx 18
|
||||
addx -8
|
||||
addx -4
|
||||
addx 2
|
||||
noop
|
||||
addx 7
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
addx -2
|
||||
addx -20
|
||||
addx 27
|
||||
addx -20
|
||||
addx 25
|
||||
addx -2
|
||||
addx -35
|
||||
noop
|
||||
noop
|
||||
addx 4
|
||||
addx 3
|
||||
addx -2
|
||||
addx 5
|
||||
addx 2
|
||||
addx -11
|
||||
addx 1
|
||||
addx 13
|
||||
addx 2
|
||||
addx 5
|
||||
addx 6
|
||||
addx -1
|
||||
addx -2
|
||||
noop
|
||||
addx 7
|
||||
addx -2
|
||||
addx 6
|
||||
addx 1
|
||||
addx -21
|
||||
addx 22
|
||||
addx -38
|
||||
addx 5
|
||||
addx 3
|
||||
addx -1
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
addx 1
|
||||
addx 4
|
||||
addx 3
|
||||
addx -2
|
||||
addx 2
|
||||
noop
|
||||
addx 7
|
||||
addx -1
|
||||
addx 2
|
||||
addx 4
|
||||
addx -10
|
||||
addx -19
|
||||
addx 35
|
||||
addx -1
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
455
2022/days/10/src/machine.rs
Normal file
455
2022/days/10/src/machine.rs
Normal file
|
|
@ -0,0 +1,455 @@
|
|||
use std::{collections::VecDeque, fmt};
|
||||
|
||||
use crate::crt::Crt;
|
||||
use crate::parse::*;
|
||||
|
||||
pub struct Machine {
|
||||
instructions: VecDeque<Instruction>,
|
||||
current_instruction: Option<(Instruction, usize)>,
|
||||
cycle: usize,
|
||||
x: i32,
|
||||
pub crt: Crt,
|
||||
}
|
||||
|
||||
impl Machine {
|
||||
pub fn load_program(instructions: VecDeque<Instruction>) -> Self {
|
||||
let mut res = Machine {
|
||||
instructions,
|
||||
current_instruction: None,
|
||||
cycle: 0,
|
||||
x: 1,
|
||||
crt: Crt::default(),
|
||||
};
|
||||
res.fetch_next_instruction();
|
||||
res
|
||||
}
|
||||
|
||||
pub fn step(&mut self) -> (bool, i32) {
|
||||
if self.current_instruction.is_none() {
|
||||
return (false, self.x);
|
||||
}
|
||||
|
||||
//we return x as it was at the beginning of the cycle.
|
||||
let x = self.x;
|
||||
let (instruction, cycles_left) = self.current_instruction.as_mut().unwrap();
|
||||
*cycles_left -= 1;
|
||||
if *cycles_left == 0 {
|
||||
if let Instruction::Addx(i) = instruction {
|
||||
self.x += *i
|
||||
}
|
||||
|
||||
self.fetch_next_instruction();
|
||||
};
|
||||
|
||||
self.cycle += 1;
|
||||
self.crt.draw_pixel(x, self.cycle);
|
||||
(true, x)
|
||||
}
|
||||
|
||||
fn fetch_next_instruction(&mut self) {
|
||||
self.current_instruction = self.instructions.pop_front().map(|i| (i, i.cycles()));
|
||||
}
|
||||
|
||||
//returns the signal strength during the middle of the 20th cycle.
|
||||
pub fn step20(&mut self) -> i32 {
|
||||
for _ in 0..19 {
|
||||
if !self.step().0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
self.step().1 * self.cycle as i32
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Machine {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"cycle={} x={} current instruction: {:?} ({} instructions left)",
|
||||
self.cycle,
|
||||
self.x,
|
||||
self.current_instruction,
|
||||
self.instructions.len()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_screen() {
|
||||
let input = vec![
|
||||
Instruction::Addx(15),
|
||||
Instruction::Addx(-11),
|
||||
Instruction::Addx(6),
|
||||
Instruction::Addx(-3),
|
||||
Instruction::Addx(5),
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(-8),
|
||||
Instruction::Addx(13),
|
||||
Instruction::Addx(4),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(5),
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(5),
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(5),
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(5),
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(-35),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(24),
|
||||
Instruction::Addx(-19),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(16),
|
||||
Instruction::Addx(-11),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(21),
|
||||
Instruction::Addx(-15),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-3),
|
||||
Instruction::Addx(9),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(-3),
|
||||
Instruction::Addx(8),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(5),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-36),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(7),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(2),
|
||||
Instruction::Addx(6),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(7),
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-13),
|
||||
Instruction::Addx(13),
|
||||
Instruction::Addx(7),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(-33),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(2),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(8),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(2),
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(17),
|
||||
Instruction::Addx(-9),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(-3),
|
||||
Instruction::Addx(11),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-13),
|
||||
Instruction::Addx(-19),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(3),
|
||||
Instruction::Addx(26),
|
||||
Instruction::Addx(-30),
|
||||
Instruction::Addx(12),
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(3),
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-9),
|
||||
Instruction::Addx(18),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(2),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(9),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(2),
|
||||
Instruction::Addx(-37),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(3),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(15),
|
||||
Instruction::Addx(-21),
|
||||
Instruction::Addx(22),
|
||||
Instruction::Addx(-6),
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(2),
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-10),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(20),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(2),
|
||||
Instruction::Addx(2),
|
||||
Instruction::Addx(-6),
|
||||
Instruction::Addx(-11),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
];
|
||||
let mut machine = Machine::load_program(input.into());
|
||||
//step till we cant anymore.
|
||||
while machine.step().0 {}
|
||||
println!("{}", machine.crt);
|
||||
assert_eq!(
|
||||
machine.crt,
|
||||
Crt {
|
||||
screen: [
|
||||
[
|
||||
true, true, false, false, true, true, false, false, true, true, false,
|
||||
false, true, true, false, false, true, true, false, false, true, true,
|
||||
false, false, true, true, false, false, true, true, false, false, true,
|
||||
true, false, false, true, true, false, false
|
||||
],
|
||||
[
|
||||
true, true, true, false, false, false, true, true, true, false, false,
|
||||
false, true, true, true, false, false, false, true, true, true, false,
|
||||
false, false, true, true, true, false, false, false, true, true, true,
|
||||
false, false, false, true, true, true, false
|
||||
],
|
||||
[
|
||||
true, true, true, true, false, false, false, false, true, true, true, true,
|
||||
false, false, false, false, true, true, true, true, false, false, false,
|
||||
false, true, true, true, true, false, false, false, false, true, true,
|
||||
true, true, false, false, false, false
|
||||
],
|
||||
[
|
||||
true, true, true, true, true, false, false, false, false, false, true,
|
||||
true, true, true, true, false, false, false, false, false, true, true,
|
||||
true, true, true, false, false, false, false, false, true, true, true,
|
||||
true, true, false, false, false, false, false
|
||||
],
|
||||
[
|
||||
true, true, true, true, true, true, false, false, false, false, false,
|
||||
false, true, true, true, true, true, true, false, false, false, false,
|
||||
false, false, true, true, true, true, true, true, false, false, false,
|
||||
false, false, false, true, true, true, true
|
||||
],
|
||||
[
|
||||
true, true, true, true, true, true, true, false, false, false, false,
|
||||
false, false, false, true, true, true, true, true, true, true, false,
|
||||
false, false, false, false, false, false, true, true, true, true, true,
|
||||
true, true, false, false, false, false, false
|
||||
]
|
||||
]
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_machine() {
|
||||
let input = vec![
|
||||
Instruction::Addx(15),
|
||||
Instruction::Addx(-11),
|
||||
Instruction::Addx(6),
|
||||
Instruction::Addx(-3),
|
||||
Instruction::Addx(5),
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(-8),
|
||||
Instruction::Addx(13),
|
||||
Instruction::Addx(4),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(5),
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(5),
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(5),
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(5),
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(-35),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(24),
|
||||
Instruction::Addx(-19),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(16),
|
||||
Instruction::Addx(-11),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(21),
|
||||
Instruction::Addx(-15),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-3),
|
||||
Instruction::Addx(9),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(-3),
|
||||
Instruction::Addx(8),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(5),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-36),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(7),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(2),
|
||||
Instruction::Addx(6),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(7),
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-13),
|
||||
Instruction::Addx(13),
|
||||
Instruction::Addx(7),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(-33),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(2),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(8),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(2),
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(17),
|
||||
Instruction::Addx(-9),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(-3),
|
||||
Instruction::Addx(11),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-13),
|
||||
Instruction::Addx(-19),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(3),
|
||||
Instruction::Addx(26),
|
||||
Instruction::Addx(-30),
|
||||
Instruction::Addx(12),
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(3),
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-9),
|
||||
Instruction::Addx(18),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(2),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(9),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(2),
|
||||
Instruction::Addx(-37),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(3),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(15),
|
||||
Instruction::Addx(-21),
|
||||
Instruction::Addx(22),
|
||||
Instruction::Addx(-6),
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(2),
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-10),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(20),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(2),
|
||||
Instruction::Addx(2),
|
||||
Instruction::Addx(-6),
|
||||
Instruction::Addx(-11),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
];
|
||||
let mut machine = Machine::load_program(input.into());
|
||||
|
||||
//20th
|
||||
assert_eq!(machine.step20(), 420);
|
||||
|
||||
machine.step20();
|
||||
//60th
|
||||
assert_eq!(machine.step20(), 1140);
|
||||
|
||||
machine.step20();
|
||||
//100th
|
||||
assert_eq!(machine.step20(), 1800);
|
||||
|
||||
machine.step20();
|
||||
//140th
|
||||
assert_eq!(machine.step20(), 2940);
|
||||
|
||||
machine.step20();
|
||||
//180th
|
||||
assert_eq!(machine.step20(), 2880);
|
||||
|
||||
machine.step20();
|
||||
//220th
|
||||
assert_eq!(machine.step20(), 3960);
|
||||
}
|
||||
}
|
||||
16
2022/days/10/src/main.rs
Normal file
16
2022/days/10/src/main.rs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
mod part1;
|
||||
mod part2;
|
||||
mod parse;
|
||||
mod machine;
|
||||
mod crt;
|
||||
|
||||
fn main() {
|
||||
let input = include_str!("./input.txt");
|
||||
let structured_input = parse::parse(input);
|
||||
|
||||
println!("Part One");
|
||||
println!("Result: {}", part1::part1(structured_input.clone()));
|
||||
|
||||
println!("Part Two");
|
||||
println!("Result:\n{}", part2::part2(structured_input.clone()));
|
||||
}
|
||||
344
2022/days/10/src/parse.rs
Normal file
344
2022/days/10/src/parse.rs
Normal file
|
|
@ -0,0 +1,344 @@
|
|||
use std::collections::VecDeque;
|
||||
|
||||
use nom::{
|
||||
branch::alt,
|
||||
bytes::complete::tag,
|
||||
combinator::{all_consuming, map, value},
|
||||
sequence::preceded,
|
||||
Finish, IResult,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Instruction {
|
||||
NoOp,
|
||||
Addx(i32),
|
||||
}
|
||||
|
||||
impl Instruction {
|
||||
fn parse(i: &str) -> IResult<&str, Self> {
|
||||
let noop = tag("noop");
|
||||
let addx = preceded(tag("addx "), nom::character::complete::i32);
|
||||
alt((value(Self::NoOp, noop), map(addx, Self::Addx)))(i)
|
||||
}
|
||||
pub fn cycles(self) -> usize {
|
||||
match self {
|
||||
Self::NoOp => 1,
|
||||
Self::Addx(_) => 2,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse(input: &str) -> VecDeque<Instruction> {
|
||||
input
|
||||
.lines()
|
||||
.map(|l| all_consuming(Instruction::parse)(l).finish().unwrap().1)
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_parse() {
|
||||
let input = concat!(
|
||||
"addx 15\n",
|
||||
"addx -11\n",
|
||||
"addx 6\n",
|
||||
"addx -3\n",
|
||||
"addx 5\n",
|
||||
"addx -1\n",
|
||||
"addx -8\n",
|
||||
"addx 13\n",
|
||||
"addx 4\n",
|
||||
"noop\n",
|
||||
"addx -1\n",
|
||||
"addx 5\n",
|
||||
"addx -1\n",
|
||||
"addx 5\n",
|
||||
"addx -1\n",
|
||||
"addx 5\n",
|
||||
"addx -1\n",
|
||||
"addx 5\n",
|
||||
"addx -1\n",
|
||||
"addx -35\n",
|
||||
"addx 1\n",
|
||||
"addx 24\n",
|
||||
"addx -19\n",
|
||||
"addx 1\n",
|
||||
"addx 16\n",
|
||||
"addx -11\n",
|
||||
"noop\n",
|
||||
"noop\n",
|
||||
"addx 21\n",
|
||||
"addx -15\n",
|
||||
"noop\n",
|
||||
"noop\n",
|
||||
"addx -3\n",
|
||||
"addx 9\n",
|
||||
"addx 1\n",
|
||||
"addx -3\n",
|
||||
"addx 8\n",
|
||||
"addx 1\n",
|
||||
"addx 5\n",
|
||||
"noop\n",
|
||||
"noop\n",
|
||||
"noop\n",
|
||||
"noop\n",
|
||||
"noop\n",
|
||||
"addx -36\n",
|
||||
"noop\n",
|
||||
"addx 1\n",
|
||||
"addx 7\n",
|
||||
"noop\n",
|
||||
"noop\n",
|
||||
"noop\n",
|
||||
"addx 2\n",
|
||||
"addx 6\n",
|
||||
"noop\n",
|
||||
"noop\n",
|
||||
"noop\n",
|
||||
"noop\n",
|
||||
"noop\n",
|
||||
"addx 1\n",
|
||||
"noop\n",
|
||||
"noop\n",
|
||||
"addx 7\n",
|
||||
"addx 1\n",
|
||||
"noop\n",
|
||||
"addx -13\n",
|
||||
"addx 13\n",
|
||||
"addx 7\n",
|
||||
"noop\n",
|
||||
"addx 1\n",
|
||||
"addx -33\n",
|
||||
"noop\n",
|
||||
"noop\n",
|
||||
"noop\n",
|
||||
"addx 2\n",
|
||||
"noop\n",
|
||||
"noop\n",
|
||||
"noop\n",
|
||||
"addx 8\n",
|
||||
"noop\n",
|
||||
"addx -1\n",
|
||||
"addx 2\n",
|
||||
"addx 1\n",
|
||||
"noop\n",
|
||||
"addx 17\n",
|
||||
"addx -9\n",
|
||||
"addx 1\n",
|
||||
"addx 1\n",
|
||||
"addx -3\n",
|
||||
"addx 11\n",
|
||||
"noop\n",
|
||||
"noop\n",
|
||||
"addx 1\n",
|
||||
"noop\n",
|
||||
"addx 1\n",
|
||||
"noop\n",
|
||||
"noop\n",
|
||||
"addx -13\n",
|
||||
"addx -19\n",
|
||||
"addx 1\n",
|
||||
"addx 3\n",
|
||||
"addx 26\n",
|
||||
"addx -30\n",
|
||||
"addx 12\n",
|
||||
"addx -1\n",
|
||||
"addx 3\n",
|
||||
"addx 1\n",
|
||||
"noop\n",
|
||||
"noop\n",
|
||||
"noop\n",
|
||||
"addx -9\n",
|
||||
"addx 18\n",
|
||||
"addx 1\n",
|
||||
"addx 2\n",
|
||||
"noop\n",
|
||||
"noop\n",
|
||||
"addx 9\n",
|
||||
"noop\n",
|
||||
"noop\n",
|
||||
"noop\n",
|
||||
"addx -1\n",
|
||||
"addx 2\n",
|
||||
"addx -37\n",
|
||||
"addx 1\n",
|
||||
"addx 3\n",
|
||||
"noop\n",
|
||||
"addx 15\n",
|
||||
"addx -21\n",
|
||||
"addx 22\n",
|
||||
"addx -6\n",
|
||||
"addx 1\n",
|
||||
"noop\n",
|
||||
"addx 2\n",
|
||||
"addx 1\n",
|
||||
"noop\n",
|
||||
"addx -10\n",
|
||||
"noop\n",
|
||||
"noop\n",
|
||||
"addx 20\n",
|
||||
"addx 1\n",
|
||||
"addx 2\n",
|
||||
"addx 2\n",
|
||||
"addx -6\n",
|
||||
"addx -11\n",
|
||||
"noop\n",
|
||||
"noop\n",
|
||||
"noop\n",
|
||||
);
|
||||
assert_eq!(
|
||||
parse(input),
|
||||
vec![
|
||||
Instruction::Addx(15),
|
||||
Instruction::Addx(-11),
|
||||
Instruction::Addx(6),
|
||||
Instruction::Addx(-3),
|
||||
Instruction::Addx(5),
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(-8),
|
||||
Instruction::Addx(13),
|
||||
Instruction::Addx(4),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(5),
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(5),
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(5),
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(5),
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(-35),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(24),
|
||||
Instruction::Addx(-19),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(16),
|
||||
Instruction::Addx(-11),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(21),
|
||||
Instruction::Addx(-15),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-3),
|
||||
Instruction::Addx(9),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(-3),
|
||||
Instruction::Addx(8),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(5),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-36),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(7),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(2),
|
||||
Instruction::Addx(6),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(7),
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-13),
|
||||
Instruction::Addx(13),
|
||||
Instruction::Addx(7),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(-33),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(2),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(8),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(2),
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(17),
|
||||
Instruction::Addx(-9),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(-3),
|
||||
Instruction::Addx(11),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-13),
|
||||
Instruction::Addx(-19),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(3),
|
||||
Instruction::Addx(26),
|
||||
Instruction::Addx(-30),
|
||||
Instruction::Addx(12),
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(3),
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-9),
|
||||
Instruction::Addx(18),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(2),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(9),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(2),
|
||||
Instruction::Addx(-37),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(3),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(15),
|
||||
Instruction::Addx(-21),
|
||||
Instruction::Addx(22),
|
||||
Instruction::Addx(-6),
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(2),
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-10),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(20),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(2),
|
||||
Instruction::Addx(2),
|
||||
Instruction::Addx(-6),
|
||||
Instruction::Addx(-11),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
191
2022/days/10/src/part1.rs
Normal file
191
2022/days/10/src/part1.rs
Normal file
|
|
@ -0,0 +1,191 @@
|
|||
use std::collections::VecDeque;
|
||||
|
||||
use crate::{parse::*, machine::Machine};
|
||||
|
||||
pub fn part1(input: VecDeque<Instruction>) -> i32 {
|
||||
let mut machine = Machine::load_program(input.into());
|
||||
let mut total = 0;
|
||||
|
||||
//20th
|
||||
total+=machine.step20();
|
||||
|
||||
machine.step20();
|
||||
//60th
|
||||
total+=machine.step20();
|
||||
|
||||
machine.step20();
|
||||
//100th
|
||||
total+=machine.step20();
|
||||
|
||||
machine.step20();
|
||||
//140th
|
||||
total+=machine.step20();
|
||||
|
||||
machine.step20();
|
||||
//180th
|
||||
total+=machine.step20();
|
||||
|
||||
machine.step20();
|
||||
//220th
|
||||
total+=machine.step20();
|
||||
|
||||
total
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_part1() {
|
||||
let input = vec![
|
||||
Instruction::Addx(15),
|
||||
Instruction::Addx(-11),
|
||||
Instruction::Addx(6),
|
||||
Instruction::Addx(-3),
|
||||
Instruction::Addx(5),
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(-8),
|
||||
Instruction::Addx(13),
|
||||
Instruction::Addx(4),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(5),
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(5),
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(5),
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(5),
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(-35),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(24),
|
||||
Instruction::Addx(-19),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(16),
|
||||
Instruction::Addx(-11),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(21),
|
||||
Instruction::Addx(-15),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-3),
|
||||
Instruction::Addx(9),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(-3),
|
||||
Instruction::Addx(8),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(5),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-36),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(7),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(2),
|
||||
Instruction::Addx(6),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(7),
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-13),
|
||||
Instruction::Addx(13),
|
||||
Instruction::Addx(7),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(-33),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(2),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(8),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(2),
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(17),
|
||||
Instruction::Addx(-9),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(-3),
|
||||
Instruction::Addx(11),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-13),
|
||||
Instruction::Addx(-19),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(3),
|
||||
Instruction::Addx(26),
|
||||
Instruction::Addx(-30),
|
||||
Instruction::Addx(12),
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(3),
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-9),
|
||||
Instruction::Addx(18),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(2),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(9),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-1),
|
||||
Instruction::Addx(2),
|
||||
Instruction::Addx(-37),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(3),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(15),
|
||||
Instruction::Addx(-21),
|
||||
Instruction::Addx(22),
|
||||
Instruction::Addx(-6),
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(2),
|
||||
Instruction::Addx(1),
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(-10),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::Addx(20),
|
||||
Instruction::Addx(1),
|
||||
Instruction::Addx(2),
|
||||
Instruction::Addx(2),
|
||||
Instruction::Addx(-6),
|
||||
Instruction::Addx(-11),
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
Instruction::NoOp,
|
||||
];
|
||||
assert_eq!(part1(input.into()), 13140)
|
||||
}
|
||||
}
|
||||
19
2022/days/10/src/part2.rs
Normal file
19
2022/days/10/src/part2.rs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
use std::collections::VecDeque;
|
||||
|
||||
use crate::{crt::Crt, parse::*, machine::Machine};
|
||||
|
||||
pub fn part2(input: VecDeque<Instruction>) -> Crt {
|
||||
let mut machine = Machine::load_program(input);
|
||||
while machine.step().0 {};
|
||||
machine.crt
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_part2() {
|
||||
assert_eq!(0, 0);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue