aoc/year2017/
day08.rs

1//! # I Heard You Like Registers
2//!
3//! Computes both parts in a single pass.
4use crate::util::hash::*;
5use crate::util::iter::*;
6use crate::util::parse::*;
7
8type Input = (i32, i32);
9
10pub fn parse(input: &str) -> Input {
11    let mut registers = FastMap::new();
12    let mut part_two = 0;
13
14    for [a, b, c, _, e, f, g] in input.split_ascii_whitespace().chunk::<7>() {
15        let first = *registers.entry(e).or_insert(0);
16        let second: i32 = g.signed();
17
18        let predicate = match f {
19            "==" => first == second,
20            "!=" => first != second,
21            ">=" => first >= second,
22            "<=" => first <= second,
23            ">" => first > second,
24            "<" => first < second,
25            _ => unreachable!(),
26        };
27
28        if predicate {
29            let third = registers.entry(a).or_insert(0);
30            let fourth: i32 = c.signed();
31
32            match b {
33                "inc" => *third += fourth,
34                "dec" => *third -= fourth,
35                _ => unreachable!(),
36            }
37
38            part_two = part_two.max(*third);
39        }
40    }
41
42    let part_one = *registers.values().max().unwrap();
43    (part_one, part_two)
44}
45
46pub fn part1(input: &Input) -> i32 {
47    input.0
48}
49
50pub fn part2(input: &Input) -> i32 {
51    input.1
52}