Skip to content

Commit b1a1094

Browse files
committed
feat(2024): solution for day 6 part 2
1 parent 6d767b9 commit b1a1094

File tree

3 files changed

+160
-145
lines changed

3 files changed

+160
-145
lines changed

2024/SUMMARY.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,30 @@ done only once, but since re-ordering can result in a new conflict with another
7373
page ordering rule, you might need to do this more often until all page ordering
7474
rules are met!).
7575

76+
## Day 6
77+
**Part 1:** Solved part 1 right away. Using an integer encoded Dir enum (4
78+
values to represent up, right, down and left and convert that to a `@Vector(2,
79+
isize)` value with a helper method) helped a lot. A right turn is then just a
80+
`+1` on the dir integer modulo 4 (use `@intFromEnum`). My main work went into
81+
parsing the map and adding ANSI control sequence based animation. `moveGuard`
82+
takes the guards position and checks if it would hit an obstacle (`#` only for
83+
part 1), if so change the `dir` property with `turnRight`, otherwise move the
84+
guard into the current `dir` direction. Store all not-yet visited positions
85+
(used a copy of `buffer` first for this, after a fresh rewrite for part 2, I
86+
just used my then available `VectorSet`), the count of thees is the result.
87+
88+
**Part 2:** Initially I started implementing part 2 but didn't get the right
89+
result for my puzzle input (though it worked for the example input). So I
90+
left it for some time and came back after a few weeks to solve it. After
91+
wrapping my head around what I did before and where a potential bug could be, I
92+
went for a rewrite. Using my then available `VectorSet` to store the visited
93+
path of the guard and the history of the position + direction (encoded in a
94+
`@Vector(3, isize)`), I finally got the correct answer. With `VectorSet` it was
95+
quite easy: store each guards position/direction combination for each step in a
96+
`@Vector(3, isize)` and use `VectorSet.contains()` to check if the guard has
97+
been on that position before (with the same direction). If that's the case, the
98+
guard moves in a loop.
99+
76100
## Day 7
77101
**Part1:** parse the equations and use recursion to check if any of the options
78102
is valid.

2024/VectorSet.zig

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,18 @@ pub fn VectorSet(comptime size: usize, comptime T: type) type {
3131
}
3232
}
3333

34+
pub fn remove(self: *VectorSet(size, T), value: @Vector(size, T)) bool {
35+
return self.hash_map.remove(value);
36+
}
37+
3438
pub fn contains(self: VectorSet(size, T), value: @Vector(size, T)) bool {
3539
return self.hash_map.contains(value);
3640
}
3741

3842
pub fn getIndex(self: VectorSet(size, T), idx: usize) @Vector(size, T) {
3943
var it = self.hash_map.keyIterator();
40-
for (0..@max(idx -| 1, 0)) |_| {
44+
if (idx == 0) return it.next().?.*;
45+
for (0..idx) |_| {
4146
_ = it.next().?;
4247
}
4348
return it.next().?.*;

0 commit comments

Comments
 (0)