-
Notifications
You must be signed in to change notification settings - Fork 0
/
day23.c
137 lines (115 loc) · 3.73 KB
/
day23.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
#include "advent_of_code.h"
ARRAY_DEF(array_u16, uint16_t)
static inline uint16_t ptu(uint8_t x, uint8_t y) {
return (y << 8) | x;
}
static inline uint8_t utx(uint16_t u) {
return u & 0xFF;
}
static inline uint8_t uty(uint16_t u) {
return utx(u >> 8);
}
static inline uint16_t add(uint16_t p, uint16_t q) {
return ptu(utx(p) + utx(q), uty(p) + uty(q));
}
void solve_day23(AdventOfCode* aoc, Stream* file_stream) {
int p1 = 0;
int p2 = 0;
int8_t* grid;
grid = malloc(1 + UINT16_MAX);
memset(grid, -1, 1 + UINT16_MAX);
array_u16_t elves;
array_u16_init(elves);
FuriString* line = furi_string_alloc();
uint8_t offset = 0;
for(uint8_t i = 128;; i++) {
if(!stream_read_line(file_stream, line)) break;
furi_string_trim(line);
if(offset == 0) {
offset = furi_string_size(line) / 2;
i -= offset;
}
for(uint8_t j = 0; j < furi_string_size(line); j++)
if(furi_string_get_char(line, j) == '#') {
uint16_t elf = ptu(j + 128 - offset, i);
grid[elf] = 0;
array_u16_push_back(elves, elf);
}
}
furi_string_free(line);
const uint16_t steps[8] = {
0xFFFF, // NW
0xFF00, // N
0xFF01, // NE
0x0001, // E
0x0101, // SE
0x0100, // S
0x01FF, // SW
0x00FF, // W
};
const int dirs[4] = {1, 5, 7, 3};
int first_dir = 0;
array_u16_it_t it;
for(p2 = 1;; p2++) {
for(array_u16_it(it, elves); !array_u16_end_p(it); array_u16_next(it)) {
uint16_t elf = *array_u16_ref(it);
bool alone = true;
int mv = 0;
for(int i = first_dir; i < first_dir + 4 && (alone || mv == 0); i++) {
int d = dirs[i % 4];
bool free = true;
for(int j = d - 1; j <= d + 1; j++) {
if(grid[add(elf, steps[j % 8])] >= 0) {
alone = false;
free = false;
break;
}
}
if(free && mv == 0) mv = d;
if(free && !alone) break;
}
if(alone || mv == 0) {
continue;
}
uint16_t n = add(elf, steps[mv]);
int8_t other = grid[add(n, steps[mv])];
if(other >= 0 && abs(mv - other) == 4)
grid[add(n, steps[mv])] = 0;
else
grid[elf] = mv;
}
int moved = 0;
for(array_u16_it(it, elves); !array_u16_end_p(it); array_u16_next(it)) {
uint16_t* elf = array_u16_ref(it);
int8_t d = grid[*elf];
if(d == 0) continue;
moved++;
grid[*elf] = -1;
*elf = add(*elf, steps[d]);
grid[*elf] = 0;
}
if(moved == 0) break;
first_dir++;
first_dir %= 4;
if(p2 != 10) continue;
int minx = 0xFF, maxx = 0;
int miny = 0xFF, maxy = 0;
for(array_u16_it(it, elves); !array_u16_end_p(it); array_u16_next(it)) {
uint16_t elf = *array_u16_ref(it);
uint8_t x = utx(elf), y = uty(elf);
if(x < minx) minx = x;
if(x > maxx) maxx = x;
if(y < miny) miny = y;
if(y > maxy) maxy = y;
}
p1 = (maxx - minx + 1) * (maxy - miny + 1) - array_u16_size(elves);
FuriString* part_1 = furi_string_alloc_printf("%d", p1);
update_results(aoc, part_1, NULL);
furi_string_free(part_1);
}
FuriString* part_2 = furi_string_alloc_printf("%d", p2);
update_results(aoc, NULL, part_2);
furi_string_free(part_2);
array_u16_clear(elves);
free(grid);
}