-
Notifications
You must be signed in to change notification settings - Fork 3
/
day_03.rs
109 lines (88 loc) · 2.36 KB
/
day_03.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
use common::{solution, Answer};
solution!("Mull It Over", 3);
fn part_a(input: &str) -> Answer {
solve(input, false).into()
}
fn part_b(input: &str) -> Answer {
solve(input, true).into()
}
fn solve(input: &str, part_b: bool) -> u32 {
let mut out = 0;
let mut parser = Parser::new(input);
let mut active = true;
while !parser.is_eof() {
active |= parser.expect("do()");
active &= !parser.expect("don't()");
if parser.expect("mul(") {
let Some(a) = parser.number() else { continue };
if !parser.expect(",") {
continue;
}
let Some(b) = parser.number() else { continue };
if !parser.expect(")") {
continue;
}
if active || !part_b {
out += a * b;
}
} else {
parser.advance(1);
}
}
out
}
struct Parser {
chars: Vec<char>,
idx: usize,
}
impl Parser {
pub fn new(input: &str) -> Self {
Self {
chars: input.chars().collect(),
idx: 0,
}
}
pub fn expect(&mut self, str: &str) -> bool {
let valid = self.idx + str.len() < self.chars.len()
&& self.chars[self.idx..self.idx + str.len()]
.iter()
.zip(str.chars())
.all(|(&a, b)| a == b);
if valid {
self.idx += str.len();
}
valid
}
pub fn number(&mut self) -> Option<u32> {
let mut working = String::new();
while self.chars[self.idx].is_ascii_digit() && self.idx < self.chars.len() {
working.push(self.chars[self.idx]);
self.idx += 1;
}
working.parse::<u32>().ok()
}
pub fn advance(&mut self, count: usize) {
self.idx += count;
}
pub fn is_eof(&self) -> bool {
self.idx >= self.chars.len()
}
}
#[cfg(test)]
mod test {
use indoc::indoc;
const CASE_A: &str = indoc! {"
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
"};
const CASE_B: &str = indoc! {"
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
"};
#[test]
fn part_a() {
assert_eq!(super::part_a(CASE_A), 161.into());
}
#[test]
fn part_b() {
assert_eq!(super::part_b(CASE_B), 48.into());
}
}