-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday14.toit
63 lines (48 loc) · 1.67 KB
/
day14.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
import .file as file
import reader show BufferedReader
import bytes show Buffer
// Increments the value for a given key in the map, starting at 0 if the key
// was not present.
inc map/Map key --by/int=1 -> none:
map[key] = (map.get key --if_absent=(: 0)) + by
main:
reader := BufferedReader
file.Stream.for_read "14.txt"
polymer := reader.read_line
pair_counts := {:}
for x := 0; x < polymer.size - 1; x++:
inc pair_counts polymer[x..x + 2]
last_char := polymer[polymer.size - 1]
reader.read_line
insertion_rules := {:}
while line := reader.read_line:
parts := line.split " -> "
lhs /string := parts[0]
rhs /string := parts[1]
insertion_rules[lhs] = [
lhs[0..1] + rhs,
rhs + lhs[1..2],
]
step_function := : | previous_pair_counts |
new_pair_counts := {:}
previous_pair_counts.do: | pair count |
if insertion_rules.contains pair:
insertion_rules[pair].do:
inc new_pair_counts it --by=count
else:
inc new_pair_counts pair --by=count
new_pair_counts // Last expression is return value of block.
10.repeat:
pair_counts = step_function.call pair_counts
print_most_minus_least_frequent pair_counts last_char
30.repeat:
pair_counts = step_function.call pair_counts
print_most_minus_least_frequent pair_counts last_char
print_most_minus_least_frequent pair_counts/Map last_char/int -> none:
element_counts := {last_char: 1}
pair_counts.do: | pair count |
first := pair[0]
inc element_counts first --by=count
biggest := element_counts.values.reduce: | a b | max a b
smallest := element_counts.values.reduce: | a b | min a b
print biggest - smallest