Skip to content

Instantly share code, notes, and snippets.

@slaght
Last active December 12, 2024 23:24
Show Gist options
  • Save slaght/3e3c4ee3b219005dad2f5f8c916a9172 to your computer and use it in GitHub Desktop.
Save slaght/3e3c4ee3b219005dad2f5f8c916a9172 to your computer and use it in GitHub Desktop.
2024 Advent of Code Day 6 in Zig
const std = @import("std");
const map = [_][]const u8{ "..................#................................................................#........#.....................................", "...#...........#...................................................#........................................#.................#...", "...................................#................#.#...............#.................................................#.........", ".....#......#................#.....................................................................#..........#........#......#...", "............................................................................................#.....................#...............", ".....................#..##...#........................#.................#.......................#..#.........#.......#...#......#.", "............##....................................##..................#...............................#....#......................", ".............#............#.#..#.......#...........#..............#...............#.....#.........................................", "....................#..........##.........#.........#................#............................................................", "....#.......#................................................................#...#.#..........................................#.#.", "..#..............#.....................................#..........#.#....#..............#..................#.#............#.......", ".#.......#.........................#........................#..#.....#............................................................", "........#...#......................#...........#......................................#....................#......................", ".............................#..............#........##....#....................#......#....................#............#......#.", "..............#..............#............#.......................##....#..........................................#.........#....", ".............#............#..........#..........#...#.....................................................#.......................", "..#........#.....#..........................................#................................................#...#.#...#..........", "........#.....................#...#..#......#.........................................................#.....##.........#..........", "..............................................................................................#...#...............................", "..#.......................#..........................#......#...................................................................#.", ".......#.........................#..............#.........#.............#.......................................#...#.............", "..................#.................#.......................#....................#....................................#...........", "...#...#.......#......##.#...............#....#..............#..........................................#....#..#.................", "#................................#.......#.....#.......#.............#..................#........#................................", "...........#..................................#.......................................................................#.#..#......", ".......#........#.........................................#..........................#...........................#.....#..........", "................#..#.....................#..#...........................#...........................................#.............", "#...#.......................#................................................#................#................................#..", "..........#...........................#.......#............................................................................#.....#", "..................................#............................................................#..................................", "......................#........#.............#....#.....#.......#..........................#..........................#...........", "...........................##.................#.............#........................#...#..................#.....................", ".............................................................................................................#....#..........#....", "................#...................................#...........#..........#....................#............................#....", "........#....#................................................#................##..#..................................#...........", "...........#....................................................#...#.#......#....................................................", ".......................#.........................................................^....................................#...........", "..........................#.............##..#........#.#....#.......#....................#...........#.........#.....#............", "........#...........................#..#..........................................................#...............................", "....#....................................#....#.........................................#.........#...............................", "...............#...................................#.....#.................................#....................................#.", "......#...............................................................#.............................#........................#....", ".........................#.....#.........................#.#...............#.........#.....#..................#..#.........#......", ".........................................................#......#........##.......................#...#......#................#...", ".......................................#.....#.................................#..................................................", "..................#..................................#..........................................................................#.", "...................#....#.........#.......#................#....................#.........................#......#.....#..#.......", "................................#..............................................................................#....#.............", ".....................#.#.............#.................#..........#.......#.........................................#.............", "......#....................#....................................................................#...#.............................", "........#........#...........#.##.............#........................#............#..............#.........#....#........#......", "........................#.................................................................#.......................................", "............................................................#.....#....................#............#.....#....#............#.....", "...........#............#....#........................................................##............#..............#..............", "...........#...........#.....#..............#..............##....#........#......#................................................", "..#................................................................................#......................#......#................", "....................#.........#......................#.............................#.......#.......................#.............#", "..#...#....#......................................................................................................#...............", "...........#................................................................................#...............................#.....", "....................................#............#..........#.........................#..................................#.......#", "...................#......#...................#............#......#........#................#.............#...........#.........#.", "........................#..................................#........#..................#..........................................", "............#..#...........#..................##....................#...........................................#.................", ".......................................................................................#.....#............#.......................", "...............................................#....#................................#..#..........................#..#...........", "..............#..#.....#.........#....................#.......#..#..............#...............................#.....#...........", "..........#............................#..........................................................................................", "........................................#........#........................................................#.#.....................", "#................#......................#..................................................................................#......", "...............................................................................#................................#.................", "...................................................................................................................#.#........#...", "#........................................................................#......................................#.#...............", "...#......#............#...................#.............#........#..........................................#......#.............", "..............##....#...................................................#......................#..................................", "...............#........................................................................#...........#...#.#.......................", "..........#.#.....#................................#...........#........#.......#.................................................", "#....................#......................................................................................#.....................", "..............#...##...........................................#.............#.......#..................#..............#..........", ".#.............................................#..............................................................................#...", "...#................................#............................................#............................#...................", "..............................#......................................................#..........................................#.", "...................#.....................................#..................................#......#..............................", "....#.#..........##.............#.......#........................##..........#.................#..................................", "#..#................#........##....................................................................................#..............", "........#...#.........................................................................#...........................................", "......................................................#....#...................#..#.................................#.............", "........#...............#........#..............................................#............................#....................", "...#..............................#.........................................##.......................................#....#.......", "...#................#..............#.......#......................................................................................", "........#..................#.............................................................#...........................#....#.......", "......................#.#..........#.......#..................................#..#...#............#...............#...............", ".....#..................#....#..#......#.......................#.....................................................#............", ".....#................................#.........................#..................#.#..........#...#............#.....#..........", "....#...................#....#.#.........#........#..................................................................#....#..#....", "........#...............#......................................#........#..............................#..........................", "....#...#.#.....#..............#..................#..........#...#..#......................#...#..........#......................#", "...........#...................................................................................................#................#.", ".................................##..............#.............................#...............................#..................", "...........#.................#.............##...................................#.......................#.................##......", ".....#..................#......#........#.................................................#..#........................#..#........", "......#.......#......................................................................#.#.........#....#...........................", "..................#.#........................#......#................................................................#......#.....", ".....................#............#...................#.....#....................................#.......#..........#.......#.....", "#.............................#...............................#...#...............................................................", "..##.........#........#......................................#.....................#...#.............#............#..#............", "..........#.....................#..#....................#...#....................#....................#.....................#.....", "...........#..........#...............#..#..........#..............................................#.#............................", "............#...#...#.............#.........#.............................#...............#.......................................", ".........#...........#...............................................................#............................................", ".#..#.................................................................#............#.#.......................##...................", "........##............#...................#................................#......................................................", "............................................#..........................#..........................................................", "#...................#...........#..............#...#............#.#........#.................#......#.............................", "........#....................................#....................................................................................", "...........#.........#..#...............#...........#.....##.#......#....##............#....#....#.#.........##.........#.........", "..#......................#..........#.................#.................#......#.............................#........#...........", "....................#.................#....#..............................#....#.............#....#....#.....................#....", "............#.................#.................................#...............................#..#...........................#..", "...................................................#.....#..............#.......#....................#.#..........................", "..#.........##..#........#...................................................................................#............#.......", ".....#..............................#......................................................................#........#.............", "................................#........##.......#...............#..............#.......#...#.#........#.........................", "..............................#.....................#.........................................#..................................#", "...........#...................##.................#.................................#...............................#........#....", "..............................#...##......#..................#...#................................#......#.......#...............#", ".....................##......#......#.#.......#..............#......#.................#.........#...................#.............", "..........................#................................#...........#..........................................................", "...#.....#....................#.....................#...#.....#.............................#.#....#.....#.#.................#....", "............................#...#........#......................................................................#.....#...........", "............#..........##..................#.............................................................#.....#..#..............." };
const point = struct { i: usize, j: usize };
fn checkMapForTurns() !void {
var guard: point = .{ .i = 0, .j = 0 };
var path: [map.len][map[0].len]u8 = undefined;
var direction: u8 = 'N';
var visits: u32 = 0;
for (0..map.len) |i| {
for (0..map[0].len) |j| {
if (map[i][j] == '^') {
guard.i = i;
guard.j = j;
path[i][j] = ',';
}
}
}
while (guard.i < map.len and guard.j < map.len and guard.i > 0 and guard.j > 0) {
switch (direction) {
'N' => {
if (map[guard.i - 1][guard.j] == '#') {
direction = 'E';
} else {
guard.i -= 1;
}
},
'E' => {
if (map[guard.i][guard.j + 1] == '#') {
direction = 'S';
} else {
guard.j += 1;
}
},
'S' => {
if (map[guard.i + 1][guard.j] == '#') {
direction = 'W';
} else {
guard.i += 1;
}
},
'W' => {
if (map[guard.i][guard.j - 1] == '#') {
direction = 'N';
} else {
guard.j -= 1;
}
},
else => {},
}
path[guard.i][guard.j] = ',';
}
for (0..path.len) |i| {
for (0..path[0].len) |j| {
if (path[i][j] == ',') {
visits += 1;
}
}
}
std.debug.print("Total visits for map: {d}\n", .{visits});
}
fn checkMapForObstacles() !void {
var obstacles: u32 = 0;
for (0..map.len) |p| {
for (0..map[0].len) |q| {
if (map[p][q] == '.') {
var guard: point = .{ .i = 0, .j = 0 };
var path: [map.len][map[0].len]u8 = undefined;
var direction: u8 = 'N';
var steps: u32 = 0;
path[p][q] = 'O';
for (0..map.len) |i| {
for (0..map[0].len) |j| {
if (map[i][j] == '^') {
guard.i = i;
guard.j = j;
path[i][j] = direction;
}
}
}
while (guard.i < map.len - 1 and guard.j < map.len - 1 and guard.i > 0 and guard.j > 0) {
switch (direction) {
'N' => {
if (map[guard.i - 1][guard.j] == '#' or path[guard.i - 1][guard.j] == 'O') {
direction = 'E';
} else {
guard.i -= 1;
}
},
'E' => {
if (map[guard.i][guard.j + 1] == '#' or path[guard.i][guard.j + 1] == 'O') {
direction = 'S';
} else {
guard.j += 1;
}
},
'S' => {
if (map[guard.i + 1][guard.j] == '#' or path[guard.i + 1][guard.j] == 'O') {
direction = 'W';
} else {
guard.i += 1;
}
},
'W' => {
if (map[guard.i][guard.j - 1] == '#' or path[guard.i][guard.j - 1] == 'O') {
direction = 'N';
} else {
guard.j -= 1;
}
},
else => {},
}
if (path[guard.i][guard.j] == direction or steps > map.len * map[0].len) {
obstacles += 1;
break;
} else {
steps += 1;
path[guard.i][guard.j] = direction;
}
}
}
}
}
std.debug.print("Total looping obstacles for map: {d}\n", .{obstacles});
}
pub fn main() !void {
_ = try checkMapForTurns();
_ = try checkMapForObstacles();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment