50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
case 'X': row_pos += deltarow;
col_pos += deltacol;
break;
}
}
printf("The guard visits %u positions before leaving the area.\n", visited);
// Part Two
free(savedmap);
}
/* === aoc202405 =======================================================
===================================================================== */
static int ppcmp(unsigned a, unsigned b, unsigned (*r)[2], size_t nr) {
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
|
case 'X': row_pos += deltarow;
col_pos += deltacol;
break;
}
}
printf("The guard visits %u positions before leaving the area.\n", visited);
// Part Two
unsigned rowblock = 0, colblock = 0, positions = 0;
int row_initial = (int)(linear_pos / size);
int col_initial = (int)(linear_pos % size);
for (;;) {
strcpy(data, savedmap);
if (data[linearize2d(size, (unsigned)rowblock, (unsigned)colblock)] == '.') {
data[linearize2d(size, (unsigned)rowblock, (unsigned)colblock)] = '#'; // block it
row_pos = row_initial; col_pos = col_initial;
deltarow = -1; deltacol = 0;
data[linearize2d(size, (unsigned)row_pos, (unsigned)col_pos)] = '1';
bool looping = false;
while (!looping && validpos(size, row_pos + deltarow, col_pos + deltacol)) {
switch (data[linearize2d(size, (unsigned)(row_pos+deltarow), (unsigned)(col_pos+deltacol))]) {
default: puts("stepped on default"); exit(EXIT_FAILURE);
case '#': if (data[linearize2d(size, (unsigned)(row_pos), (unsigned)(col_pos))] == '8') looping = true;
rightturn(&deltarow, &deltacol);
break;
case '8': looping = true; break;
case '.': data[linearize2d(size, (unsigned)(row_pos+deltarow), (unsigned)(col_pos+deltacol))] = '0';
/*fallthrough*/
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7': data[linearize2d(size, (unsigned)(row_pos+deltarow), (unsigned)(col_pos+deltacol))]++;
row_pos += deltarow;
col_pos += deltacol;
break;
}
}
if (looping) positions++;
}
colblock++;
if (colblock == size) {
colblock = 0;
rowblock++;
if (rowblock == size) break;
}
}
printf("You can obstruct %u different positions.\n", positions);
free(savedmap);
}
/* === aoc202405 =======================================================
===================================================================== */
static int ppcmp(unsigned a, unsigned b, unsigned (*r)[2], size_t nr) {
|