-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.mojo
143 lines (115 loc) · 3.07 KB
/
3.mojo
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
# fn:
# default is immutable ref.
# def:
# default is immutable ref. if mutated, it will copy it.
# conventions: borrowed, inout, owned, ref????
# & and &mut seem superior :/
# 171183089
# 63866497
from collections import Optional
fn munch_num(data: String, inout idx: Int) raises -> Int:
var acc = String()
acc += munch_digit(data, idx)
# i miss you ? operator
try:
acc += munch_digit(data, idx)
except:
pass
try:
acc += munch_digit(data, idx)
except:
pass
return int(acc)
fn munch_digit(data: String, inout idx: Int) raises -> String:
if not (idx < len(data) and data[idx].isdigit()):
raise Error()
x = data[idx]
idx += 1
return x
fn munch_char(data: String, inout idx: Int, c: String) raises:
if not (idx < len(data) and data[idx] == c):
raise Error()
idx += 1
fn part_one() raises:
with open("./inputs/3.input", "r") as f:
data = f.read()
# look for mul
acc = 0
prev_idx = 0
while prev_idx < len(data):
try:
var idx = prev_idx
munch_char(data, idx, "m")
munch_char(data, idx, "u")
munch_char(data, idx, "l")
munch_char(data, idx, "(")
val1 = munch_num(data, idx)
munch_char(data, idx, ",")
val2 = munch_num(data, idx)
munch_char(data, idx, ")")
acc += val1 * val2
prev_idx = idx
except:
prev_idx += 1
print(acc)
fn match_mul(data: String, inout idx: Int) raises -> Int:
munch_char(data, idx, "m")
munch_char(data, idx, "u")
munch_char(data, idx, "l")
munch_char(data, idx, "(")
val1 = munch_num(data, idx)
munch_char(data, idx, ",")
val2 = munch_num(data, idx)
munch_char(data, idx, ")")
return val1 * val2
fn match_do(data: String, inout idx: Int) raises:
munch_char(data, idx, "d")
munch_char(data, idx, "o")
munch_char(data, idx, "(")
munch_char(data, idx, ")")
fn match_dont(data: String, inout idx: Int) raises:
munch_char(data, idx, "d")
munch_char(data, idx, "o")
munch_char(data, idx, "n")
munch_char(data, idx, "'")
munch_char(data, idx, "t")
munch_char(data, idx, "(")
munch_char(data, idx, ")")
fn part_two() raises:
with open("./inputs/3.input", "r") as f:
data = f.read()
# look for mul
acc = 0
prev_idx = 0
ok = True
while prev_idx < len(data):
try:
idx = prev_idx
x = match_mul(data, idx)
if ok:
acc += x
prev_idx = idx
continue
except:
pass
try:
idx = prev_idx
match_dont(data, idx)
ok = False
prev_idx = idx
continue
except:
pass
try:
idx = prev_idx
match_do(data, idx)
ok = True
prev_idx = idx
continue
except:
pass
prev_idx += 1
print(acc)
fn main() raises:
part_one()
part_two()