-
Notifications
You must be signed in to change notification settings - Fork 0
/
day24.c3
176 lines (165 loc) · 3.47 KB
/
day24.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
module day24;
import std::io;
import std::math;
import std::collections::list;
def CharsArray = List(<String>);
const int PADDING = 100;
const MAX_WIDTH = 120;
const MAX_HEIGHT = 40;
const MAX_PHASE = MAX_WIDTH * MAX_HEIGHT;
char[MAX_WIDTH][MAX_HEIGHT] map;
int[MAX_WIDTH][MAX_HEIGHT][MAX_PHASE] map_cost;
bool[MAX_WIDTH][MAX_HEIGHT][MAX_PHASE] blizzard;
int width;
int height;
int common;
fn void load_map()
{
File f = file::open("blizzard.txt", "rb")!!;
defer (void)f.close();
@pool()
{
String line = io::treadline(&f)!!;
width = line.len - 2;
};
height = 0;
while (!f.eof())
{
@pool()
{
String line = io::treadline(&f)!!;
if (line[1] == '#') break;
line = line[1..^2];
foreach (int i, c : line)
{
map[height][i] = c;
}
height++;
};
}
assert(MAX_HEIGHT > height);
assert(MAX_WIDTH > width);
int a = width;
int b = height;
while (a != b)
{
if (a > b)
{
a -= b;
}
else
{
b -= a;
}
}
common = width * height / a;
assert(common < 1000);
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
switch (map[i][j])
{
case '.':
continue;
case '^':
for (int it = 0; it < common; it++)
{
int off = it % (height * 2);
blizzard[it][(i - off + height * 2) % height][j] = true;
}
case 'v':
for (int it = 0; it < width * height; it++)
{
blizzard[it][(i + it) % height][j] = true;
}
case '>':
for (int it = 0; it < width * height; it++)
{
blizzard[it][i][(j + it) % width] = true;
}
case '<':
for (int it = 0; it < width * height; it++)
{
int off = it % (width * 2);
blizzard[it][i][(j + width * 2 - off) % width] = true;
}
default:
unreachable("Invalid element");
}
}
}
}
fn void explore(int[<2>] loc, int round)
{
if (round > 10000) return;
if (loc.comp_lt({ 0, 0 }).or() || loc.comp_ge({width, height}).or()) return;
int seq = round % common;
bool is_blizzard = blizzard[seq][loc[1]][loc[0]];
if (is_blizzard) return;
int current_cost = map_cost[seq][loc[1]][loc[0]];
if (current_cost > 0 && current_cost <= round) return;
map_cost[seq][loc[1]][loc[0]] = round;
round++;
for (int i = 0; i < 5; i++)
{
switch (i)
{
case 0:
explore(loc + { 0, 1 }, round);
case 1:
explore(loc + { 0, -1 }, round);
case 2:
explore(loc + { 1, 0 }, round);
case 3:
explore(loc + { -1, 0 }, round);
case 4:
explore(loc, round);
default:
unreachable();
}
}
}
fn void solve()
{
int round = 0;
while (blizzard[round % common][0][0]) round++;
explore({ 0, 0 }, round);
int min = 1000000000;
for (int i = 0; i < common; i++)
{
int cost = map_cost[i][height - 1][width - 1];
if (cost > 0 && cost < min) min = cost;
}
io::printfn("Cost: %d", min + 1);
mem::clear(&map_cost, int.sizeof * MAX_PHASE * MAX_HEIGHT * MAX_WIDTH);
round = min + 1;
for (int i = 0; i < common; i++)
{
explore({ width - 1, height - 1 }, round + i);
}
min = 999999999;
for (int i = 0; i < common; i++)
{
int cost = map_cost[i][0][0];
if (cost > 0 && cost < min) min = cost;
}
round = min + 1;
mem::clear(&map_cost, int.sizeof * MAX_PHASE * MAX_HEIGHT * MAX_WIDTH);
for (int i = 0; i < common; i++)
{
explore({ 0, 0 }, round + i);
}
min = 1000000000;
for (int i = 0; i < common; i++)
{
int cost = map_cost[i][height - 1][width - 1];
if (cost > 0 && cost < min) min = cost;
}
io::printfn("Cost: %d", min + 1);
}
fn void main()
{
load_map();
solve();
}