Skip to content

Commit 1d5bb45

Browse files
committed
Stub out day 6 in Zig
1 parent fdb2ec1 commit 1d5bb45

File tree

8 files changed

+301
-0
lines changed

8 files changed

+301
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ My solutions to the [Advent of Code 2024](https://adventofcode.com/2024), writte
1111
- [x] [**Day 03**](day03): [Perl](day03/src/day03.pl)
1212
- [x] [**Day 04**](day04): [C](day04/src/day04.c)
1313
- [x] [**Day 05**](day05): [Prolog](day05/src/day05.pl)
14+
- [ ] [**Day 06**](day06): [Zig](day06/src/day06.zig)
1415

1516
## Development
1617

day06/derivation.nix

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{ stdenv, zig }:
2+
stdenv.mkDerivation {
3+
name = "advent-of-code-2024-day06";
4+
src = ./src;
5+
sourceRoot = ".";
6+
7+
nativeBuildInputs = [
8+
zig
9+
];
10+
11+
buildPhase = ''
12+
mkdir out cache
13+
14+
zig build-exe --global-cache-dir cache -femit-bin=out/day06 -O ReleaseSafe src/day06.zig
15+
'';
16+
17+
installPhase = ''
18+
mkdir -p $out/bin
19+
cp out/day06 $out/bin
20+
'';
21+
}

day06/flake.lock

+27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

day06/flake.nix

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
description = "Advent of Code 2024 - Day 06 solution";
3+
4+
inputs = {
5+
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
6+
};
7+
8+
outputs = { self, nixpkgs }:
9+
let
10+
supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];
11+
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
12+
in {
13+
packages = forAllSystems (system:
14+
let
15+
pkgs = import nixpkgs { inherit system; };
16+
in {
17+
default = pkgs.callPackage ./derivation.nix {};
18+
}
19+
);
20+
21+
apps = forAllSystems (system: {
22+
default = {
23+
type = "app";
24+
program = "${self.packages.${system}.default}/bin/day06";
25+
};
26+
});
27+
};
28+
}

day06/resources/demo.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
....#.....
2+
.........#
3+
..........
4+
..#.......
5+
.......#..
6+
..........
7+
.#..^.....
8+
........#.
9+
#.........
10+
......#...

day06/resources/input.txt

+130
Large diffs are not rendered by default.

day06/src/day06.zig

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
const std = @import("std");
2+
3+
const allocator = std.heap.page_allocator;
4+
5+
const List = std.ArrayList;
6+
const Map = std.AutoHashMap;
7+
const String = []u8;
8+
const Matrix = List(String);
9+
const Vec2 = struct { x: i32, y: i32 };
10+
const State = struct { pos: Vec2, dir: Vec2 };
11+
12+
fn Set(comptime T: type) type {
13+
return Map(T, void);
14+
}
15+
16+
fn width(matrix: Matrix) i32 {
17+
return matrix.items[0].len;
18+
}
19+
20+
fn height(matrix: Matrix) i32 {
21+
return matrix.items.len;
22+
}
23+
24+
fn parseDirection(c: u8) ?Vec2 {
25+
return switch (c) {
26+
'^' => .{ .x = 0, .y = -1 },
27+
'v' => .{ .x = 0, .y = 1 },
28+
'<' => .{ .x = -1, .y = 0 },
29+
'>' => .{ .x = 1, .y = 0 },
30+
else => null,
31+
};
32+
}
33+
34+
fn findStart(matrix: Matrix) State {
35+
for (0..height(matrix)) |i| {
36+
for (0..width(matrix)) |j| {
37+
const dir = parseDirection(matrix[i][j]);
38+
if (dir != null) {
39+
return dir;
40+
}
41+
}
42+
}
43+
std.debug.panic("Could not find start");
44+
}
45+
46+
pub fn main() !u8 {
47+
const args = try std.process.argsAlloc(allocator);
48+
if (args.len <= 1) {
49+
try std.io.getStdErr().writer().print("Usage: {s} <path to input>\n", .{args[0]});
50+
return 1;
51+
}
52+
53+
var matrix = Matrix.init(allocator);
54+
defer {
55+
for (matrix.items) |line| {
56+
allocator.free(line);
57+
}
58+
matrix.deinit();
59+
}
60+
61+
var buffer: [1024]u8 = undefined;
62+
var file = try std.fs.cwd().openFile(args[1], .{});
63+
var bufReader = std.io.bufferedReader(file.reader());
64+
var reader = bufReader.reader();
65+
defer file.close();
66+
67+
while (try reader.readUntilDelimiterOrEof(&buffer, '\n')) |bufLine| {
68+
const line = try allocator.dupeZ(u8, bufLine);
69+
try matrix.append(line);
70+
}
71+
72+
for (matrix.items) |line| {
73+
std.log.debug("{s}", .{line});
74+
}
75+
76+
return 0;
77+
}

paths.json

+7
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,12 @@
3838
},
3939
"path": "day05/src/day05.pl",
4040
"completed": true
41+
},
42+
{
43+
"lang": {
44+
"codemirror": "zig",
45+
"name": "Zig"
46+
},
47+
"path": "day06/src/day06.zig"
4148
}
4249
]

0 commit comments

Comments
 (0)