-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.c3
87 lines (83 loc) · 1.9 KB
/
day9.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
module day9;
import std::io;
import std::math;
const MAP_SIZE = 400;
struct State
{
bool[MAP_SIZE][MAP_SIZE] map;
int[<2>] head;
int[<2>][] tail;
}
fn void process_line(State* state, char dir, int steps)
{
for (int s = 0; s < steps; s++)
{
switch (dir)
{
case 'R': state.head[0] += 1;
case 'L': state.head[0] -= 1;
case 'D': state.head[1] -= 1;
case 'U': state.head[1] += 1;
default: unreachable();
}
assert(state.head[0] > -MAP_SIZE / 2 && state.head[1] > -MAP_SIZE / 2 && state.head[0] < MAP_SIZE / 2 && state.head[1] < MAP_SIZE / 2);
int tail_parts = state.tail.len;
int[<2>] last_part = state.head;
for (int i = 0; i < tail_parts; i++)
{
int[<2>] tail = state.tail[i];
int[<2>] diff = last_part - tail;
int x_diff = math::abs(diff[0]);
int y_diff = math::abs(diff[1]);
switch
{
case x_diff > 1 && y_diff > 1:
assert(y_diff == 2 && x_diff == 2);
tail[0] += diff[0] / 2;
tail[1] += diff[1] / 2;
case x_diff > 1:
assert(y_diff < 2 && x_diff == 2);
tail[0] += diff[0] / 2;
tail[1] += diff[1];
case y_diff > 1:
assert(x_diff < 2 && y_diff == 2);
tail[0] += diff[0];
tail[1] += diff[1] / 2;
default:
break;
}
state.tail[i] = tail;
last_part = tail;
if (i == tail_parts - 1)
{
state.map[tail[0] + MAP_SIZE / 2][tail[1] + MAP_SIZE / 2] = true;
}
}
}
}
macro void part(int $part)
{
State state;
state.map[MAP_SIZE / 2][MAP_SIZE / 2] = true;
int[<2>][$part == 1 ? 1 : 9] tail;
state.tail = &tail;
File f = file::open("moves.txt", "rb")!!;
defer (void)f.close();
while (!f.eof())
{
String line = io::treadline(&f)!!;
assert(line.len);
process_line(&state, line[0], line[2..].to_int()!!);
}
int sum = 0;
foreach (bool[MAP_SIZE]* &row : state.map)
{
foreach (val : *row) if (val) sum++;
}
io::printfn("Grids visited by the tail: %d", sum);
}
fn void main()
{
part(1);
part(2);
}