-
Notifications
You must be signed in to change notification settings - Fork 0
/
day23.rs
138 lines (117 loc) · 3.42 KB
/
day23.rs
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//! [Day 23: Coprocessor Conflagration](https://adventofcode.com/2017/day/23)
use std::collections::HashMap;
struct Program {
opcodes: Vec<(String, Vec<String>)>,
regs: HashMap<String, i64>,
ip: i64,
mul_called: u32,
}
impl Program {
fn new(opcodes: &[(String, Vec<String>)]) -> Self {
Self {
opcodes: opcodes.to_vec(),
regs: HashMap::new(),
ip: 0,
mul_called: 0,
}
}
fn run(&mut self) {
while self.ip >= 0 && self.ip < (self.opcodes.len() as i64) {
let (instr, args) = &self.opcodes[self.ip as usize];
self.ip += 1;
let mut value = |i: usize| {
let a = &args[i];
if let Ok(v) = a.parse::<i64>() {
v
} else {
*self.regs.entry(a.clone()).or_insert(0i64)
}
};
// println!("{}> {instr} {args:?}", self.ip);
match instr.as_str() {
"set" => {
let v = value(1);
self.regs.insert(args[0].clone(), v);
}
"sub" => {
*self.regs.entry(args[0].clone()).or_insert(0) -= value(1);
}
"mul" => {
self.mul_called += 1;
*self.regs.entry(args[0].clone()).or_insert(0) *= value(1);
}
"jnz" => {
if value(0) != 0 {
self.ip += value(1) - 1;
}
}
"hcf" => {
return;
}
_ => panic!("unknown instr {instr}"),
}
}
}
}
struct Puzzle {
program: Vec<(String, Vec<String>)>,
}
impl Puzzle {
const fn new() -> Self {
Self { program: vec![] }
}
/// Get the puzzle input.
fn configure(&mut self, path: &str) {
let data = std::fs::read_to_string(path).unwrap();
self.program = data
.lines()
.filter_map(|s| s.split_once(' '))
.map(|(a, b)| {
(
a.to_string(),
b.split_ascii_whitespace()
.map(String::from)
.collect::<Vec<_>>(),
)
})
.collect();
}
/// Solve part one.
fn part1(&self) -> u32 {
let mut p = Program::new(&self.program);
p.run();
p.mul_called
}
/// Solve part two.
fn part2(&self) -> i64 {
let mut p = Program::new(&self.program);
p.regs.insert("a".to_string(), 1);
p.opcodes[11] = ("hcf".to_string(), vec![]);
p.run();
let b = p.regs["b"];
let c = p.regs["c"];
// non prime numbers between b and c, 17 by 17
let mut h = 0;
for n in (b..=c).step_by(17) {
if n % 2 == 0 {
h += 1;
} else {
let sqrt = ((n as f32).sqrt()).round() as i64;
for i in 3..=sqrt {
if n % i == 0 {
h += 1;
break;
}
}
}
}
h
}
}
fn main() {
let args = aoc::parse_args();
let mut puzzle = Puzzle::new();
puzzle.configure(args.path.as_str());
println!("{}", puzzle.part1());
println!("{}", puzzle.part2());
}