1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
#![allow(dead_code)]
#![allow(unused_variables)]
use advent_of_code::prelude::*;
pub struct Cpu {
x: i64,
history: Vec<i64>,
}
impl Cpu {
fn new() -> Self {
Self {
x: 1,
history: vec![1],
}
}
fn step(&mut self, op: Op) {
match op {
Op::Noop => {
self.history.push(self.x);
}
Op::Addx(v) => {
self.history.push(self.x);
self.x += v;
self.history.push(self.x);
}
}
}
}
#[derive(Clone, Copy)]
pub enum Op {
Noop,
Addx(i64),
}
pub fn parse(fh: File) -> Result<impl Iterator<Item = Op>> {
Ok(parse::raw_lines(fh).map(|line| {
if line == "noop" {
Op::Noop
} else if let Some(v) = line.strip_prefix("addx ") {
Op::Addx(v.parse().unwrap())
} else {
panic!("failed to parse {}", line)
}
}))
}
pub fn part1(ops: impl Iterator<Item = Op>) -> Result<i64> {
let mut cpu = Cpu::new();
for op in ops {
cpu.step(op);
}
let mut total = 0;
for cycle in [20, 60, 100, 140, 180, 220] {
let strength = i64::try_from(cycle).unwrap() * cpu.history[cycle - 1];
total += strength;
}
Ok(total)
}
pub fn part2(ops: impl Iterator<Item = Op>) -> Result<i64> {
let mut cpu = Cpu::new();
for op in ops {
cpu.step(op);
}
for (y, row) in cpu.history.chunks(40).enumerate() {
for (x, pos) in row.iter().enumerate() {
if i64::try_from(x).unwrap().abs_diff(*pos) <= 1 {
print!("#");
} else {
print!(".");
}
}
println!();
}
Ok(0)
}
#[test]
fn test() {
assert_eq!(
part1(parse(parse::data(2022, 10).unwrap()).unwrap()).unwrap(),
15680
);
assert_eq!(
part2(parse(parse::data(2022, 10).unwrap()).unwrap()).unwrap(),
0
);
}
|