-
Notifications
You must be signed in to change notification settings - Fork 0
/
14.zote
134 lines (116 loc) · 2.5 KB
/
14.zote
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
include!("stdlib");
include!("aoc.zote");
input := read("input");
lines := input >> split("\n");
cubes := set();
rocks := set();
rows := len(lines);
cols := len(lines[0]);
for (row, line) in lines >> enumerate for (col, char) in line >> enumerate {
if char == '#' {
[row, col] >> insert(cubes);
} else if char == 'O' {
[row, col] >> insert(rocks);
}
}
// Four almost identical functions for moving. Could be one, but don't know the performance penalty for the extra if, and I wanted speed
fn move_up() -> {
next := set();
move := false;
for (i, rock) in rocks >> keys >> enumerate {
(row, col) := rock;
if row != 0 {
up := [row - 1, col];
if !(up >> in(cubes)) and !(up >> in(rocks)) {
up >> insert(next);
move = true;
} else {
rock >> insert(next);
}
} else {
rock >> insert(next);
}
}
rocks = next;
move
}
fn move_left() -> {
next := set();
move := false;
for (i, rock) in rocks >> keys >> enumerate {
(row, col) := rock;
if col != 0 {
left := [row, col-1];
if !(left >> in(cubes)) and !(left >> in(rocks)) {
left >> insert(next);
move = true;
} else {
rock >> insert(next);
}
} else {
rock >> insert(next);
}
}
rocks = next;
move
}
fn move_down() -> {
next := set();
move := false;
for (i, rock) in rocks >> keys >> enumerate {
(row, col) := rock;
if row != rows-1 {
down := [row + 1, col];
if !(down >> in(cubes)) and !(down >> in(rocks)) {
down >> insert(next);
move = true;
} else {
rock >> insert(next);
}
} else {
rock >> insert(next);
}
}
rocks = next;
move
}
fn move_right() -> {
next := set();
move := false;
for (i, rock) in rocks >> keys >> enumerate {
(row, col) := rock;
if col != cols-1 {
right := [row, col+1];
if !(right >> in(cubes)) and !(right >> in(rocks)) {
right >> insert(next);
move = true;
} else {
rock >> insert(next);
}
} else {
rock >> insert(next);
}
}
rocks = next;
move
}
fn load() -> rocks >> keys >> map(\>> [0] >> sub(rows) >> neg) >> sum;
start := 0;
pattern := [];
for i in [0:1000] {
while move_up() {};
if i == 0 print(load());
while move_left() {};
while move_down() {};
while move_right() {};
load() >> push(pattern);
// Try to find a pattern (of sufficient length) repeating twice.
if i%2 == 1 and i > 100 for l in [25:i/2] {
if pattern[-l:] == pattern[-2*l:-l] {
// Extrapolate the answer from the pattern repeating to infinity
free := (1000000000 - i - 2) % l;
print(pattern[-l:][free]);
return
}
}
}