Solve day 10
This commit is contained in:
parent
ce99b571c0
commit
4f7ed8d228
3 changed files with 372 additions and 0 deletions
140
input/10
Normal file
140
input/10
Normal file
|
@ -0,0 +1,140 @@
|
||||||
|
addx 1
|
||||||
|
addx 4
|
||||||
|
addx 21
|
||||||
|
addx -20
|
||||||
|
addx 4
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 5
|
||||||
|
addx 3
|
||||||
|
noop
|
||||||
|
addx 2
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 4
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
addx -37
|
||||||
|
addx 22
|
||||||
|
addx -4
|
||||||
|
addx -14
|
||||||
|
addx 2
|
||||||
|
addx 5
|
||||||
|
addx 3
|
||||||
|
addx -2
|
||||||
|
addx 2
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx -15
|
||||||
|
addx 32
|
||||||
|
addx -14
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx 3
|
||||||
|
noop
|
||||||
|
addx -13
|
||||||
|
addx -2
|
||||||
|
addx 18
|
||||||
|
addx -36
|
||||||
|
noop
|
||||||
|
addx 11
|
||||||
|
addx -7
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 6
|
||||||
|
addx 22
|
||||||
|
addx -21
|
||||||
|
addx 3
|
||||||
|
addx 2
|
||||||
|
addx 4
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 5
|
||||||
|
addx -16
|
||||||
|
addx 17
|
||||||
|
addx 2
|
||||||
|
addx 5
|
||||||
|
addx -11
|
||||||
|
addx 15
|
||||||
|
addx -15
|
||||||
|
addx -24
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 7
|
||||||
|
addx 2
|
||||||
|
addx -6
|
||||||
|
addx 9
|
||||||
|
noop
|
||||||
|
addx 5
|
||||||
|
noop
|
||||||
|
addx -3
|
||||||
|
addx 4
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 7
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 5
|
||||||
|
addx -28
|
||||||
|
addx 29
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx -7
|
||||||
|
addx -29
|
||||||
|
noop
|
||||||
|
addx 7
|
||||||
|
addx -2
|
||||||
|
addx 2
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx -3
|
||||||
|
addx 4
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx 8
|
||||||
|
addx -30
|
||||||
|
addx 25
|
||||||
|
addx 7
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx -2
|
||||||
|
addx 2
|
||||||
|
addx -10
|
||||||
|
addx -24
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx 2
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
addx 2
|
||||||
|
addx 8
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx -1
|
||||||
|
addx -9
|
||||||
|
addx 14
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
noop
|
146
input/10-test
Normal file
146
input/10-test
Normal 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
|
86
src/bin/10.rs
Normal file
86
src/bin/10.rs
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
use aoc2022::*;
|
||||||
|
use itertools::Itertools;
|
||||||
|
|
||||||
|
const INPUT: &str = include_str!("../../input/10");
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let solution: i64 = Cpu::load(input())
|
||||||
|
.filter(|(cycle, _x)| [20, 60, 100, 140, 180, 220].contains(cycle))
|
||||||
|
.map(|(cycle, x)| cycle * x)
|
||||||
|
.sum();
|
||||||
|
solved_level_1(solution);
|
||||||
|
|
||||||
|
Cpu::load(input()).for_each(|(cycle, x)| {
|
||||||
|
let screen_x = (cycle - 1) % 40;
|
||||||
|
|
||||||
|
if screen_x.abs_diff(x) <= 1 {
|
||||||
|
print!("█");
|
||||||
|
} else {
|
||||||
|
print!(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
if screen_x == 39 {
|
||||||
|
println!();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Cpu {
|
||||||
|
x: i64,
|
||||||
|
instruction_stack: Vec<Instruction>,
|
||||||
|
cycle: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Cpu {
|
||||||
|
fn load<I>(it: I) -> Self
|
||||||
|
where
|
||||||
|
I: IntoIterator<Item = Instruction>,
|
||||||
|
{
|
||||||
|
let mut instructions = it.into_iter().collect_vec();
|
||||||
|
instructions.reverse();
|
||||||
|
Self {
|
||||||
|
x: 1,
|
||||||
|
instruction_stack: instructions,
|
||||||
|
cycle: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Iterator for Cpu {
|
||||||
|
type Item = (i64, i64);
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
self.cycle += 1;
|
||||||
|
|
||||||
|
let Some(op) = self.instruction_stack.pop() else {
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
|
||||||
|
let result = (self.cycle, self.x);
|
||||||
|
|
||||||
|
match op {
|
||||||
|
Instruction::Nop => {}
|
||||||
|
Instruction::AddX(n) => self.instruction_stack.push(Instruction::DoAddX(n)),
|
||||||
|
Instruction::DoAddX(n) => self.x += n,
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Instruction {
|
||||||
|
Nop,
|
||||||
|
AddX(i64),
|
||||||
|
DoAddX(i64),
|
||||||
|
}
|
||||||
|
|
||||||
|
fn input() -> impl Iterator<Item = Instruction> {
|
||||||
|
INPUT.lines().map(|line| {
|
||||||
|
if line.starts_with("addx") {
|
||||||
|
let n = line.split(' ').nth(1).unwrap();
|
||||||
|
Instruction::AddX(n.parse().unwrap())
|
||||||
|
} else {
|
||||||
|
Instruction::Nop
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue