-
Notifications
You must be signed in to change notification settings - Fork 0
/
day09.c
147 lines (122 loc) · 3.62 KB
/
day09.c
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
#include "advent_of_code.h"
typedef struct {
int x, y;
} Point;
int pt_to_i(Point pt, int w) {
return pt.x + w * pt.y;
}
bool follow(Point* knots, int i, int nb) {
bool moved = false;
if(knots[i - 1].y - knots[i].y > 1) {
knots[i].y++;
moved = true;
} else if(knots[i - 1].y - knots[i].y < -1) {
knots[i].y--;
moved = true;
}
if(moved) {
if(knots[i - 1].x > knots[i].x)
knots[i].x++;
else if(knots[i - 1].x < knots[i].x)
knots[i].x--;
} else {
if(knots[i - 1].x - knots[i].x > 1) {
knots[i].x++;
moved = true;
} else if(knots[i - 1].x - knots[i].x < -1) {
knots[i].x--;
moved = true;
}
if(moved) {
if(knots[i - 1].y > knots[i].y)
knots[i].y++;
else if(knots[i - 1].y < knots[i].y)
knots[i].y--;
}
}
if(!moved) return false;
if(i >= nb - 1) return true;
return follow(knots, i + 1, nb);
}
int num_visited(Stream* file_stream, Point start, int w, int h, int nb) {
FuriString* line = furi_string_alloc();
stream_rewind(file_stream);
Point knots[nb];
for(int i = 0; i < nb; i++) knots[i] = start;
bitset_t visited;
bitset_init(visited);
bitset_resize(visited, w * h);
bitset_set_at(visited, pt_to_i(start, w), true);
while(true) {
if(!stream_read_line(file_stream, line)) break;
furi_string_trim(line);
char c = furi_string_get_char(line, 0);
int steps = strtoul(furi_string_get_cstr(line) + 1, NULL, 10);
for(int i = 0; i < steps; i++) {
switch(c) {
case 'U':
knots[0].y++;
break;
case 'D':
knots[0].y--;
break;
case 'L':
knots[0].x--;
break;
case 'R':
knots[0].x++;
break;
}
if(follow(knots, 1, nb)) {
bitset_set_at(visited, pt_to_i(knots[nb - 1], w), true);
}
}
}
int resp = bitset_popcount(visited);
bitset_clear(visited);
furi_string_free(line);
return resp;
}
void solve_day09(AdventOfCode* aoc, Stream* file_stream) {
FuriString* line = furi_string_alloc();
Point head = {0};
int minx = 0, maxx = 0, miny = 0, maxy = 0;
while(true) {
if(!stream_read_line(file_stream, line)) break;
furi_string_trim(line);
char c = furi_string_get_char(line, 0);
int steps = strtoul(furi_string_get_cstr(line) + 1, NULL, 10);
switch(c) {
case 'U':
head.y += steps;
if(head.y > maxy) maxy = head.y;
break;
case 'D':
head.y -= steps;
if(head.y < miny) miny = head.y;
break;
case 'L':
head.x -= steps;
if(head.x < minx) minx = head.x;
break;
case 'R':
head.x += steps;
if(head.x > maxx) maxx = head.x;
break;
}
}
head.x = -minx;
head.y = -miny;
int w = maxx - minx + 1;
int h = maxy - miny + 1;
int resp = num_visited(file_stream, head, w, h, 2);
FuriString* part_1 = furi_string_alloc_printf("%d", resp);
FuriString* part_2 = furi_string_alloc();
update_results(aoc, part_1, part_2);
resp = num_visited(file_stream, head, w, h, 10);
furi_string_printf(part_2, "%d", resp);
update_results(aoc, part_1, part_2);
furi_string_free(part_2);
furi_string_free(part_1);
furi_string_free(line);
}