-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday03.rs
149 lines (124 loc) · 3.64 KB
/
day03.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
type Expr = (u32, u32);
struct Parser<'a> {
chars: std::iter::Peekable<std::str::Chars<'a>>,
dont: bool,
}
impl<'a> Parser<'a> {
fn new(input: &'a str) -> Self {
Self {
chars: input.chars().peekable(),
dont: false,
}
}
const PATTERN_DONT: [char; 6] = ['o', 'n', '\'', 't', '(', ')'];
const PATTERN_DO: [char; 3] = ['o', '(', ')'];
const PATTERN_MUL: [char; 2] = ['u', 'l'];
fn match_pattern(&mut self, pattern: &[char]) -> bool {
if self.chars.clone().zip(pattern).all(|(a, &b)| a == b) {
for _ in 0..pattern.len() {
self.chars.next();
}
true
} else {
false
}
}
fn parse(mut self) -> (Vec<Expr>, Vec<Expr>) {
let mut do_exprs = Vec::new();
let mut dont_exprs = Vec::new();
while self.chars.peek().is_some() {
if let Some(expr) = self.expr() {
if self.dont {
dont_exprs.push(expr);
} else {
do_exprs.push(expr);
}
}
}
(do_exprs, dont_exprs)
}
fn expr(&mut self) -> Option<Expr> {
match self.chars.next()? {
'd' => self.do_(),
'm' => self.mul(),
_ => None,
}
}
fn do_(&mut self) -> Option<Expr> {
if self.match_pattern(&Self::PATTERN_DONT) {
self.dont = true;
} else if self.match_pattern(&Self::PATTERN_DO) {
self.dont = false;
}
None
}
fn mul(&mut self) -> Option<Expr> {
if self.match_pattern(&Self::PATTERN_MUL) {
self.chars.next_if_eq(&'(')?;
let a = self.number()?;
self.chars.next_if_eq(&',')?;
let b = self.number()?;
self.chars.next_if_eq(&')')?;
Some((a, b))
} else {
None
}
}
fn number(&mut self) -> Option<u32> {
let mut num = Vec::new();
while let Some(c) = self.chars.next_if(|&c| c.is_ascii_digit()) {
num.push(c as u32 - b'0' as u32);
}
if !num.is_empty() {
Some(num.iter().enumerate().fold(0, |acc, (i, n)| {
acc + n * 10_u32.pow((num.len() - 1 - i) as u32)
}))
} else {
None
}
}
}
#[derive(Clone)]
struct Solution {
do_exprs: Vec<Expr>,
dont_exprs: Vec<Expr>,
}
impl Solver for Solution {
fn new(input: &str) -> Anyhow<Self> {
let (do_exprs, dont_exprs) = Parser::new(input).parse();
Ok(Self {
do_exprs,
dont_exprs,
})
}
fn part1(&mut self) -> Anyhow<impl fmt::Display> {
Ok(self
.do_exprs
.iter()
.chain(self.dont_exprs.iter())
.fold(0, |acc, (a, b)| acc + a * b))
}
fn part2(&mut self) -> Anyhow<impl fmt::Display> {
Ok(self.do_exprs.iter().fold(0, |acc, (a, b)| acc + a * b))
}
}
aoc::solution!();
#[cfg(test)]
mod test {
use super::{Solution, Solver};
const INPUT1: &str = r"xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))";
const INPUT2: &str =
r"xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5)))";
#[test]
fn test_part1() {
let mut solution = Solution::new(INPUT1).unwrap();
let answer = solution.part1().unwrap().to_string();
assert_eq!(answer, "161");
}
#[test]
fn test_part2() {
let mut solution = Solution::new(INPUT2).unwrap();
let answer = solution.part2().unwrap().to_string();
assert_eq!(answer, "48");
}
}