-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3.c3
80 lines (73 loc) · 2.53 KB
/
day3.c3
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
import std::io;
import std::time;
import std::ascii;
fn void main()
{
@pool() {
Clock c = clock::now();
io::printfn("- Part1: %d - %s", solve(true)!!, c.mark());
io::printfn("- Part2: %d - %s", solve(false)!!, c.mark());
};
}
enum State { FUN, LEFT, RIGHT }
fn long! solve(bool part1)
{
File input = file::open("input.txt", "r")!;
long result = 0;
State state = State.FUN;
bool mul = true;
DString tokens;
tokens.temp_init();
long[2] nums;
while (try ch = input.read_byte()) {
switch (state) {
case State.LEFT:
case State.RIGHT:
char ending = state == State.LEFT ? ',' : ')';
if (ch == ending) {
if (tokens.len() > 3 || tokens.len() < 1) {
tokens.clear();
state = State.FUN;
nextcase state;
}
short idx = state == State.LEFT ? 0 : 1;
nums[idx] = tokens.str_view().to_long()!;
state = state == State.LEFT ? State.RIGHT : State.FUN;
tokens.clear();
if (state == State.FUN) {
long mult = nums[0] * nums[1];
if (part1) result += mult;
if (mul && !part1) result += mult;
nextcase state;
}
} else if (!ascii::is_digit(ch)) {
tokens.clear();
state = State.FUN;
nextcase state;
} else {
tokens.append(ch);
}
case State.FUN:
tokens.append(ch);
if (ch == '(') {
if (tokens.str_view().ends_with("mul(")) {
state = State.LEFT;
tokens.clear();
} else if (try char peek = input.read_byte()) {
if (peek == ')') {
if (tokens.str_view().ends_with("don't(")) {
mul = false;
} else if (tokens.str_view().ends_with("do(")) {
mul = true;
}
tokens.clear();
} else {
tokens.clear();
tokens.append(peek);
}
}
}
}
}
return result;
}