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
|
/// Sometimes I wake up in the morning and pretend to be a regex engine
use std::{error::Error, fs::File, io::Read};
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
enum State {
Outside,
D,
O,
DoLParen,
N,
Apos,
T,
DontLParen,
M,
U,
L,
Num1,
Num2,
End,
}
struct Scanner {
state: State,
mul_enable: bool,
num1: i64,
num2: i64,
total1: i64,
total2: i64,
}
impl Scanner {
fn new() -> Self {
Self {
state: State::Outside,
mul_enable: true,
num1: 0,
num2: 0,
total1: 0,
total2: 0,
}
}
fn feed(&mut self, s: &[u8]) {
// Technically I should be feeding an "EOF" character but it's fine
// since we don't terminate on repetition
for c in s.iter().copied() {
let next_state = match self.state {
State::Outside | State::End => match c {
b'm' => State::M,
b'd' => State::D,
_ => State::Outside,
},
State::D => match c {
b'o' => State::O,
_ => State::Outside,
},
State::O => match c {
b'(' => State::DoLParen,
b'n' => State::N,
_ => State::Outside,
},
State::N => match c {
b'\'' => State::Apos,
_ => State::Outside,
},
State::Apos => match c {
b't' => State::T,
_ => State::Outside,
},
State::T => match c {
b'(' => State::DontLParen,
_ => State::Outside,
},
State::DoLParen | State::DontLParen => match c {
b')' => State::End,
_ => State::Outside,
},
State::M => match c {
b'u' => State::U,
_ => State::Outside,
},
State::U => match c {
b'l' => State::L,
_ => State::Outside,
},
State::L => match c {
b'(' => State::Num1,
_ => State::Outside,
},
State::Num1 => match c {
b',' => State::Num2,
_ if c.is_ascii_digit() => State::Num1,
_ => State::Outside,
},
State::Num2 => match c {
b')' => State::End,
_ if c.is_ascii_digit() => State::Num2,
_ => State::Outside,
},
};
match (self.state, next_state) {
(State::L, State::Num1) => self.num1 = 0,
(State::Num1, State::Num2) => self.num2 = 0,
(State::DoLParen, State::End) => self.mul_enable = true,
(State::DontLParen, State::End) => self.mul_enable = false,
(State::Num2, State::End) => {
let m = self.num1 * self.num2;
self.total1 += m;
if self.mul_enable {
self.total2 += m;
}
}
(State::Num1, State::Num1) => {
self.num1 *= 10;
self.num1 += char::from(c).to_digit(10).unwrap() as i64;
}
(State::Num2, State::Num2) => {
self.num2 *= 10;
self.num2 += char::from(c).to_digit(10).unwrap() as i64;
}
_ => (),
}
self.state = next_state;
}
}
fn totals(&self) -> (i64, i64) {
(self.total1, self.total2)
}
}
fn main() -> Result<(), Box<dyn Error>> {
let mut f = File::open("input/3")?;
let mut buf = [0; 8 * 1024];
let mut scanner = Scanner::new();
loop {
let n = f.read(&mut buf)?;
if n == 0 {
break;
};
scanner.feed(&buf[..n]);
}
let (p1, p2) = scanner.totals();
println!("{p1}\n{p2}");
Ok(())
}
|