-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday-10.toit
77 lines (64 loc) · 1.6 KB
/
day-10.toit
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
import .file as file
import reader show BufferedReader
SCORES ::= {
')': 3,
']': 57,
'}': 1197,
'>': 25137,
}
CLOSERS ::= {
'(': ')',
'[': ']',
'{': '}',
'<': '>',
}
main:
reader := BufferedReader
file.Stream.for_read "10.txt"
score := 0
completer_scores := []
while line := reader.read_line:
completer_scorer := CompleterScorer
result := recurse line 0 null completer_scorer
if result is Score:
score += (result as Score).score
else:
completer_scores.add completer_scorer.score
completer_scores.sort --in_place: | a b | a - b
print score
print completer_scores[completer_scores.size / 2]
// Returning score: Line had wrong closer
// Returning position at end: Count up completer
// Returning position elsewhere: Keep going.
recurse line/string pos/int expect/int? completer/CompleterScorer -> Result:
if pos == line.size:
if expect != null: completer.add expect
return Position pos
char := line[pos]
if char == expect: return Position pos + 1
if CLOSERS.contains char:
result := recurse line pos + 1 CLOSERS[char] completer
if result is Score:
return result
else:
return recurse line ((result as Position).position) expect completer
return Score
SCORES[char]
interface Result:
class Score implements Result:
score /int
constructor .score:
class Position implements Result:
position /int
constructor .position:
COMPLETE ::= {
')': 1,
']': 2,
'}': 3,
'>': 4,
}
class CompleterScorer:
score /int := 0
add char/int -> none:
score *= 5
score += COMPLETE[char]