diff --git a/README.md b/README.md index d227751..4dacf48 100644 --- a/README.md +++ b/README.md @@ -12,24 +12,34 @@ Can be run with `zig run -O ReleaseFast benchmark.zig` | Day | Part | Mean (μs) | Min (μs) | Max (μs) | |-----|------|-------------------|----------|----------| -| 1 | 1 | +23 ± 3.74 | +22 | +92 | -| 1 | 2 | +23 ± 2.00 | +22 | +53 | -| 2 | 1 | +34 ± 2.45 | +32 | +83 | -| 2 | 2 | +261 ± 36.08 | +239 | +764 | -| 3 | 1 | +19 ± 1.41 | +18 | +38 | -| 3 | 2 | +18 ± 1.00 | +17 | +44 | -| 4 | 1 | +214 ± 29.29 | +202 | +536 | -| 4 | 2 | +215 ± 29.09 | +201 | +558 | -| 5 | 1 | +139 ± 36.43 | +117 | +540 | -| 5 | 2 | +153 ± 44.35 | +116 | +475 | -| 6 | 1 | +31 ± 2.83 | +28 | +101 | +| 1 | 1 | +25 ± 6.93 | +23 | +67 | +| 1 | 2 | +25 ± 4.69 | +24 | +50 | +| 2 | 1 | +35 ± 3.00 | +33 | +45 | +| 2 | 2 | +266 ± 15.17 | +251 | +329 | +| 3 | 1 | +25 ± 2.45 | +24 | +40 | +| 3 | 2 | +18 ± 2.00 | +17 | +28 | +| 4 | 1 | +217 ± 4.47 | +211 | +232 | +| 4 | 2 | +218 ± 5.10 | +212 | +239 | +| 5 | 1 | +175 ± 50.21 | +131 | +353 | +| 5 | 2 | +141 ± 22.93 | +130 | +252 | +| 6 | 1 | +32 ± 1.73 | +31 | +42 | | 6 | 2 | Too long ~60s | 0 | 0 | -| 7 | 1 | +182 ± 18.06 | +157 | +439 | +| 7 | 1 | +211 ± 16.19 | +189 | +288 | | 7 | 2 | Too long ~0.2s | 0 | 0 | -| 8 | 1 | +540 ± 53.40 | +506 | +895 | -| 8 | 2 | +828 ± 81.50 | +769 | +1333 | -| 9 | 1 | +1770 ± 215.91 | +1389 | +5250 | +| 8 | 1 | +511 ± 13.93 | +497 | +581 | +| 8 | 2 | +844 ± 46.60 | +805 | +1095 | +| 9 | 1 | +1514 ± 122.07 | +1307 | +1811 | | 9 | 2 | Too long ~0.6s | 0 | 0 | -| 10 | 1 | +38 ± 5.66 | +36 | +160 | -| 10 | 2 | +27 ± 2.45 | +27 | +64 | -| Total| | +4515 ± 565.65 | +3898 | +11425 | +| 10 | 1 | +37 ± 7.87 | +35 | +83 | +| 10 | 2 | +28 ± 5.57 | +27 | +62 | +| 11 | 1 | +2139 ± 291.76 | +1931 | +2989 | +| 11 | 2 | +5060 ± 159.47 | +4903 | +5580 | +| 12 | 1 | +44947 ± 1417.02 | +43224 | +49471 | +| 12 | 2 | +48279 ± 2536.03 | +45591 | +57175 | +| 13 | 1 | +1522 ± 119.51 | +1445 | +2178 | +| 13 | 2 | Too long ~6h | 0 | 0 | +| 14 | 1 | +137 ± 5.10 | +130 | +165 | +| 14 | 2 | Need input | 0 | 0 | +| 15 | 1 | +71 ± 41.96 | +42 | +182 | +| 15 | 2 | +200 ± 34.13 | +172 | +301 | +| Total | +106677 ± 4935.90 | +101385 | +123638 | diff --git a/benchmark.zig b/benchmark.zig index 71a13ea..497d895 100644 --- a/benchmark.zig +++ b/benchmark.zig @@ -25,6 +25,9 @@ const d112 = @import("day11/part2.zig"); const d121 = @import("day12/part1.zig"); const d122 = @import("day12/part2.zig"); const d131 = @import("day13/part1.zig"); +const d141 = @import("day14/part1.zig"); +const d151 = @import("day15/part1.zig"); +const d152 = @import("day15/part2.zig"); const NUMBER_OF_RUN = 50; @@ -76,6 +79,12 @@ pub fn main() !void { try benchmark(d131.main, 13, 1); print("| 13 | 2 | Too long ~6h | 0 | 0 |\n", .{}); separator(); + try benchmark(d141.main, 14, 1); + print("| 14 | 2 | Need input | 0 | 0 |\n", .{}); + separator(); + try benchmark(d151.main, 15, 1); + try benchmark(d152.main, 15, 2); + separator(); print("| Total | {d: >8} ± {d: <6.2} | {d:>8} | {d:>8} |\n", .{ total_mean, total_std_dev, total_min, total_max }); separator(); } diff --git a/day14/part1.zig b/day14/part1.zig index 52eeb3c..3c74c2c 100644 --- a/day14/part1.zig +++ b/day14/part1.zig @@ -38,7 +38,7 @@ pub fn main() !void { } } - print("Total: {d}\n", .{tl_quadrant * tr_quadrant * bl_quadrant * br_quadrant}); + try std.testing.expectEqual(218965032, tl_quadrant * tr_quadrant * bl_quadrant * br_quadrant); } fn posFinal(x0: Int, y0: Int, vX: Int, vY: Int, mapW: Int, mapH: Int, dt: Int) [2]Int { diff --git a/day15/input b/day15/input new file mode 100644 index 0000000..8c14283 --- /dev/null +++ b/day15/input @@ -0,0 +1,71 @@ +################################################## +#..O.O..#..O.O.O.O.......##O..OOO.....O...O..#..O# +#.O.O.#.....O..O......O.O.OOO.......OO.O.....#...# +#OO........O.....OO......##OO.#OO.OO..O....OOO...# +#......O...O..O.#....O...O.....O..#.O.O.O...#O...# +#OOO.OO.OO...####O.#O...O..OO...O#.O..#.OO.O...O.# +#O#...OOOO......O#O#....#OO...O.....O.O.OOO.OOO..# +#O.O...O.#OO......#...........O...OO.O..O#.#OO...# +#O.....O.O.O.#...OO..O#....OO.O.O....#.........O.# +#.O.#O.O#...O#..O.O.#.O.OO..O..O...O.O...OO.O...O# +#..#..O.#.O..##......OO...........O.....O...#.#..# +#.OO#.........O...O.....OO.O...OO............O#.O# +##.O.OO.O...O.O..O..OO.#....O.#....O......OOO...O# +#..OO..O.O..O..O..O...O..O.....#OO.....O...#OOO.O# +#.......O.#O.OO..#......O..OO.......O..O.O....OO## +#.....O.........O...#.O..O..#OO.O...O.O.#....O...# +#.OO##O#..O.#..O......OO..OO.OO.....O..OO#O.O..O## +#.O....O...#O.O..O#....O#O...OO.....O....O.......# +#...OOO....#O.OO...#.OO#O...O#O..#..#.O..O....#..# +##....O.OO.O#..........O#.OOO.#....#...O#....OO..# +#.#OOO..OO..O.......#.OO.O..#O.#.OO....OO.O.....O# +#.O......#..O.#.O..........#O..O.OO...#O...O#....# +#.#....O.O....O.......O..OOO..OO...OO.#OOO.O.O.#.# +#.O.#.O.O....OOO...O...O#......OO##.O..OO.....O..# +#.OO.......#O.OO#..#....@O...O....OOO.O..OO......# +#O..#..#OO...O#OO.......OO..##..O.#.O.O..O.O..OO.# +#.#.O.OO.........OO#.O..O.O...OOO.O......O#...OO.# +#.OO...#..OO.##......O#.O.OO....O..O#.......O.OO## +#OO..O.O.O....O#.O..O......#...OO#.O..O.O..O..OOO# +#...OO....OOO.....O..O..O....OO......OOO.........# +#O...#.....O...O..OO..O.OO....O.O..O....O....O.O.# +#.....OOOO..#..#.O.OO.......O....OO#.O...O..O..O.# +##.OO...O.#O......#O.O#O#..O#..O.....O....OO..O.## +#..O..OOO...OO.OO.#O#.OOO......O...OO...O.O......# +#.OO.O..O.O.O#..O.#.O.OOO...O..OO.O##.O.#...#...O# +#........#...O##.....OO....O.O....OO#.O.O.O.#OO.O# +#.O..O.O.#...O.......#...#..O...O.#.........#..O## +#...........O.....O..OO.......#..#...O...#..O.##O# +#.OOO..O..OO....#..OOOO......O...##..#.......O...# +##..#O..OOO.#O...O..OO...O....OO..#..#.OO.O.OO...# +##O....#...OO#.O.O...O...O.O..O.......O.....O..#.# +#........#.O##...O#..O..O.#O........O#..OO#.O.O### +#....#.O............O..O...OO....OOO...OO...#....# +#.#O.....#...OO.OO.........#.#.OOO.OO.O...O#.O.#.# +##....#..OO...O...O..#O..O.O..O.O..O..O.OO.......# +#...........OO.O.O...O......O...O.OOO.....O..O...# +##O.O..O.O...O.....O.O.O.O.OO.#O.O.O..#.....#....# +##.O.......#O.#...O#O#O..O..#O..O....OO.O.O......# +#O...O.O.........O......O.......O......#..O.....## +################################################## + +^>>^v><^>v>>^v^^^v<>>v>v^>>v><<<>^<vvvv^v^>v^>>^<>vv^>^>^v<^v^v<<<<^<>v<<>vv<>^vv<^v><^^>^>^v>>^>^v>>>v<<^>^>^<^<><^vvv<<^vvv>vv^><v^v^^^>v^<^v>^^<<>^><<<>>>^v^>vv><<>>v>vv<<>^v<>>v><>>^^<^^<^v>v^vv>>^^^>v><^>>>>^v>>vvv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<>><>>>v^vvv^v^v>v><^>>v<<^>^>>v^<<^>>><^<><^>>^>>^vv^<>v<^^>v>>><<>><<^<^v>^>^>^v^>>><^><<><^<<><<<>v^^^^vv<>vv<<^>>^^>>><^vvv>^<>^v^^>v^^v<>^^>^<>><><^^<^^^^<^^<>><>>^><^^>^^><^<><>>^^^<<^<^^^>><^^>v<>^^^^>^^^>>v>^^>^v<><<^<>vv<>^^><^vv><>>>^<<>><>v>^<>^<>^>^>v>>v>>>>^^><^^^^^><<^>^^<<<v<^vvv<>>vv<^^<^^vvv^^^^v<<<^<^>v^vv<<^>vv>^<>^>vv^>v>v<>^<<>^^v>^^v>>>^vv>>^>>>><>>^v^^<>^v^^v<><^>^>><^<<^>>>vvv>vv^>v>>^^>^>vv>vv<<^><^vv^>v<<>^^<<><>^v>vv^v^><<^<<^^<>>v>^^<><^<^<>^>v><>>><>>v>>v>^v<<>v^<>^vvv<><<<^<<^<>v^>< diff --git a/day15/part1.zig b/day15/part1.zig new file mode 100644 index 0000000..61089fd --- /dev/null +++ b/day15/part1.zig @@ -0,0 +1,137 @@ +const std = @import("std"); +const print = std.debug.print; +const input = @embedFile("input"); +const MAP_SIZE = 50; + +const Cell = enum { Empty, Box, Wall, Robot }; +const Direction = enum { Top, Bot, Left, Right }; + +const Position = struct { x: usize, y: usize }; + +pub fn main() !void { + var map: [MAP_SIZE][MAP_SIZE]Cell = undefined; + var robot_position: Position = undefined; + + for (input[0 .. MAP_SIZE * (MAP_SIZE + 1)], 0..) |c, i| { + if (c == '\n') continue; + map[@divFloor(i, MAP_SIZE + 1)][i % (MAP_SIZE + 1)] = switch (c) { + '.' => .Empty, + '#' => .Wall, + 'O' => .Box, + '@' => .Robot, + else => unreachable, + }; + if (c == '@') robot_position = Position{ .x = @divFloor(i, MAP_SIZE + 1), .y = i % (MAP_SIZE + 1) }; + } + + for (input[MAP_SIZE * (MAP_SIZE + 1) ..]) |c| switch (c) { + '>' => next(&map, &robot_position, .Right), + '<' => next(&map, &robot_position, .Left), + '^' => next(&map, &robot_position, .Top), + 'v' => next(&map, &robot_position, .Bot), + else => {}, + }; + + try std.testing.expectEqual(1487337, calculateSumPosition(map)); +} + +// The copy paste suck but if I want to use -1 to do dx dy, I need to use something else than usize and it fuck up the indexing +fn next(map: *[MAP_SIZE][MAP_SIZE]Cell, robot_position: *Position, direction: Direction) void { + const x = robot_position.x; + const y = robot_position.y; + + switch (direction) { + .Top => switch (map[x - 1][y]) { + .Empty => { + map[x][y] = .Empty; + map[x - 1][y] = .Robot; + robot_position.*.x -= 1; + }, + .Wall => {}, + .Box => if (detectFirstEmpty(map.*, robot_position.*, direction)) |first_empty| { + map[first_empty.x][first_empty.y] = .Box; + map[x][y] = .Empty; + map[x - 1][y] = .Robot; + robot_position.*.x -= 1; + }, + else => unreachable, + }, + .Bot => switch (map[x + 1][y]) { + .Empty => { + map[x][y] = .Empty; + map[x + 1][y] = .Robot; + robot_position.*.x += 1; + }, + .Wall => {}, + .Box => if (detectFirstEmpty(map.*, robot_position.*, direction)) |first_empty| { + map[first_empty.x][first_empty.y] = .Box; + map[x][y] = .Empty; + map[x + 1][y] = .Robot; + robot_position.*.x += 1; + }, + else => unreachable, + }, + .Left => switch (map[x][y - 1]) { + .Empty => { + map[x][y] = .Empty; + map[x][y - 1] = .Robot; + robot_position.*.y -= 1; + }, + .Wall => {}, + .Box => if (detectFirstEmpty(map.*, robot_position.*, direction)) |first_empty| { + map[first_empty.x][first_empty.y] = .Box; + map[x][y] = .Empty; + map[x][y - 1] = .Robot; + robot_position.*.y -= 1; + }, + else => unreachable, + }, + .Right => switch (map[x][y + 1]) { + .Empty => { + map[x][y] = .Empty; + map[x][y + 1] = .Robot; + robot_position.*.y += 1; + }, + .Wall => {}, + .Box => if (detectFirstEmpty(map.*, robot_position.*, direction)) |first_empty| { + map[first_empty.x][first_empty.y] = .Box; + map[x][y] = .Empty; + map[x][y + 1] = .Robot; + robot_position.*.y += 1; + }, + else => unreachable, + }, + } +} + +fn detectFirstEmpty(map: [MAP_SIZE][MAP_SIZE]Cell, start: Position, direction: Direction) ?Position { + var x = start.x; + var y = start.y; + if (direction == .Top) x -= 1; + if (direction == .Bot) x += 1; + if (direction == .Left) y -= 1; + if (direction == .Right) y += 1; + while (x != 0 and x != MAP_SIZE - 1 and y != 0 and y != MAP_SIZE - 1) : ({ + if (direction == .Top) x -= 1; + if (direction == .Bot) x += 1; + if (direction == .Left) y -= 1; + if (direction == .Right) y += 1; + }) { + switch (map[x][y]) { + .Empty => return Position{ .x = x, .y = y }, + .Box => continue, + .Wall => return null, + .Robot => unreachable, + } + } + return null; +} + +fn calculateSumPosition(map: [MAP_SIZE][MAP_SIZE]Cell) usize { + var total: usize = 0; + for (map, 0..) |row, x| for (row, 0..) |cell, y| switch (cell) { + .Box => total += x * 100 + y, + else => {}, + }; + return total; +} diff --git a/day15/part2.zig b/day15/part2.zig new file mode 100644 index 0000000..6b3b58f --- /dev/null +++ b/day15/part2.zig @@ -0,0 +1,192 @@ +const std = @import("std"); +const print = std.debug.print; +const input = @embedFile("input"); +const MAPW = 50 * 2; +const MAPH = 50; + +const Cell = enum { Empty, BoxL, BoxR, Wall, Robot }; +const Direction = enum { Top, Bot, Left, Right }; + +const Position = struct { x: usize, y: usize }; + +pub fn main() !void { + var map: [MAPH][MAPW]Cell = undefined; + var robot_position: Position = undefined; + + for (input[0 .. MAPH * (MAPH + 1)], 0..) |c, i| { + if (c == '\n') continue; + const x = @divFloor(i, MAPH + 1); + const y = (i % (MAPH + 1)) * 2; + map[x][y] = switch (c) { + '.' => .Empty, + '#' => .Wall, + 'O' => .BoxL, + '@' => .Robot, + else => unreachable, + }; + map[x][y + 1] = switch (c) { + '.', '@' => .Empty, + '#' => .Wall, + 'O' => .BoxR, + else => unreachable, + }; + if (c == '@') robot_position = Position{ .x = x, .y = y }; + } + + for (input[MAPH * (MAPH + 1) ..]) |c| switch (c) { + '>' => next(&map, &robot_position, .Right), + '<' => next(&map, &robot_position, .Left), + '^' => next(&map, &robot_position, .Top), + 'v' => next(&map, &robot_position, .Bot), + else => continue, + }; + + try std.testing.expectEqual(1521952, calculateSumPosition(map)); +} + +// The copy paste suck but if I want to use -1 to do dx dy, I need to use something else than usize and it fuck up the indexing +fn next(map: *[MAPH][MAPW]Cell, robot_position: *Position, direction: Direction) void { + const x = robot_position.x; + const y = robot_position.y; + + switch (direction) { + .Left => switch (map[x][y - 1]) { + .Empty => { + map[x][y] = .Empty; + map[x][y - 1] = .Robot; + robot_position.*.y -= 1; + }, + .Wall => {}, + .BoxR, .BoxL => if (moveLeftRight(map, robot_position.*, direction)) { + map[x][y - 1] = .Robot; + map[x][y] = .Empty; + robot_position.*.y -= 1; + }, + else => unreachable, + }, + .Right => switch (map[x][y + 1]) { + .Empty => { + map[x][y] = .Empty; + map[x][y + 1] = .Robot; + robot_position.*.y += 1; + }, + .Wall => {}, + .BoxR, .BoxL => if (moveLeftRight(map, robot_position.*, direction)) { + map[x][y + 1] = .Robot; + map[x][y] = .Empty; + robot_position.*.y += 1; + }, + else => unreachable, + }, + .Top => switch (map[x - 1][y]) { + .Empty => { + map[x][y] = .Empty; + map[x - 1][y] = .Robot; + robot_position.*.x -= 1; + }, + .Wall => {}, + .BoxL, .BoxR => if (moveTopBot(map, robot_position.*, direction)) { + map[x - 1][y] = .Robot; + map[x][y] = .Empty; + robot_position.*.x -= 1; + }, + else => unreachable, + }, + .Bot => switch (map[x + 1][y]) { + .Empty => { + map[x][y] = .Empty; + map[x + 1][y] = .Robot; + robot_position.*.x += 1; + }, + .Wall => {}, + .BoxL, .BoxR => if (moveTopBot(map, robot_position.*, direction)) { + map[x + 1][y] = .Robot; + map[x][y] = .Empty; + robot_position.*.x += 1; + }, + else => unreachable, + }, + } +} + +fn moveLeftRight(map: *[MAPH][MAPW]Cell, start: Position, direction: Direction) bool { + if (direction == .Top or direction == .Bot) @panic("Nope"); + + var y = start.y; + var is_right = direction == .Right; + if (direction == .Left) y -= 1; + if (direction == .Right) y += 1; + while (y != 0 and y != MAPW - 1) : ({ + if (direction == .Left) y -= 1; + if (direction == .Right) y += 1; + }) { + switch (map[start.x][y]) { + .Empty => { + while (y != start.y) : ({ + if (direction == .Left) y += 1; + if (direction == .Right) y -= 1; + is_right = !is_right; + }) { + map.*[start.x][y] = if (is_right) .BoxR else .BoxL; + } + return true; + }, + .BoxR, .BoxL => continue, + .Wall => return false, + .Robot => unreachable, + } + } + return false; +} + +fn canMoveTopBot(map: [MAPH][MAPW]Cell, start: Position, direction: Direction) bool { + if (direction == .Right or direction == .Left) @panic("Nope"); + + var x = start.x; + if (direction == .Top) x -= 1; + if (direction == .Bot) x += 1; + + switch (map[x][start.y]) { + .Empty => return true, + .BoxR => return canMoveTopBot(map, Position{ .x = x, .y = start.y }, direction) and canMoveTopBot(map, Position{ .x = x, .y = start.y - 1 }, direction), + .BoxL => return canMoveTopBot(map, Position{ .x = x, .y = start.y }, direction) and canMoveTopBot(map, Position{ .x = x, .y = start.y + 1 }, direction), + .Wall => return false, + .Robot => unreachable, + } +} + +fn moveTopBot(map: *[MAPH][MAPW]Cell, start: Position, direction: Direction) bool { + if (direction == .Right or direction == .Left) @panic("Nope"); + + if (!canMoveTopBot(map.*, start, direction)) return false; + + const x = if (direction == .Top) start.x - 1 else start.x + 1; + switch (map[x][start.y]) { + .Empty => return true, + .BoxR => if (moveTopBot(map, Position{ .x = x, .y = start.y }, direction) and moveTopBot(map, Position{ .x = x, .y = start.y - 1 }, direction)) { + map.*[if (direction == .Top) x - 1 else x + 1][start.y] = .BoxR; + map.*[if (direction == .Top) x - 1 else x + 1][start.y - 1] = .BoxL; + map.*[x][start.y] = .Empty; + map.*[x][start.y - 1] = .Empty; + return true; + } else return false, + .BoxL => if (moveTopBot(map, Position{ .x = x, .y = start.y }, direction) and moveTopBot(map, Position{ .x = x, .y = start.y + 1 }, direction)) { + map.*[if (direction == .Top) x - 1 else x + 1][start.y] = .BoxL; + map.*[if (direction == .Top) x - 1 else x + 1][start.y + 1] = .BoxR; + map.*[x][start.y] = .Empty; + map.*[x][start.y + 1] = .Empty; + return true; + } else return false, + .Wall => return false, + .Robot => unreachable, + } +} + +fn calculateSumPosition(map: [MAPH][MAPW]Cell) usize { + var total: usize = 0; + for (map, 0..) |row, x| for (row, 0..) |cell, y| switch (cell) { + .BoxL => total += x * 100 + y, + else => {}, + }; + return total; +}