-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
184 lines (170 loc) · 5.23 KB
/
main.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use std::iter::Peekable;
use std::str::Chars;
#[derive(Debug, PartialEq, Clone)]
enum Token {
Num(usize),
Plus,
Times,
Expr(Vec<Token>),
}
fn parse(it: &mut Peekable<Chars>) -> Vec<Token> {
let mut tokens = vec![];
loop {
let next = it.next();
match next {
Some(c) => match c {
'(' => {
let mut sub = vec![c];
let mut level = 1;
while let Some(c) = it.next() {
sub.push(c);
match c {
'(' => {
level += 1;
}
')' => {
level -= 1;
if level == 0 {
break;
}
}
_ => {}
}
}
let sub_expr = sub[1..(sub.len() - 1)].iter().collect::<String>();
let mut sub_it = sub_expr.chars().peekable();
tokens.push(Token::Expr(parse(&mut sub_it)));
}
'+' => {
tokens.push(Token::Plus);
}
'*' => {
tokens.push(Token::Times);
}
c if c.is_digit(10) => {
let mut sub = vec![c];
while let Some(c) = it.peek() {
if c.is_digit(10) {
sub.push(it.next().unwrap());
} else {
break;
}
}
tokens.push(Token::Num(
sub.iter().collect::<String>().parse::<usize>().unwrap(),
))
}
' ' => {
// ignore spaces
}
_ => panic!(format!("unexpected char {}", c)),
},
None => break,
}
}
tokens
}
fn parse_expr(expr: &str) -> Vec<Token> {
let mut it = expr.chars().peekable();
parse(&mut it)
}
fn eval1(expr_str: &str) -> usize {
let tokens = parse_expr(expr_str);
fn eval_tokens(t: &[Token]) -> usize {
fn as_num(t: &Token) -> usize {
match t {
Token::Num(n) => *n,
Token::Expr(e) => eval_tokens(&e),
_ => panic!(format!("expected number, instead saw token {:?}", t)),
}
}
if t.len() == 1 {
return as_num(&t[0]);
}
let left = as_num(&t[0]);
let op = &t[1];
let right = as_num(&t[2]);
let val = match op {
Token::Plus => left + right,
Token::Times => left * right,
_ => panic!(format!("unexpected op {:?}", op)),
};
if t.len() > 3 {
let mut next = vec![Token::Num(val)];
for x in &t[3..] {
next.push(x.clone());
}
eval_tokens(&next)
} else {
val
}
}
eval_tokens(&tokens)
}
fn eval2(expr_str: &str) -> usize {
let tokens = parse_expr(expr_str);
fn eval_tokens(t: &[Token]) -> usize {
fn as_num(t: &Token) -> usize {
match t {
Token::Num(n) => *n,
Token::Expr(e) => eval_tokens(&e),
_ => panic!(format!("expected number, instead saw token {:?}", t)),
}
}
if t.len() == 1 {
return as_num(&t[0]);
}
let left = as_num(&t[0]);
let op = &t[1];
match op {
Token::Plus => {
let right = as_num(&t[2]);
let val = left + right;
if t.len() > 3 {
let mut next = vec![Token::Num(val)];
for x in &t[3..] {
next.push(x.clone());
}
eval_tokens(&next)
} else {
val
}
}
Token::Times => left * eval_tokens(&t[2..]),
_ => panic!(format!("unexpected op {:?}", op)),
}
}
eval_tokens(&tokens)
}
fn part1(s: &str) -> usize {
s.split('\n').map(eval1).sum()
}
fn part2(s: &str) -> usize {
s.split('\n').map(eval2).sum()
}
fn main() {
println!("Part 1: {}", part1(include_str!("in.txt")));
println!("Part 2: {}", part2(include_str!("in.txt")));
}
#[test]
fn test_part1() {
assert_eq!(eval1("1 + 2 * 3 + 4 * 5 + 6"), 71);
assert_eq!(eval1("2 * 3 + (4 * 5)"), 26);
assert_eq!(eval1("5 + (8 * 3 + 9 + 3 * 4 * 3)"), 437);
assert_eq!(eval1("5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4))"), 12240);
assert_eq!(
eval1("((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2"),
13632
);
}
#[test]
fn test_part2() {
assert_eq!(eval2("1 + (2 * 3) + (4 * (5 + 6))"), 51);
assert_eq!(eval2("2 * 3 + (4 * 5)"), 46);
assert_eq!(eval2("5 + (8 * 3 + 9 + 3 * 4 * 3)"), 1445);
assert_eq!(eval2("5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4))"), 669060);
assert_eq!(
eval2("((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2"),
23340
);
}