-
Notifications
You must be signed in to change notification settings - Fork 0
/
08.rs
96 lines (87 loc) · 2.24 KB
/
08.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
#![feature(test)]
type Input = Vec<Instruction>;
#[derive(Debug, Clone, Copy)]
struct Instruction {
operation: Operation,
argument: i32,
}
#[derive(Debug, Copy, Clone)]
enum Operation {
Nop,
Acc,
Jmp,
}
fn setup(input: &str) -> Input {
input
.lines()
.map(|line| {
let mut line = line.split_whitespace();
let operation = match line.next().unwrap() {
"nop" => Operation::Nop,
"acc" => Operation::Acc,
"jmp" => Operation::Jmp,
_ => panic!(),
};
let argument = line.next().unwrap().parse().unwrap();
Instruction {
operation,
argument,
}
})
.collect()
}
fn part1(input: &Input) -> i32 {
let mut exec = vec![false; input.len()];
let mut i = 0;
let mut acc = 0;
while !exec[i] {
exec[i] = true;
match input[i].operation {
Operation::Nop => i += 1,
Operation::Acc => {
acc += input[i].argument;
i += 1;
}
Operation::Jmp => i = (i as i32 + input[i].argument) as _,
}
}
acc
}
fn simulate(input: &Input) -> Option<i32> {
let mut exec = vec![false; input.len()];
let mut i = 0;
let mut acc = 0;
loop {
if i == input.len() {
return Some(acc);
}
if i > input.len() || exec[i] {
return None;
}
exec[i] = true;
match input[i].operation {
Operation::Nop => i += 1,
Operation::Acc => {
acc += input[i].argument;
i += 1;
}
Operation::Jmp => i = (i as i32 + input[i].argument).try_into().ok()?,
}
}
}
fn part2(input: &Input) -> i32 {
let mut prog = input.clone();
for i in 0..prog.len() {
prog[i].operation = match prog[i].operation {
Operation::Nop => Operation::Jmp,
Operation::Acc => continue,
Operation::Jmp => Operation::Nop,
};
if let Some(acc) = simulate(&prog) {
return acc;
}
prog[i].operation = input[i].operation;
}
panic!()
}
aoc::main!(2020, 8, ex: 1);