-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8.rs
94 lines (76 loc) · 2.26 KB
/
day8.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
//! [Day 8: I Heard You Like Registers](https://adventofcode.com/2017/day/8)
use rustc_hash::FxHashMap;
use pest::Parser;
use pest_derive::Parser;
#[derive(Parser)]
#[grammar = "./src/year2017/day8/day8.pest"]
struct MyParser;
struct Cpu {
registers: FxHashMap<String, i32>,
last_max: i32, // i.e. part 1
value_max: i32, // i.e. part 2
}
impl Cpu {
fn parse_line(&mut self, line: &str) {
let mut a = MyParser::parse(Rule::instr, line)
.unwrap()
.next()
.unwrap()
.into_inner();
let target = a.next().unwrap().as_str();
let oper = a.next().unwrap().as_rule();
let value = a.next().unwrap().as_str().parse::<i32>().unwrap();
let lhs = a.next().unwrap().as_str();
let cmp = a.next().unwrap().as_rule();
let rhs = a.next().unwrap().as_str().parse::<i32>().unwrap();
let lhs = self.registers.get(lhs).unwrap_or(&0);
let ok = match cmp {
Rule::equal => lhs == &rhs,
Rule::different => lhs != &rhs,
Rule::greater => lhs > &rhs,
Rule::greater_or_equal => lhs >= &rhs,
Rule::less => lhs < &rhs,
Rule::less_or_equal => lhs <= &rhs,
_ => panic!(),
};
if ok {
let r = self.registers.entry(target.to_string()).or_insert(0);
match oper {
Rule::inc => *r += value,
Rule::dec => *r -= value,
_ => panic!(),
}
let max = *self.registers.values().max().unwrap();
self.last_max = max;
self.value_max = self.value_max.max(self.last_max);
}
}
}
/// # Panics
#[must_use]
pub fn solve(data: &str) -> (i32, i32) {
let mut cpu = Cpu {
registers: FxHashMap::default(),
last_max: 0,
value_max: 0,
};
for line in data.lines() {
cpu.parse_line(line);
}
(cpu.last_max, cpu.value_max)
}
pub fn main() {
let args = aoc::parse_args();
args.run(solve);
}
#[cfg(test)]
mod test {
use super::*;
const TEST_INPUT: &str = include_str!("test.txt");
#[test]
fn test_solve() {
let answer = solve(TEST_INPUT);
assert_eq!(answer.0, 1);
assert_eq!(answer.1, 10);
}
}