generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17.rs
169 lines (146 loc) · 4.04 KB
/
17.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use itertools::Itertools;
advent_of_code::solution!(17);
#[derive(Debug, Eq, Hash, PartialEq, Clone, Copy)]
struct Machine {
a: u64,
b: u64,
c: u64,
pc: usize,
}
impl Machine {
pub fn perform_cycle(&mut self, program: &[u8]) -> Option<u64> {
let op_code = program[self.pc];
let operand = program[self.pc + 1];
match op_code {
0 => {
self.a /= 2u64.pow(self.get_combo_operand(operand) as u32);
self.pc += 2;
None
}
1 => {
self.b ^= operand as u64;
self.pc += 2;
None
}
2 => {
self.b = self.get_combo_operand(operand) % 8;
self.pc += 2;
None
}
3 => {
if self.a == 0 {
self.pc += 2;
return None;
};
self.pc = operand as usize;
None
}
4 => {
self.b ^= self.c;
self.pc += 2;
None
}
5 => {
self.pc += 2;
Some(self.get_combo_operand(operand) % 8)
}
6 => {
self.b = self.a / 2u64.pow(self.get_combo_operand(operand) as u32);
self.pc += 2;
None
}
7 => {
self.c = self.a / 2u64.pow(self.get_combo_operand(operand) as u32);
self.pc += 2;
None
}
_ => None,
}
}
fn get_combo_operand(&self, operand: u8) -> u64 {
match operand {
0..=3 => operand as u64,
4 => self.a,
5 => self.b,
6 => self.c,
7.. => panic!("Bad stuff"),
}
}
}
pub fn part_one(input: &str) -> Option<String> {
let lines = input.lines().collect_vec();
let mut machine = Machine {
a: lines[0].split_once(":").unwrap().1.trim().parse().unwrap(),
b: lines[1].split_once(":").unwrap().1.trim().parse().unwrap(),
c: lines[2].split_once(":").unwrap().1.trim().parse().unwrap(),
pc: 0,
};
let program: Vec<u8> = lines[4]
.split_once(":")
.unwrap()
.1
.trim()
.split(",")
.map(|n| n.parse().unwrap())
.collect_vec();
let mut result: Vec<String> = vec![];
while machine.pc < program.len() {
if let Some(out) = machine.perform_cycle(&program) {
result.push(out.to_string())
};
}
Some(result.join(","))
}
pub fn part_two(input: &str) -> Option<u64> {
let lines = input.lines().collect_vec();
let program: Vec<u8> = lines[4]
.split_once(":")
.unwrap()
.1
.trim()
.split(",")
.map(|n| n.parse().unwrap())
.collect_vec();
dfs(0, &program, 0)
}
fn dfs(a: u64, program: &[u8], depth: u64) -> Option<u64> {
if depth == program.len() as u64 {
return Some(a);
}
let mut p_copy = program.to_owned();
p_copy.reverse();
for i in 0..8 {
let mut machine = Machine {
a: a * 8 + i,
b: 0,
c: 0,
pc: 0,
};
let mut result: Vec<u8> = vec![];
while machine.pc < program.len() {
if let Some(out) = machine.perform_cycle(program) {
result.push(out as u8)
};
}
if p_copy[depth as usize] == result[0] {
if let Some(out) = dfs(a * 8 + i, program, depth + 1) {
return Some(out);
};
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some("0,3,5,4,3,0".to_string()));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(117440));
}
}