diff options
| author | Louis Burda <quent.burda@gmail.com> | 2023-04-08 12:40:30 -0400 |
|---|---|---|
| committer | Louis Burda <quent.burda@gmail.com> | 2023-04-09 10:21:36 -0400 |
| commit | 9282e95e8844afe856ba76ceb6d2c3010df8bb1a (patch) | |
| tree | e35affc89b20324371381e079f7cb5f8a06aa81b | |
| parent | 2b5d4232879dc74491dabf54a0ddc958d66ebcec (diff) | |
| download | aoc2020-zig-master.tar.gz aoc2020-zig-master.zip | |
| -rw-r--r-- | .gitignore | 3 | ||||
| -rw-r--r-- | .gitmodules | 3 | ||||
| -rw-r--r-- | README.md (renamed from docs/README.md) | 2 | ||||
| -rw-r--r-- | data/helper/config | 13 | ||||
| -rwxr-xr-x | data/helper/init.sh | 12 | ||||
| -rwxr-xr-x | data/helper/scripts/build | 7 | ||||
| -rwxr-xr-x | data/helper/scripts/run | 4 | ||||
| -rwxr-xr-x | data/helper/scripts/test | 7 | ||||
| -rw-r--r-- | data/helper/template/main.zig | 16 | ||||
| m--------- | helper | 0 | ||||
| -rw-r--r-- | lib/input.zig | 1 | ||||
| -rw-r--r-- | src/.gitignore | 3 | ||||
| -rw-r--r-- | src/01/input (renamed from src/day1/input) | 0 | ||||
| -rw-r--r-- | src/01/main.zig (renamed from src/day1/main.zig) | 27 | ||||
| -rw-r--r-- | src/01/part1 (renamed from src/day1/part1) | 0 | ||||
| -rw-r--r-- | src/01/part2 (renamed from src/day1/part2) | 0 | ||||
| -rw-r--r-- | src/02/input (renamed from src/day2/input) | 0 | ||||
| -rw-r--r-- | src/02/main.zig (renamed from src/day2/main.zig) | 28 | ||||
| -rw-r--r-- | src/02/part1 (renamed from src/day2/part1) | 0 | ||||
| -rw-r--r-- | src/02/part2 (renamed from src/day2/part2) | 0 | ||||
| -rw-r--r-- | src/03/input (renamed from src/day3/input) | 0 | ||||
| -rw-r--r-- | src/03/main.zig (renamed from src/day3/main.zig) | 28 | ||||
| -rw-r--r-- | src/03/part1 (renamed from src/day3/part1) | 0 | ||||
| -rw-r--r-- | src/03/part2 (renamed from src/day3/part2) | 0 | ||||
| -rw-r--r-- | src/04/input (renamed from src/day4/input) | 0 | ||||
| -rw-r--r-- | src/04/main.zig (renamed from src/day4/main.zig) | 34 | ||||
| -rw-r--r-- | src/04/part1 (renamed from src/day4/part1) | 0 | ||||
| -rw-r--r-- | src/04/part2 (renamed from src/day4/part2) | 0 | ||||
| -rw-r--r-- | src/05/input (renamed from src/day5/input) | 0 | ||||
| -rw-r--r-- | src/05/main.zig (renamed from src/day5/main.zig) | 26 | ||||
| -rw-r--r-- | src/05/part1 (renamed from src/day5/part1) | 0 | ||||
| -rw-r--r-- | src/05/part2 (renamed from src/day5/part2) | 0 | ||||
| -rw-r--r-- | src/06/input (renamed from src/day6/input) | 0 | ||||
| -rw-r--r-- | src/06/main.zig (renamed from src/day6/main.zig) | 22 | ||||
| -rw-r--r-- | src/06/part1 (renamed from src/day6/part1) | 0 | ||||
| -rw-r--r-- | src/06/part2 (renamed from src/day6/part2) | 0 | ||||
| -rw-r--r-- | src/07/input (renamed from src/day7/input) | 0 | ||||
| -rw-r--r-- | src/07/main.zig (renamed from src/day7/main.zig) | 61 | ||||
| -rw-r--r-- | src/07/part1 (renamed from src/day7/part1) | 0 | ||||
| -rw-r--r-- | src/07/part2 (renamed from src/day7/part2) | 0 | ||||
| -rw-r--r-- | src/08/input (renamed from src/day8/input) | 0 | ||||
| -rw-r--r-- | src/08/main.zig (renamed from src/day8/main.zig) | 25 | ||||
| -rw-r--r-- | src/08/part1 (renamed from src/day8/part1) | 0 | ||||
| -rw-r--r-- | src/08/part2 (renamed from src/day8/part2) | 0 | ||||
| -rw-r--r-- | src/09/input (renamed from src/day9/input) | 0 | ||||
| -rw-r--r-- | src/09/main.zig (renamed from src/day9/main.zig) | 25 | ||||
| -rw-r--r-- | src/09/part1 (renamed from src/day9/part1) | 0 | ||||
| -rw-r--r-- | src/09/part2 (renamed from src/day9/part2) | 0 | ||||
| -rw-r--r-- | src/10/input (renamed from src/day10/input) | 0 | ||||
| -rw-r--r-- | src/10/main.zig (renamed from src/day10/main.zig) | 67 | ||||
| -rw-r--r-- | src/10/out (renamed from src/day10/out) | 0 | ||||
| -rw-r--r-- | src/10/part1 (renamed from src/day10/part1) | 0 | ||||
| -rw-r--r-- | src/10/part2 (renamed from src/day10/part2) | 0 | ||||
| -rw-r--r-- | src/10/test-input (renamed from src/day10/test-input) | 0 | ||||
| -rw-r--r-- | src/10/test-input2 (renamed from src/day10/test-input2) | 0 | ||||
| -rw-r--r-- | src/11/input (renamed from src/day11/input) | 0 | ||||
| -rw-r--r-- | src/11/main.zig (renamed from src/day11/main.zig) | 32 | ||||
| -rw-r--r-- | src/11/part1 (renamed from src/day11/part1) | 0 | ||||
| -rw-r--r-- | src/11/part2 (renamed from src/day11/part2) | 0 | ||||
| -rw-r--r-- | src/12/input (renamed from src/day12/input) | 0 | ||||
| -rw-r--r-- | src/12/input-test (renamed from src/day12/input-test) | 0 | ||||
| -rw-r--r-- | src/12/main.zig (renamed from src/day12/main.zig) | 25 | ||||
| -rw-r--r-- | src/12/part1 (renamed from src/day12/part1) | 0 | ||||
| -rw-r--r-- | src/12/part2 (renamed from src/day12/part2) | 0 | ||||
| -rw-r--r-- | src/13/input (renamed from src/day13/input) | 0 | ||||
| -rw-r--r-- | src/13/input-test (renamed from src/day13/input-test) | 0 | ||||
| -rw-r--r-- | src/13/main.zig (renamed from src/day13/main.zig) | 41 | ||||
| -rw-r--r-- | src/13/notes (renamed from src/day13/notes) | 0 | ||||
| -rw-r--r-- | src/13/part1 (renamed from src/day13/part1) | 0 | ||||
| -rw-r--r-- | src/13/part2 (renamed from src/day13/part2) | 0 | ||||
| -rw-r--r-- | src/14/input (renamed from src/day14/input) | 0 | ||||
| -rw-r--r-- | src/14/input-test (renamed from src/day14/input-test) | 0 | ||||
| -rw-r--r-- | src/14/input-test2 (renamed from src/day14/input-test2) | 0 | ||||
| -rw-r--r-- | src/14/input-test3 (renamed from src/day14/input-test3) | 0 | ||||
| -rw-r--r-- | src/14/main.zig (renamed from src/day14/main.zig) | 26 | ||||
| -rw-r--r-- | src/14/part1 (renamed from src/day14/part1) | 0 | ||||
| -rw-r--r-- | src/14/part2 (renamed from src/day14/part2) | 0 | ||||
| -rw-r--r-- | src/15/input (renamed from src/day15/input) | 0 | ||||
| -rw-r--r-- | src/15/main.zig (renamed from src/day15/main.zig) | 32 | ||||
| -rw-r--r-- | src/15/part1 (renamed from src/day15/part1) | 0 | ||||
| -rw-r--r-- | src/15/part2 (renamed from src/day15/part2) | 0 | ||||
| -rw-r--r-- | src/16/input (renamed from src/day16/input) | 0 | ||||
| -rw-r--r-- | src/16/input-test (renamed from src/day16/input-test) | 0 | ||||
| -rw-r--r-- | src/16/main.zig (renamed from src/day16/main.zig) | 45 | ||||
| -rw-r--r-- | src/16/part1 (renamed from src/day16/part1) | 0 | ||||
| -rw-r--r-- | src/16/part2 (renamed from src/day16/part2) | 0 | ||||
| -rw-r--r-- | src/17/input (renamed from src/day17/input) | 0 | ||||
| -rw-r--r-- | src/17/input-test (renamed from src/day17/input-test) | 0 | ||||
| -rw-r--r-- | src/17/main.zig (renamed from src/day17/main.zig) | 54 | ||||
| -rw-r--r-- | src/17/part1 (renamed from src/day17/part1) | 0 | ||||
| -rw-r--r-- | src/17/part2 (renamed from src/day17/part2) | 0 | ||||
| -rw-r--r-- | src/18/input (renamed from src/day18/input) | 0 | ||||
| -rw-r--r-- | src/18/input-test (renamed from src/day18/input-test) | 0 | ||||
| -rw-r--r-- | src/18/input-test2 (renamed from src/day18/input-test2) | 0 | ||||
| -rw-r--r-- | src/18/main.zig (renamed from src/day18/main.zig) | 44 | ||||
| -rw-r--r-- | src/18/part1 (renamed from src/day18/part1) | 0 | ||||
| -rw-r--r-- | src/18/part2 (renamed from src/day18/part2) | 0 | ||||
| -rw-r--r-- | src/19/input (renamed from src/day19/input) | 0 | ||||
| -rw-r--r-- | src/19/input-test (renamed from src/day19/input-test) | 0 | ||||
| -rw-r--r-- | src/19/input-test2 (renamed from src/day19/input-test2) | 0 | ||||
| -rw-r--r-- | src/19/input-test3 (renamed from src/day19/input-test3) | 0 | ||||
| -rw-r--r-- | src/19/main.zig (renamed from src/day19/main.zig) | 56 | ||||
| -rw-r--r-- | src/19/part1 (renamed from src/day19/part1) | 0 | ||||
| -rw-r--r-- | src/19/part2 (renamed from src/day19/part2) | 0 | ||||
| -rw-r--r-- | src/19/trace (renamed from src/day19/trace) | 0 | ||||
| -rw-r--r-- | src/20/input (renamed from src/day20/input) | 0 | ||||
| -rw-r--r-- | src/20/input-test (renamed from src/day20/input-test) | 0 | ||||
| -rw-r--r-- | src/20/input-test2 (renamed from src/day20/input-test2) | 0 | ||||
| -rw-r--r-- | src/20/main.zig (renamed from src/day20/main.zig) | 103 | ||||
| -rw-r--r-- | src/20/part1 (renamed from src/day20/part1) | 0 | ||||
| -rw-r--r-- | src/20/part2 (renamed from src/day20/part2) | 0 | ||||
| -rw-r--r-- | src/21/input (renamed from src/day21/input) | 0 | ||||
| -rw-r--r-- | src/21/input-test (renamed from src/day21/input-test) | 0 | ||||
| -rw-r--r-- | src/21/main.zig (renamed from src/day21/main.zig) | 74 | ||||
| -rw-r--r-- | src/21/part1 (renamed from src/day21/part1) | 0 | ||||
| -rw-r--r-- | src/21/part2 (renamed from src/day21/part2) | 0 | ||||
| -rw-r--r-- | src/22/input (renamed from src/day22/input) | 0 | ||||
| -rw-r--r-- | src/22/input-test (renamed from src/day22/input-test) | 0 | ||||
| -rw-r--r-- | src/22/input-test2 (renamed from src/day22/input-test2) | 0 | ||||
| -rw-r--r-- | src/22/main.zig (renamed from src/day22/main.zig) | 69 | ||||
| -rw-r--r-- | src/22/part1 (renamed from src/day22/part1) | 0 | ||||
| -rw-r--r-- | src/22/part2 (renamed from src/day22/part2) | 0 | ||||
| -rw-r--r-- | src/23/input (renamed from src/day23/input) | 0 | ||||
| -rw-r--r-- | src/23/input-test (renamed from src/day23/input-test) | 0 | ||||
| -rw-r--r-- | src/23/main.zig (renamed from src/day23/main.zig) | 52 | ||||
| -rw-r--r-- | src/23/notes (renamed from src/day23/notes) | 0 | ||||
| -rw-r--r-- | src/23/part1 (renamed from src/day23/part1) | 0 | ||||
| -rw-r--r-- | src/23/part2 (renamed from src/day23/part2) | 0 | ||||
| -rw-r--r-- | src/24/input (renamed from src/day24/input) | 0 | ||||
| -rw-r--r-- | src/24/input-test (renamed from src/day24/input-test) | 0 | ||||
| -rw-r--r-- | src/24/main.zig (renamed from src/day24/main.zig) | 26 | ||||
| -rw-r--r-- | src/24/part1 (renamed from src/day24/part1) | 0 | ||||
| -rw-r--r-- | src/24/part2 (renamed from src/day24/part2) | 0 | ||||
| -rw-r--r-- | src/25/input (renamed from src/day25/input) | 0 | ||||
| -rw-r--r-- | src/25/input-test (renamed from src/day25/input-test) | 0 | ||||
| -rw-r--r-- | src/25/main.zig (renamed from src/day25/main.zig) | 24 | ||||
| -rw-r--r-- | src/25/part1 (renamed from src/day25/part1) | 0 | ||||
| -rw-r--r-- | src/25/part2 (renamed from src/day25/part2) | 0 | ||||
| -rw-r--r-- | src/Makefile | 21 | ||||
| -rw-r--r-- | src/common/aoc.zig (renamed from lib/aoc.zig) | 69 | ||||
| -rw-r--r-- | src/common/console8.zig (renamed from lib/console8.zig) | 20 |
141 files changed, 654 insertions, 573 deletions
diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 6517929..0000000 --- a/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -main -cache -zig-cache diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 1ef657f..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "helper"] - path = helper - url = git@github.com:Sinitax/aoc-helper.git diff --git a/docs/README.md b/README.md index f69129b..f47e337 100644 --- a/docs/README.md +++ b/README.md @@ -2,4 +2,4 @@ Solutions to the annual [coding advent calendar](https://adventofcode.com) written in [Zig](https://ziglang.org/). -The version of zig used to compile to code is: `0.7.0+d4c167f3c` +The version of zig used to compile the code is: `0.10.1` diff --git a/data/helper/config b/data/helper/config deleted file mode 100644 index ea85a1d..0000000 --- a/data/helper/config +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/bash - -# year you are solving problems for -export AOCYEAR=2020 - -# directory you want day directories to be prepared in (format: src/dayN/..) -export SRCDIR="src" - -# specify what files to copy to your day directory on prepare -export TEMPLATE_DIR="data/helper/template" -export TEMPLATE_FILES=" -main.zig:main.zig -" diff --git a/data/helper/init.sh b/data/helper/init.sh deleted file mode 100755 index 4553fb7..0000000 --- a/data/helper/init.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash - -REPOROOT=$(git rev-parse --show-toplevel) - -set -e - -ln -sf "$REPOROOT/data/helper/config" "$REPOROOT/helper/data/config" -for f in "$REPOROOT/data/helper/scripts"/*; do - ln -sf "$f" "$REPOROOT/helper/scripts" -done - -echo "done" diff --git a/data/helper/scripts/build b/data/helper/scripts/build deleted file mode 100755 index e7b4f7c..0000000 --- a/data/helper/scripts/build +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -[ ! -e "$SOLUTIONS_ROOT/cache" ] && mkdir "$SOLUTIONS_ROOT/cache" -zig build-exe --cache-dir "$SOLUTIONS_ROOT/cache" \ - --pkg-begin "console8" "$SOLUTIONS_ROOT/lib/console8.zig" --pkg-end \ - --pkg-begin "aoc" "$SOLUTIONS_ROOT/lib/aoc.zig" --pkg-end \ - main.zig diff --git a/data/helper/scripts/run b/data/helper/scripts/run deleted file mode 100755 index fde940f..0000000 --- a/data/helper/scripts/run +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash - -[ ! -e "main" ] && $HELPER_ROOT/scripts/build -$PWD/main $@ diff --git a/data/helper/scripts/test b/data/helper/scripts/test deleted file mode 100755 index 945f226..0000000 --- a/data/helper/scripts/test +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -[ ! -e "$SOLUTIONS_ROOT/cache" ] && mkdir "$SOLUTIONS_ROOT/cache" -zig test --cache-dir "$SOLUTIONS_ROOT/cache" \ - --pkg-begin "console8" "$SOLUTIONS_ROOT/lib/console8.zig" --pkg-end \ - --pkg-begin "aoc" "$SOLUTIONS_ROOT/lib/aoc.zig" --pkg-end \ - main.zig diff --git a/data/helper/template/main.zig b/data/helper/template/main.zig deleted file mode 100644 index 9e409ef..0000000 --- a/data/helper/template/main.zig +++ /dev/null @@ -1,16 +0,0 @@ -const std = @import("std"); -const aoc = @import("aoc"); - -fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { - var answer: u32 = 0; - - std.debug.print("{}\n", .{answer}); -} - -fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { - var answer: u32 = 0; - - std.debug.print("{}\n", .{answer}); -} - -pub const main = aoc.gen_main(part1, part2); diff --git a/helper b/helper deleted file mode 160000 -Subproject 6417aa3043784125bd6630152d34ce48617ea85 diff --git a/lib/input.zig b/lib/input.zig deleted file mode 100644 index 95a0b68..0000000 --- a/lib/input.zig +++ /dev/null @@ -1 +0,0 @@ -const std = @import("std"); diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 0000000..52b857a --- /dev/null +++ b/src/.gitignore @@ -0,0 +1,3 @@ +main +main.o +.cache diff --git a/src/day1/input b/src/01/input index 215b6f9..215b6f9 100644 --- a/src/day1/input +++ b/src/01/input diff --git a/src/day1/main.zig b/src/01/main.zig index 66f9731..8111960 100644 --- a/src/day1/main.zig +++ b/src/01/main.zig @@ -1,9 +1,7 @@ const std = @import("std"); const aoc = @import("aoc"); -const Parts = struct { - a: u32, b: u32 -}; +const Parts = struct { a: u32, b: u32 }; fn findparts(intlist: *const std.ArrayList(u32), sum: u32) ?Parts { var start: usize = 0; @@ -22,11 +20,11 @@ fn findparts(intlist: *const std.ArrayList(u32), sum: u32) ?Parts { return null; } -fn parse(allocator: *std.mem.Allocator, input: []u8) !std.ArrayList(u32) { +fn parse(allocator: std.mem.Allocator, input: []u8) !std.ArrayList(u32) { var intlist = std.ArrayList(u32).init(allocator); errdefer intlist.deinit(); - var it = std.mem.tokenize(input, "\n"); + var it = std.mem.tokenize(u8, input, "\n"); while (it.next()) |line| { try intlist.append(try std.fmt.parseInt(u32, line, 10)); } @@ -34,18 +32,24 @@ fn parse(allocator: *std.mem.Allocator, input: []u8) !std.ArrayList(u32) { return intlist; } -fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + const intlist = try parse(allocator, input); defer intlist.deinit(); std.sort.sort(u32, intlist.items, {}, comptime std.sort.asc(u32)); if (findparts(&intlist, 2020)) |parts| { - std.debug.print("{}\n", .{parts.a * parts.b}); + return try std.fmt.allocPrint(allocator, "{d}", .{parts.a * parts.b}); } + + return null; } -fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + const intlist = try parse(allocator, input); defer intlist.deinit(); @@ -57,11 +61,12 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { const tmp = intlist.items[third]; intlist.items[third] = target + 1; if (findparts(&intlist, target - tmp)) |parts| { - std.debug.print("{}\n", .{parts.a * parts.b * tmp}); - return; + return try std.fmt.allocPrint(allocator, "{d}", .{parts.a * parts.b * tmp}); } intlist.items[third] = tmp; } + + return null; } -pub const main = aoc.gen_main(part1, part2); +pub const main = aoc.main(part1, part2, .{ "658899", "155806250" }); diff --git a/src/day1/part1 b/src/01/part1 index 0452569..0452569 100644 --- a/src/day1/part1 +++ b/src/01/part1 diff --git a/src/day1/part2 b/src/01/part2 index 5e5b59a..5e5b59a 100644 --- a/src/day1/part2 +++ b/src/01/part2 diff --git a/src/day2/input b/src/02/input index 9219f52..9219f52 100644 --- a/src/day2/input +++ b/src/02/input diff --git a/src/day2/main.zig b/src/02/main.zig index 9b2b125..e965c95 100644 --- a/src/day2/main.zig +++ b/src/02/main.zig @@ -1,24 +1,22 @@ const std = @import("std"); const aoc = @import("aoc"); -const Entry = struct { - a: u16, b: u16, char: u8, pass: []u8 -}; +const Entry = struct { a: u16, b: u16, char: u8, pass: []u8 }; -fn freeEntries(allocator: *std.mem.Allocator, entries: *const std.ArrayList(Entry)) void { +fn freeEntries(allocator: std.mem.Allocator, entries: *const std.ArrayList(Entry)) void { for (entries.items) |item| allocator.free(item.pass); entries.deinit(); } -fn parse(allocator: *std.mem.Allocator, input: []u8) !std.ArrayList(Entry) { +fn parse(allocator: std.mem.Allocator, input: []u8) !std.ArrayList(Entry) { var entries = std.ArrayList(Entry).init(allocator); errdefer freeEntries(allocator, &entries); - var lineit = std.mem.tokenize(input, "\n"); + var lineit = std.mem.tokenize(u8, input, "\n"); while (lineit.next()) |line| { var entry: Entry = undefined; - var it = std.mem.tokenize(line, " -"); + var it = std.mem.tokenize(u8, line, " -"); entry.a = try std.fmt.parseInt(u16, it.next().?, 10); entry.b = try std.fmt.parseInt(u16, it.next().?, 10); entry.char = it.next().?[0]; @@ -29,7 +27,9 @@ fn parse(allocator: *std.mem.Allocator, input: []u8) !std.ArrayList(Entry) { return entries; } -fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + const entries = try parse(allocator, input); defer freeEntries(allocator, &entries); @@ -39,10 +39,13 @@ fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { for (entry.pass) |c| count += @boolToInt(c == entry.char); valid += @boolToInt(count >= entry.a and count <= entry.b); } - std.debug.print("{}\n", .{valid}); + + return try std.fmt.allocPrint(allocator, "{d}", .{valid}); } -fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + const entries = try parse(allocator, input); defer freeEntries(allocator, &entries); @@ -51,7 +54,8 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { valid += @boolToInt((entry.pass[entry.a - 1] == entry.char) != (entry.pass[entry.b - 1] == entry.char)); } - std.debug.print("{}\n", .{valid}); + + return try std.fmt.allocPrint(allocator, "{d}", .{valid}); } -pub const main = aoc.gen_main(part1, part2); +pub const main = aoc.main(part1, part2, .{ "603", "404" }); diff --git a/src/day2/part1 b/src/02/part1 index 99c2d91..99c2d91 100644 --- a/src/day2/part1 +++ b/src/02/part1 diff --git a/src/day2/part2 b/src/02/part2 index 97f255b..97f255b 100644 --- a/src/day2/part2 +++ b/src/02/part2 diff --git a/src/day3/input b/src/03/input index a11fc85..a11fc85 100644 --- a/src/day3/input +++ b/src/03/input diff --git a/src/day3/main.zig b/src/03/main.zig index 986df20..2ec8f24 100644 --- a/src/day3/main.zig +++ b/src/03/main.zig @@ -2,22 +2,20 @@ const std = @import("std"); const aoc = @import("aoc"); const Entry = []u1; -const Vector = struct { - x: u16, y: u16 -}; +const Vector = struct { x: u16, y: u16 }; -fn freeEntries(allocator: *std.mem.Allocator, entries: *const std.ArrayList(Entry)) void { +fn freeEntries(allocator: std.mem.Allocator, entries: *const std.ArrayList(Entry)) void { for (entries.items) |entry| { allocator.free(entry); } entries.deinit(); } -fn parse(allocator: *std.mem.Allocator, input: []u8) !std.ArrayList(Entry) { +fn parse(allocator: std.mem.Allocator, input: []u8) !std.ArrayList(Entry) { var entries = std.ArrayList(Entry).init(allocator); errdefer freeEntries(allocator, &entries); - var lineit = std.mem.tokenize(input, "\n"); + var lineit = std.mem.tokenize(u8, input, "\n"); while (lineit.next()) |line| { var linemap: []u1 = try allocator.alloc(u1, line.len); errdefer allocator.free(linemap); @@ -43,19 +41,23 @@ fn treesHit(map: std.ArrayList(Entry), slope: Vector) u16 { return count; } -fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + const map = try parse(allocator, input); defer freeEntries(allocator, &map); const answer = treesHit(map, Vector{ .x = 3, .y = 1 }); - std.debug.print("{}\n", .{answer}); + + return try std.fmt.allocPrint(allocator, "{d}", .{answer}); } -fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + const map = try parse(allocator, input); defer freeEntries(allocator, &map); - var answer: u32 = 1; const slopes = [_]Vector{ Vector{ .x = 1, .y = 1 }, Vector{ .x = 3, .y = 1 }, @@ -63,11 +65,13 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { Vector{ .x = 7, .y = 1 }, Vector{ .x = 1, .y = 2 }, }; + + var answer: u32 = 1; for (slopes) |slope| { answer *= treesHit(map, slope); } - std.debug.print("{}\n", .{answer}); + return try std.fmt.allocPrint(allocator, "{d}", .{answer}); } -pub const main = aoc.gen_main(part1, part2); +pub const main = aoc.main(part1, part2, .{ "282", "958815792" }); diff --git a/src/day3/part1 b/src/03/part1 index e44b180..e44b180 100644 --- a/src/day3/part1 +++ b/src/03/part1 diff --git a/src/day3/part2 b/src/03/part2 index 715fd1a..715fd1a 100644 --- a/src/day3/part2 +++ b/src/03/part2 diff --git a/src/day4/input b/src/04/input index 46fe04a..46fe04a 100644 --- a/src/day4/input +++ b/src/04/input diff --git a/src/day4/main.zig b/src/04/main.zig index 393a502..e2a860d 100644 --- a/src/day4/main.zig +++ b/src/04/main.zig @@ -17,7 +17,7 @@ fn intChecker( } if (len != null and parsed.len != len.?) return false; - const val = std.fmt.parseInt(u32, parsed, 10) catch |x| { + const val = std.fmt.parseInt(u32, parsed, 10) catch { return false; }; return (val >= min and val <= max); @@ -40,7 +40,7 @@ fn combineOr( fn isColorStr(input: []const u8) bool { if (input.len != 7) return false; - _ = std.fmt.parseInt(u32, input[1..], 16) catch |x| { + _ = std.fmt.parseInt(u32, input[1..], 16) catch { return false; }; return input[0] == '#'; @@ -51,14 +51,14 @@ fn isEyeColor(input: []const u8) bool { return std.mem.indexOf(u8, valids[0..], input) != null; } -fn countValids(allocator: *std.mem.Allocator, input: []u8, validate: bool) !u16 { - var entryit = std.mem.split(input, "\n\n"); +fn countValids(input: []u8, validate: bool) !u16 { + var entryit = std.mem.split(u8, input, "\n\n"); const required_keys = [_][]const u8{ "byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid", "cid" }; - const validation_funcs = [required_keys.len]?fn ([]const u8) bool{ + const validation_funcs = [required_keys.len]?*const fn ([]const u8) bool{ intChecker(1920, 2002, 4, null), intChecker(2010, 2020, 4, null), intChecker(2020, 2030, 4, null), - combineOr(comptime intChecker(150, 193, 3, "cm"), comptime intChecker(59, 76, 2, "in")), + combineOr(intChecker(150, 193, 3, "cm"), intChecker(59, 76, 2, "in")), isColorStr, isEyeColor, intChecker(0, 999999999, 9, null), @@ -68,7 +68,7 @@ fn countValids(allocator: *std.mem.Allocator, input: []u8, validate: bool) !u16 entryloop: while (entryit.next()) |entry| { const key_mask = [required_keys.len]u8{ 1, 1, 1, 1, 1, 1, 1, 'X' }; var present = [required_keys.len]u1{ 0, 0, 0, 0, 0, 0, 0, 0 }; - var partit = std.mem.tokenize(entry, ": \n"); + var partit = std.mem.tokenize(u8, entry, ": \n"); while (partit.next()) |key| { const value = partit.next().?; for (required_keys) |ckey, i| { @@ -81,7 +81,7 @@ fn countValids(allocator: *std.mem.Allocator, input: []u8, validate: bool) !u16 } } for (key_mask) |k, i| { - if (key_mask[i] != 'X' and present[i] != key_mask[i]) + if (k != 'X' and present[i] != k) continue :entryloop; } count += 1; @@ -89,12 +89,20 @@ fn countValids(allocator: *std.mem.Allocator, input: []u8, validate: bool) !u16 return count; } -fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { - std.debug.print("{}\n", .{try countValids(allocator, input, false)}); +fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + + const answer = try countValids(input, false); + + return try std.fmt.allocPrint(allocator, "{d}", .{answer}); } -fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { - std.debug.print("{}\n", .{try countValids(allocator, input, true)}); +fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + + const answer = try countValids(input, true); + + return try std.fmt.allocPrint(allocator, "{d}", .{answer}); } -pub const main = aoc.gen_main(part1, part2); +pub const main = aoc.main(part1, part2, .{ "226", "160" }); diff --git a/src/day4/part1 b/src/04/part1 index 2ef2dca..2ef2dca 100644 --- a/src/day4/part1 +++ b/src/04/part1 diff --git a/src/day4/part2 b/src/04/part2 index acad3cd..acad3cd 100644 --- a/src/day4/part2 +++ b/src/04/part2 diff --git a/src/day5/input b/src/05/input index 6cd6f33..6cd6f33 100644 --- a/src/day5/input +++ b/src/05/input diff --git a/src/day5/main.zig b/src/05/main.zig index 1551017..30f491f 100644 --- a/src/day5/main.zig +++ b/src/05/main.zig @@ -5,7 +5,7 @@ const row_count = 128; const col_count = 8; // Input is coded in a way that we can do an implicit binary search by replacing -// identifiers for the lower half with "0" and for the upper halb with "1" +// identifiers for the lower half with "0" and for the upper half with "1" // See commit <f1b717029cf3262c1fa2760124af258924d668da> for actual binary search fn code2Id(input: []const u8) !u32 { @@ -15,18 +15,23 @@ fn code2Id(input: []const u8) !u32 { return value; } -fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { - var lineit = std.mem.tokenize(input, "\n"); +fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + + var lineit = std.mem.tokenize(u8, input, "\n"); var answer: u32 = 0; while (lineit.next()) |line| { const id = try code2Id(line[0..10]); answer = std.math.max(answer, id); } - std.debug.print("{}\n", .{answer}); + + return try std.fmt.allocPrint(allocator, "{d}", .{answer}); } -fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { - var lineit = std.mem.tokenize(input, "\n"); +fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + + var lineit = std.mem.tokenize(u8, input, "\n"); var seats = [_]u1{0} ** (row_count * col_count); var min: u32 = std.math.inf_u32; while (lineit.next()) |line| { @@ -35,12 +40,13 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { seats[id] = 1; } - for (seats[min..]) |b, i| { + for (seats[min..]) |_, i| { if (seats[min + i] == 0) { - std.debug.print("{}\n", .{min + i}); - break; + return try std.fmt.allocPrint(allocator, "{d}", .{min + i}); } } + + return null; } -pub const main = aoc.gen_main(part1, part2); +pub const main = aoc.main(part1, part2, .{ "850", "599" }); diff --git a/src/day5/part1 b/src/05/part1 index 83d0f49..83d0f49 100644 --- a/src/day5/part1 +++ b/src/05/part1 diff --git a/src/day5/part2 b/src/05/part2 index f34c2db..f34c2db 100644 --- a/src/day5/part2 +++ b/src/05/part2 diff --git a/src/day6/input b/src/06/input index b47104b..b47104b 100644 --- a/src/day6/input +++ b/src/06/input diff --git a/src/day6/main.zig b/src/06/main.zig index ace7689..b02576b 100644 --- a/src/day6/main.zig +++ b/src/06/main.zig @@ -1,8 +1,10 @@ const std = @import("std"); const aoc = @import("aoc"); -fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { - var entryit = std.mem.split(input, "\n\n"); +fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + + var entryit = std.mem.split(u8, input, "\n\n"); var answer: u32 = 0; while (entryit.next()) |group| { var seen = [_]u1{0} ** 256; @@ -12,22 +14,26 @@ fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { seen[c] = 1; } } - std.debug.print("{}\n", .{answer}); + + return try std.fmt.allocPrint(allocator, "{d}", .{answer}); } -fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { - var entryit = std.mem.split(input, "\n\n"); +fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + + var entryit = std.mem.split(u8, input, "\n\n"); var answer: u32 = 0; while (entryit.next()) |group| { var count = [_]u16{0} ** 256; var members: u16 = 0; - var memberit = std.mem.tokenize(group, "\n "); + var memberit = std.mem.tokenize(u8, group, "\n "); while (memberit.next()) |member| : (members += 1) { for (member) |c| count[c] += 1; } for (count) |v| answer += @boolToInt(v == members); } - std.debug.print("{}\n", .{answer}); + + return try std.fmt.allocPrint(allocator, "{d}", .{answer}); } -pub const main = aoc.gen_main(part1, part2); +pub const main = aoc.main(part1, part2, .{ "6748", "3445" }); diff --git a/src/day6/part1 b/src/06/part1 index 7719598..7719598 100644 --- a/src/day6/part1 +++ b/src/06/part1 diff --git a/src/day6/part2 b/src/06/part2 index 73840f2..73840f2 100644 --- a/src/day6/part2 +++ b/src/06/part2 diff --git a/src/day7/input b/src/07/input index d52a49d..d52a49d 100644 --- a/src/day7/input +++ b/src/07/input diff --git a/src/day7/main.zig b/src/07/main.zig index 3ef0231..f252d22 100644 --- a/src/day7/main.zig +++ b/src/07/main.zig @@ -1,27 +1,21 @@ const std = @import("std"); const aoc = @import("aoc"); -const testv = @ptrToInt(&target); -const BagCount = struct { - id: u64, count: u32 -}; - -// null for 'not available' and errors for 'parsing failed' +const BagCount = struct { id: u64, count: u32 }; const BagRule = struct { outer: u64, - inner_striter: std.mem.SplitIterator, - const Self = @This(); - - fn from(line: []const u8) !Self { - var partit = std.mem.split(line[0 .. line.len - 1], " bags contain "); - var outer = std.hash.CityHash64.hash(partit.next().?); - var innerit = std.mem.split(partit.next().?, ", "); - return Self{ .outer = outer, .inner_striter = innerit }; + innerit: std.mem.SplitIterator(u8), + + fn from(line: []const u8) BagRule { + var partit = std.mem.split(u8, line, " bags contain "); + const outer = std.hash.CityHash64.hash(partit.next().?); + var innerit = std.mem.split(u8, partit.next().?, ", "); + return BagRule{ .outer = outer, .innerit = innerit }; } - fn nextInner(self: *Self) !?BagCount { - if (self.inner_striter.next()) |bagstr| { + fn nextInner(self: *BagRule) !?BagCount { + if (self.innerit.next()) |bagstr| { if (std.mem.eql(u8, bagstr[0..2], "no")) return null; const lastsep = std.mem.lastIndexOfScalar(u8, bagstr, ' ').?; const firstsep = std.mem.indexOfScalar(u8, bagstr, ' ').?; @@ -56,50 +50,59 @@ fn countParentTypes(map: *std.AutoHashMap(u64, std.ArrayList(u64)), inner: u64, return sum; } -fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { - var lineit = std.mem.tokenize(input, "\n"); +fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + + var lineit = std.mem.tokenize(u8, input, "\n"); var map = std.AutoHashMap(u64, std.ArrayList(u64)).init(allocator); defer { var mapit = map.iterator(); - while (mapit.next()) |kv| kv.value.deinit(); + while (mapit.next()) |kv| kv.value_ptr.deinit(); map.deinit(); } while (lineit.next()) |line| { - var rule = try BagRule.from(line); + var rule = BagRule.from(line); while (try rule.nextInner()) |inner| { if (map.get(inner.id) == null) try map.put(inner.id, std.ArrayList(u64).init(allocator)); - try map.getEntry(inner.id).?.*.value.append(rule.outer); + try map.getEntry(inner.id).?.value_ptr.append(rule.outer); } } const target = std.hash.CityHash64.hash("shiny gold"); var seen = std.ArrayList(u64).init(allocator); defer seen.deinit(); - std.debug.print("{}\n", .{countParentTypes(&map, target, &seen)}); + + const answer = try countParentTypes(&map, target, &seen); + + return try std.fmt.allocPrint(allocator, "{d}", .{answer}); } -fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { - var lineit = std.mem.tokenize(input, "\n"); +fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + + var lineit = std.mem.tokenize(u8, input, "\n"); var map = std.AutoHashMap(u64, std.ArrayList(BagCount)).init(allocator); defer { var mapit = map.iterator(); - while (mapit.next()) |kv| kv.value.deinit(); + while (mapit.next()) |kv| kv.value_ptr.deinit(); map.deinit(); } const target = std.hash.CityHash64.hash("shiny gold"); while (lineit.next()) |line| { - var rule = try BagRule.from(line); + var rule = BagRule.from(line); if (map.get(rule.outer) == null) try map.put(rule.outer, std.ArrayList(BagCount).init(allocator)); while (try rule.nextInner()) |bag| { - try map.getEntry(rule.outer).?.*.value.append(bag); + try map.getEntry(rule.outer).?.value_ptr.append(bag); } } - std.debug.print("{}\n", .{countBagsTotal(&map, target)}); + const answer = countBagsTotal(&map, target); + + return try std.fmt.allocPrint(allocator, "{d}", .{answer}); } -pub const main = aoc.gen_main(part1, part2); +pub const main = aoc.main(part1, part2, .{ "161", "30899" }); diff --git a/src/day7/part1 b/src/07/part1 index 5525618..5525618 100644 --- a/src/day7/part1 +++ b/src/07/part1 diff --git a/src/day7/part2 b/src/07/part2 index eabda86..eabda86 100644 --- a/src/day7/part2 +++ b/src/07/part2 diff --git a/src/day8/input b/src/08/input index 44a20f8..44a20f8 100644 --- a/src/day8/input +++ b/src/08/input diff --git a/src/day8/main.zig b/src/08/main.zig index 97600e5..d8aa00f 100644 --- a/src/day8/main.zig +++ b/src/08/main.zig @@ -3,8 +3,9 @@ const aoc = @import("aoc"); const Console = @import("console8").Console; fn runTillRepeat(console: *Console, instlist: *std.ArrayList(u64), swapon: ?u64) !void { - while (try console.parseNext()) |*inst| { - // std.debug.print("{:<5}: {} {}\n", .{ console.instructptr, inst.opcode, inst.argval }); + while (try console.parseNext()) |_inst| { + var inst = _inst; + aoc.debugfmt("{:<5}: {s} {d}\n", .{ console.instructptr, inst.opcode, inst.argval }); if (std.mem.indexOfScalar(u64, instlist.items, console.instructptr) != null) { return; } @@ -16,11 +17,13 @@ fn runTillRepeat(console: *Console, instlist: *std.ArrayList(u64), swapon: ?u64) }; } try instlist.append(console.instructptr); - try console.exec(inst); + try console.exec(&inst); } } -fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + var console = try Console.init(input, allocator); defer console.deinit(); @@ -28,10 +31,13 @@ fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { defer instlist.deinit(); try runTillRepeat(&console, &instlist, null); - std.debug.print("{}\n", .{console.accumulator}); + + return try std.fmt.allocPrint(allocator, "{d}", .{console.accumulator}); } -fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + var console = try Console.init(input, allocator); defer console.deinit(); @@ -51,10 +57,11 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { //std.debug.print("Swapping instruction: {:<5}: {}\n", .{ inst, console.instlist[inst] }); runTillRepeat(&console, &sim_seen, inst) catch {}; if (console.jumpAddr == console.instlist.len or console.instructptr == console.instlist.len) { - std.debug.print("{}\n", .{console.accumulator}); - break; + return try std.fmt.allocPrint(allocator, "{d}", .{console.accumulator}); } } + + return null; } -pub const main = aoc.gen_main(part1, part2); +pub const main = aoc.main(part1, part2, .{ "1766", "1639" }); diff --git a/src/day8/part1 b/src/08/part1 index abc8089..abc8089 100644 --- a/src/day8/part1 +++ b/src/08/part1 diff --git a/src/day8/part2 b/src/08/part2 index ea07683..ea07683 100644 --- a/src/day8/part2 +++ b/src/08/part2 diff --git a/src/day9/input b/src/09/input index 20f8901..20f8901 100644 --- a/src/day9/input +++ b/src/09/input diff --git a/src/day9/main.zig b/src/09/main.zig index d28af56..1187e99 100644 --- a/src/day9/main.zig +++ b/src/09/main.zig @@ -13,12 +13,12 @@ fn preambleHasSum(preamble: []u64, target: u64) bool { return false; } -fn getInvalid(allocator: *std.mem.Allocator, input: []u8) !u64 { +fn getInvalid(allocator: std.mem.Allocator, input: []u8) !u64 { var preamble = std.ArrayList(u64).init(allocator); defer preamble.deinit(); var i: u64 = 0; - var numit = std.mem.tokenize(input, "\n"); + var numit = std.mem.tokenize(u8, input, "\n"); while (numit.next()) |numstr| : (i += 1) { const num = try std.fmt.parseInt(u64, numstr, 10); if (i > preamble_size) { @@ -41,17 +41,23 @@ fn listSum(items: []u64) u64 { return sum; } -fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { - std.debug.print("{}\n", .{try getInvalid(allocator, input)}); +fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + + const answer = try getInvalid(allocator, input); + + return try std.fmt.allocPrint(allocator, "{d}", .{answer}); } -fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + const invalid = try getInvalid(allocator, input); var numset = std.ArrayList(u64).init(allocator); defer numset.deinit(); - var numit = std.mem.tokenize(input, "\n"); + var numit = std.mem.tokenize(u8, input, "\n"); while (true) { const sum = listSum(numset.items); if (sum < invalid) { @@ -67,8 +73,11 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { } if (numset.items.len > 0) { - std.debug.print("{}\n", .{std.mem.min(u64, numset.items) + std.mem.max(u64, numset.items)}); + const answer = std.mem.min(u64, numset.items) + std.mem.max(u64, numset.items); + return try std.fmt.allocPrint(allocator, "{d}", .{answer}); } + + return null; } -pub const main = aoc.gen_main(part1, part2); +pub const main = aoc.main(part1, part2, .{ "466456641", "55732936" }); diff --git a/src/day9/part1 b/src/09/part1 index 621aa28..621aa28 100644 --- a/src/day9/part1 +++ b/src/09/part1 diff --git a/src/day9/part2 b/src/09/part2 index 674e22d..674e22d 100644 --- a/src/day9/part2 +++ b/src/09/part2 diff --git a/src/day10/input b/src/10/input index 20133c9..20133c9 100644 --- a/src/day10/input +++ b/src/10/input diff --git a/src/day10/main.zig b/src/10/main.zig index 2004408..993b2cf 100644 --- a/src/day10/main.zig +++ b/src/10/main.zig @@ -1,13 +1,15 @@ const std = @import("std"); const aoc = @import("aoc"); -fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + var target: u32 = 0; var intlist = std.ArrayList(u32).init(allocator); defer intlist.deinit(); - var lineit = std.mem.tokenize(input, "\n"); + var lineit = std.mem.tokenize(u8, input, "\n"); while (lineit.next()) |line| { var val = try std.fmt.parseInt(u32, line, 10); if (val > target) { @@ -19,7 +21,7 @@ fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { target += 3; try intlist.append(target); - if (intlist.items.len == 0) return; + if (intlist.items.len == 0) return null; std.sort.sort(u32, intlist.items, {}, comptime std.sort.asc(u32)); @@ -27,14 +29,14 @@ fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { var last: u32 = 0; for (intlist.items) |ov| { if (ov - last > 3 or ov == last) { - std.debug.print("Unable to find a suitable adapter for {} output jolts (next: {} jolts)\n", .{ last, ov }); + aoc.debugfmt("Unable to find a suitable adapter for {} output jolts (next: {} jolts)\n", .{ last, ov }); return aoc.Error.InvalidInput; } diffs[ov - last] += 1; last = ov; } - std.debug.print("{}\n", .{diffs[1] * diffs[3]}); + return try std.fmt.allocPrint(allocator, "{d}", .{diffs[1] * diffs[3]}); } fn countArrangements(arrangement: []u32, adapters: []const u32, adaptermask: []u1, target: u32, fillcount: u32, searchstart: u32, count: *u32) void { @@ -59,45 +61,10 @@ fn countArrangements(arrangement: []u32, adapters: []const u32, adaptermask: []u } } -fn part2Recursive(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { - var target: u32 = 0; - - var intlist = std.ArrayList(u32).init(allocator); - defer intlist.deinit(); - - var lineit = std.mem.tokenize(input, "\n"); - while (lineit.next()) |line| { - var val = try std.fmt.parseInt(u32, line, 10); - if (val > target) { - target = val; - } - try intlist.append(val); - } - - target += 3; - try intlist.append(target); - - if (intlist.items.len == 0) return; - - std.sort.sort(u32, intlist.items, {}, comptime std.sort.asc(u32)); - - var count: u32 = 0; - - var arrangements = try allocator.alloc(u32, intlist.items.len); - defer allocator.free(arrangements); - - var adaptermask = try allocator.alloc(u1, intlist.items.len); - defer allocator.free(adaptermask); - - countArrangements(arrangements, intlist.items, adaptermask, target, 0, 0, &count); - - std.debug.print("{}\n", .{count}); -} - fn groupPermutations(adapters: []const u32) u64 { if (adapters.len == 1) return 1; var count: u64 = 0; - for (adapters) |v, i| { + for (adapters) |_, i| { if (i == 0) continue; if (adapters[i] <= adapters[0] + 3) { count += groupPermutations(adapters[i..]); @@ -106,7 +73,9 @@ fn groupPermutations(adapters: []const u32) u64 { return count; } -fn part2Iterative(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + var target: u32 = 0; var intlist = std.ArrayList(u32).init(allocator); @@ -114,7 +83,7 @@ fn part2Iterative(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !voi try intlist.append(0); - var lineit = std.mem.tokenize(input, "\n"); + var lineit = std.mem.tokenize(u8, input, "\n"); while (lineit.next()) |line| { var val = try std.fmt.parseInt(u32, line, 10); if (val > target) { @@ -138,17 +107,13 @@ fn part2Iterative(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !voi // whenever we encounter a 3 jolt gap, we can calc permutations of group before if (i + 1 < intlist.items.len and intlist.items[i + 1] == intlist.items[i] + 3) { multiplier *= groupPermutations(intlist.items[gapstart .. i + 1]); - std.debug.print("gap from {} to {} => {}\n", .{ intlist.items[i], intlist.items[i + 1], multiplier }); - std.debug.print("group size: {}\n", .{i + 1 - gapstart}); + aoc.debugfmt("gap from {} to {} => {}\n", .{ intlist.items[i], intlist.items[i + 1], multiplier }); + aoc.debugfmt("group size: {}\n", .{i + 1 - gapstart}); gapstart = i + 1; } } - std.debug.print("{}\n", .{multiplier}); -} - -fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { - try part2Iterative(allocator, input, args); + return try std.fmt.allocPrint(allocator, "{d}", .{multiplier}); } -pub const main = aoc.gen_main(part1, part2); +pub const main = aoc.main(part1, part2, .{ "1998", "347250213298688" }); diff --git a/src/day10/out b/src/10/out index 2c31060..2c31060 100644 --- a/src/day10/out +++ b/src/10/out diff --git a/src/day10/part1 b/src/10/part1 index 3e17466..3e17466 100644 --- a/src/day10/part1 +++ b/src/10/part1 diff --git a/src/day10/part2 b/src/10/part2 index 6f7e947..6f7e947 100644 --- a/src/day10/part2 +++ b/src/10/part2 diff --git a/src/day10/test-input b/src/10/test-input index e6376dc..e6376dc 100644 --- a/src/day10/test-input +++ b/src/10/test-input diff --git a/src/day10/test-input2 b/src/10/test-input2 index ec4a03f..ec4a03f 100644 --- a/src/day10/test-input2 +++ b/src/10/test-input2 diff --git a/src/day11/input b/src/11/input index 980d6e0..980d6e0 100644 --- a/src/day11/input +++ b/src/11/input diff --git a/src/day11/main.zig b/src/11/main.zig index faeefbe..857c457 100644 --- a/src/day11/main.zig +++ b/src/11/main.zig @@ -17,8 +17,8 @@ const adjacent = [_]Dir{ Dir{ .x = 1, .y = 1 }, }; -fn parseMap(mapitems: *[]SeatState, dims: *MapDims, input: []const u8, allocator: *std.mem.Allocator) !void { - var lineit = std.mem.tokenize(input, "\n"); +fn parseMap(mapitems: *[]SeatState, dims: *MapDims, input: []const u8, allocator: std.mem.Allocator) !void { + var lineit = std.mem.tokenize(u8, input, "\n"); var map = std.ArrayList(SeatState).init(allocator); errdefer map.deinit(); while (lineit.next()) |line| { @@ -27,7 +27,7 @@ fn parseMap(mapitems: *[]SeatState, dims: *MapDims, input: []const u8, allocator } else if (dims.width != line.len) { return aoc.Error.InvalidInput; } - for (line) |c, i| { + for (line) |c| { try map.append(switch (c) { '#' => SeatState.TAKEN, 'L' => SeatState.EMPTY, @@ -50,9 +50,9 @@ fn printMap(mapitems: []SeatState, dims: MapDims) void { SeatState.FLOOR => '.', }; if (i % dims.width == 0) { - std.debug.print("\n", .{}); + aoc.debugfmt("\n", .{}); } - std.debug.print("{c}", .{c}); + aoc.debugfmt("{c}", .{c}); } } @@ -112,7 +112,9 @@ fn simulateStrategyTwo(before: []SeatState, after: []SeatState, dims: MapDims) u return seat_changes; } -fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + var mapdims = MapDims{ .width = 0, .height = 0 }; var mapitems: []SeatState = undefined; try parseMap(&mapitems, &mapdims, input, allocator); @@ -123,7 +125,7 @@ fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { var round: u32 = 0; while (simulateStrategyOne(mapitems, mapresult, mapdims) > 0) : (round += 1) { - std.debug.print("\rRound: {}", .{round}); + aoc.debugfmt("\rRound: {}", .{round}); std.mem.copy(SeatState, mapitems, mapresult); } @@ -132,10 +134,14 @@ fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { taken_count += @boolToInt(state == SeatState.TAKEN); } - std.debug.print("\nSeats Occupied: {}\n", .{taken_count}); + aoc.debugfmt("\nSeats Occupied: {}\n", .{taken_count}); + + return try std.fmt.allocPrint(allocator, "{d}", .{taken_count}); } -fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + var mapdims = MapDims{ .width = 0, .height = 0 }; var mapitems: []SeatState = undefined; try parseMap(&mapitems, &mapdims, input, allocator); @@ -146,7 +152,7 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { var round: u32 = 0; while (simulateStrategyTwo(mapitems, mapresult, mapdims) > 0) : (round += 1) { - std.debug.print("\rRound: {}", .{round}); + aoc.debugfmt("\rRound: {}", .{round}); std.mem.copy(SeatState, mapitems, mapresult); } @@ -155,7 +161,9 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { taken_count += @boolToInt(state == SeatState.TAKEN); } - std.debug.print("\nSeats Occupied: {}\n", .{taken_count}); + aoc.debugfmt("\nSeats Occupied: {}\n", .{taken_count}); + + return try std.fmt.allocPrint(allocator, "{d}", .{taken_count}); } -pub const main = aoc.gen_main(part1, part2); +pub const main = aoc.main(part1, part2, .{ "2126", "1914" }); diff --git a/src/day11/part1 b/src/11/part1 index 1ad3ef1..1ad3ef1 100644 --- a/src/day11/part1 +++ b/src/11/part1 diff --git a/src/day11/part2 b/src/11/part2 index 0aa31f5..0aa31f5 100644 --- a/src/day11/part2 +++ b/src/11/part2 diff --git a/src/day12/input b/src/12/input index 987fe92..987fe92 100644 --- a/src/day12/input +++ b/src/12/input diff --git a/src/day12/input-test b/src/12/input-test index d382291..d382291 100644 --- a/src/day12/input-test +++ b/src/12/input-test diff --git a/src/day12/main.zig b/src/12/main.zig index 4ef9772..12fd5f0 100644 --- a/src/day12/main.zig +++ b/src/12/main.zig @@ -5,9 +5,11 @@ const Dir = aoc.Dir; const Ship = struct { pos: Pos, waypoint: Pos, dir: usize }; -fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + var ship = Ship{ .pos = Pos{ .x = 0, .y = 0 }, .waypoint = Pos{ .x = 0, .y = 0 }, .dir = 0 }; - var lineit = std.mem.tokenize(input, "\n"); + var lineit = std.mem.tokenize(u8, input, "\n"); while (lineit.next()) |line| { const val = try std.fmt.parseInt(u32, line[1..], 10); switch (line[0]) { @@ -24,12 +26,16 @@ fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { } } - std.debug.print("{}\n", .{std.math.absCast(ship.pos.x) + std.math.absCast(ship.pos.y)}); + const answer = std.math.absCast(ship.pos.x) + std.math.absCast(ship.pos.y); + + return try std.fmt.allocPrint(allocator, "{d}", .{answer}); } -fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + var ship = Ship{ .pos = Pos{ .x = 0, .y = 0 }, .waypoint = Pos{ .x = 10, .y = 1 }, .dir = 0 }; - var lineit = std.mem.tokenize(input, "\n"); + var lineit = std.mem.tokenize(u8, input, "\n"); while (lineit.next()) |line| { const val = try std.fmt.parseInt(u32, line[1..], 10); switch (line[0]) { @@ -37,7 +43,6 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { 'S' => ship.waypoint = ship.waypoint.add(Dir.South.mult(val)), 'E' => ship.waypoint = ship.waypoint.add(Dir.East.mult(val)), 'W' => ship.waypoint = ship.waypoint.add(Dir.West.mult(val)), - // WORKAROUND for language bug (parameter is implicitly passed as reference and assignment order) 'L' => { var new = Dir.rotCCW(ship.waypoint, @divExact(val, 90)); ship.waypoint = new; @@ -51,10 +56,12 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { return aoc.Error.InvalidInput; }, } - std.debug.print("{} {} {} {} {}\n", .{ line, ship.waypoint.x, ship.waypoint.y, ship.pos.x, ship.pos.y }); + aoc.debugfmt("{s} {} {} {} {}\n", .{ line, ship.waypoint.x, ship.waypoint.y, ship.pos.x, ship.pos.y }); } - std.debug.print("{}\n", .{std.math.absCast(ship.pos.x) + std.math.absCast(ship.pos.y)}); + const answer = std.math.absCast(ship.pos.x) + std.math.absCast(ship.pos.y); + + return try std.fmt.allocPrint(allocator, "{d}", .{answer}); } -pub const main = aoc.gen_main(part1, part2); +pub const main = aoc.main(part1, part2, .{ "1191", "61053" }); diff --git a/src/day12/part1 b/src/12/part1 index 831d3c8..831d3c8 100644 --- a/src/day12/part1 +++ b/src/12/part1 diff --git a/src/day12/part2 b/src/12/part2 index 74d86c0..74d86c0 100644 --- a/src/day12/part2 +++ b/src/12/part2 diff --git a/src/day13/input b/src/13/input index c16c8bb..c16c8bb 100644 --- a/src/day13/input +++ b/src/13/input diff --git a/src/day13/input-test b/src/13/input-test index d76f619..d76f619 100644 --- a/src/day13/input-test +++ b/src/13/input-test diff --git a/src/day13/main.zig b/src/13/main.zig index 9519f4c..0252e2b 100644 --- a/src/day13/main.zig +++ b/src/13/main.zig @@ -24,31 +24,33 @@ fn waittime(start: u32, busid: u32) u32 { } fn printRegion(buses: []u32, start: u64, end: u64) void { - std.debug.print(" ", .{}); + aoc.debugfmt(" ", .{}); for (buses) |bus| { - std.debug.print(" {:^5}", .{bus}); + aoc.debugfmt(" {:^5}", .{bus}); } - std.debug.print("\n", .{}); + aoc.debugfmt("\n", .{}); var i = start; while (i < end) : (i += 1) { - std.debug.print("{:<10}", .{i}); + aoc.debugfmt("{:<10}", .{i}); for (buses) |bus| { var c: u8 = '.'; if (bus > 0 and i % bus == 0) c = 'D'; - std.debug.print(" {c} ", .{c}); + aoc.debugfmt(" {c} ", .{c}); } - std.debug.print("\n", .{}); + aoc.debugfmt("\n", .{}); } - std.debug.print("\n", .{}); + aoc.debugfmt("\n", .{}); } -fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { - var lineit = std.mem.tokenize(input, "\n"); +fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + + var lineit = std.mem.tokenize(u8, input, "\n"); var start = try std.fmt.parseInt(u32, lineit.next().?, 10); var bestbus: ?u32 = null; - var busit = std.mem.tokenize(lineit.next().?, ","); + var busit = std.mem.tokenize(u8, lineit.next().?, ","); while (busit.next()) |bus| { if (bus[0] == 'x') continue; const val = try std.fmt.parseInt(u32, bus, 10); @@ -57,17 +59,21 @@ fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { } } - std.debug.print("{} {}\n", .{ bestbus.?, bestbus.? * waittime(start, bestbus.?) }); + const answer = bestbus.? * waittime(start, bestbus.?); + + return try std.fmt.allocPrint(allocator, "{d}", .{answer}); } -fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { - var lineit = std.mem.tokenize(input, "\n"); +fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + + var lineit = std.mem.tokenize(u8, input, "\n"); _ = try std.fmt.parseInt(u32, lineit.next().?, 10); var busses = std.ArrayList(u32).init(allocator); defer busses.deinit(); - var busit = std.mem.tokenize(lineit.next().?, ","); + var busit = std.mem.tokenize(u8, lineit.next().?, ","); const first = try std.fmt.parseInt(u32, busit.next().?, 10); try busses.append(first); @@ -94,12 +100,13 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { if (aoc.debug) { printRegion(busses.items, offset, offset + delay + 1); printRegion(busses.items, offset + cycle, offset + cycle + delay + 1); - std.debug.print("{}\n", .{offset}); + aoc.debugfmt("{}\n", .{offset}); } } printRegion(busses.items, offset, offset + busses.items.len); - std.debug.print("{}\n", .{offset}); + + return try std.fmt.allocPrint(allocator, "{}", .{offset}); } -pub const main = aoc.gen_main(part1, part2); +pub const main = aoc.main(part1, part2, .{ "6568", "554865447501099" }); diff --git a/src/day13/notes b/src/13/notes index 7930ccf..7930ccf 100644 --- a/src/day13/notes +++ b/src/13/notes diff --git a/src/day13/part1 b/src/13/part1 index b4ffca7..b4ffca7 100644 --- a/src/day13/part1 +++ b/src/13/part1 diff --git a/src/day13/part2 b/src/13/part2 index c8f8f3b..c8f8f3b 100644 --- a/src/day13/part2 +++ b/src/13/part2 diff --git a/src/day14/input b/src/14/input index 6facee9..6facee9 100644 --- a/src/day14/input +++ b/src/14/input diff --git a/src/day14/input-test b/src/14/input-test index e15150a..e15150a 100644 --- a/src/day14/input-test +++ b/src/14/input-test diff --git a/src/day14/input-test2 b/src/14/input-test2 index df1adb9..df1adb9 100644 --- a/src/day14/input-test2 +++ b/src/14/input-test2 diff --git a/src/day14/input-test3 b/src/14/input-test3 index 2e961e3..2e961e3 100644 --- a/src/day14/input-test3 +++ b/src/14/input-test3 diff --git a/src/day14/main.zig b/src/14/main.zig index a55b237..e45203d 100644 --- a/src/day14/main.zig +++ b/src/14/main.zig @@ -25,7 +25,9 @@ fn getAddrStr(line: []const u8) ![]const u8 { return line[4..sep.?]; } -fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + var answer: u64 = 0; var memmap = std.AutoHashMap(u36, u64).init(allocator); @@ -34,7 +36,7 @@ fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { var ormask: u36 = 0; var andmask: u36 = ~@as(u36, 0); - var lineit = std.mem.tokenize(input, "\n"); + var lineit = std.mem.tokenize(u8, input, "\n"); while (lineit.next()) |line| { if (std.mem.eql(u8, line[0..4], "mask")) { andmask = ~@as(u36, 0); @@ -55,15 +57,15 @@ fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { var mapit = memmap.iterator(); while (mapit.next()) |kv| { - answer += kv.value; + answer += kv.value_ptr.*; } - std.debug.print("{}\n", .{answer}); + return try std.fmt.allocPrint(allocator, "{}", .{answer}); } fn bitRecurse(map: *std.AutoHashMap(u36, u64), addr: *u36, val: u64, mask: u36, index: u6) anyerror!void { if (index == 36) { - std.debug.print("SET: {b} {}\n", .{ addr.*, val }); + aoc.debugfmt("SET: {b} {}\n", .{ addr.*, val }); try map.put(addr.*, val); } else { if (getBit(mask, index)) { @@ -77,7 +79,9 @@ fn bitRecurse(map: *std.AutoHashMap(u36, u64), addr: *u36, val: u64, mask: u36, } } -fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + var answer: u64 = 0; var memmap = std.AutoHashMap(u36, u64).init(allocator); @@ -86,7 +90,7 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { var ormask: u36 = 0; var flipmask: u36 = 0; - var lineit = std.mem.tokenize(input, "\n"); + var lineit = std.mem.tokenize(u8, input, "\n"); while (lineit.next()) |line| { if (std.mem.eql(u8, line[0..4], "mask")) { ormask = 0; @@ -102,17 +106,17 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { var addr = try std.fmt.parseInt(u36, try getAddrStr(line), 10); const val = try std.fmt.parseInt(u36, try getValStr(line), 10); addr = addr | ormask; - std.debug.print("{b} {b} {} {}\n", .{ flipmask, ormask, addr, val }); + aoc.debugfmt("{b} {b} {} {}\n", .{ flipmask, ormask, addr, val }); try bitRecurse(&memmap, &addr, val, flipmask, 0); } } var mapit = memmap.iterator(); while (mapit.next()) |kv| { - answer += kv.value; + answer += kv.value_ptr.*; } - std.debug.print("{}\n", .{answer}); + return try std.fmt.allocPrint(allocator, "{}", .{answer}); } -pub const main = aoc.gen_main(part1, part2); +pub const main = aoc.main(part1, part2, .{ "4886706177792", "3348493585827" }); diff --git a/src/day14/part1 b/src/14/part1 index 5e2f6a1..5e2f6a1 100644 --- a/src/day14/part1 +++ b/src/14/part1 diff --git a/src/day14/part2 b/src/14/part2 index ee110aa..ee110aa 100644 --- a/src/day14/part2 +++ b/src/14/part2 diff --git a/src/day15/input b/src/15/input index 75e31b4..75e31b4 100644 --- a/src/day15/input +++ b/src/15/input diff --git a/src/day15/main.zig b/src/15/main.zig index b21c735..3fcb555 100644 --- a/src/day15/main.zig +++ b/src/15/main.zig @@ -3,14 +3,14 @@ const aoc = @import("aoc"); const Occurance = struct { last: u32, beforelast: u32 }; -fn getNthSpoken(allocator: *std.mem.Allocator, input: []u8, spoken: u32) !u32 { +fn getNthSpoken(allocator: std.mem.Allocator, input: []u8, spoken: u32) !u32 { var occurances = std.AutoHashMap(u32, Occurance).init(allocator); defer occurances.deinit(); var intlist = std.ArrayList(u32).init(allocator); defer intlist.deinit(); - var numit = std.mem.tokenize(input, "\n,"); + var numit = std.mem.tokenize(u8, input, "\n,"); var i: u32 = 0; while (numit.next()) |numstr| : (i += 1) { const num = try std.fmt.parseInt(u32, numstr, 10); @@ -20,18 +20,18 @@ fn getNthSpoken(allocator: *std.mem.Allocator, input: []u8, spoken: u32) !u32 { while (i < spoken) : (i += 1) { if (aoc.debug and i % 100000 == 0) - std.debug.print("\r{}", .{i}); + aoc.debugfmt("\r{}", .{i}); const num = intlist.items[i - 1]; var entry = occurances.getEntry(num); var diff: u32 = 0; if (entry) |occ_entry| { - diff = occ_entry.value.last - occ_entry.value.beforelast; + diff = occ_entry.value_ptr.last - occ_entry.value_ptr.beforelast; } entry = occurances.getEntry(diff); if (entry) |occ_entry| { - occ_entry.value.beforelast = occ_entry.value.last; - occ_entry.value.last = i; + occ_entry.value_ptr.beforelast = occ_entry.value_ptr.last; + occ_entry.value_ptr.last = i; } else { try occurances.put(diff, Occurance{ .last = i, .beforelast = i }); } @@ -39,17 +39,25 @@ fn getNthSpoken(allocator: *std.mem.Allocator, input: []u8, spoken: u32) !u32 { } if (aoc.debug) - std.debug.print("\r \r", .{}); + aoc.debugfmt("\r \r", .{}); return intlist.items[spoken - 1]; } -fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { - std.debug.print("{}\n", .{try getNthSpoken(allocator, input, 2020)}); +fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + + const answer = try getNthSpoken(allocator, input, 2020); + + return try std.fmt.allocPrint(allocator, "{}", .{answer}); } -fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { - std.debug.print("{}\n", .{try getNthSpoken(allocator, input, 30000000)}); +fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + + const answer = try getNthSpoken(allocator, input, 30000000); + + return try std.fmt.allocPrint(allocator, "{}", .{answer}); } -pub const main = aoc.gen_main(part1, part2); +pub const main = aoc.main(part1, part2, .{ "1259", "689" }); diff --git a/src/day15/part1 b/src/15/part1 index 7a0aa08..7a0aa08 100644 --- a/src/day15/part1 +++ b/src/15/part1 diff --git a/src/day15/part2 b/src/15/part2 index 3be53a9..3be53a9 100644 --- a/src/day15/part2 +++ b/src/15/part2 diff --git a/src/day16/input b/src/16/input index 4123288..4123288 100644 --- a/src/day16/input +++ b/src/16/input diff --git a/src/day16/input-test b/src/16/input-test index 88174f3..88174f3 100644 --- a/src/day16/input-test +++ b/src/16/input-test diff --git a/src/day16/main.zig b/src/16/main.zig index 0ab257b..1b45177 100644 --- a/src/day16/main.zig +++ b/src/16/main.zig @@ -19,13 +19,13 @@ fn parseRange(str: []const u8) !Range { } fn parseRules(rules: *std.ArrayList(FieldRule), str: []const u8) !void { - var lineit = std.mem.tokenize(str, "\n"); + var lineit = std.mem.tokenize(u8, str, "\n"); while (lineit.next()) |line| { const sep = try assert(usize, std.mem.indexOf(u8, line, ": ")); if (sep == line.len - 1) return aoc.Error.InvalidInput; const name = line[0..sep]; var rangepart = line[sep + 2 ..]; - var spaceit = std.mem.tokenize(rangepart, " "); + var spaceit = std.mem.tokenize(u8, rangepart, " "); const r1 = try parseRange(try assert([]const u8, spaceit.next())); _ = spaceit.next(); const r2 = try parseRange(try assert([]const u8, spaceit.next())); @@ -34,9 +34,9 @@ fn parseRules(rules: *std.ArrayList(FieldRule), str: []const u8) !void { } fn parseVals(vals: *std.ArrayList(u32), str: []const u8) !void { - var lineit = std.mem.tokenize(str, "\n"); + var lineit = std.mem.tokenize(u8, str, "\n"); while (lineit.next()) |line| { - var partit = std.mem.tokenize(line, ","); + var partit = std.mem.tokenize(u8, line, ","); while (partit.next()) |part| { try vals.append(try std.fmt.parseInt(u32, part, 10)); } @@ -48,7 +48,7 @@ fn matchesRule(rule: FieldRule, v: u32) bool { } fn hasRule(rules: []FieldRule, v: u32) bool { - for (rules) |r, i| { + for (rules) |r| { if (matchesRule(r, v)) return true; } return false; @@ -58,14 +58,16 @@ fn printCandidates(candidates: []u1, rulecount: usize, remove: usize) void { var i: u32 = 0; while (i < rulecount) : (i += 1) { for (candidates[i * rulecount .. (i + 1) * rulecount]) |c| { - std.debug.print("{} ", .{c}); + aoc.debugfmt("{} ", .{c}); } - if (i == remove) std.debug.print(" <<<", .{}); - std.debug.print("\n", .{}); + if (i == remove) aoc.debugfmt(" <<<", .{}); + aoc.debugfmt("\n", .{}); } } -fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + var answer: u32 = 0; var rules = std.ArrayList(FieldRule).init(allocator); @@ -74,7 +76,7 @@ fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { var othervals = std.ArrayList(u32).init(allocator); defer othervals.deinit(); - var secit = std.mem.split(input, "\n\n"); + var secit = std.mem.split(u8, input, "\n\n"); try parseRules(&rules, try assert([]const u8, secit.next())); _ = secit.next(); try parseVals(&othervals, (try assert([]const u8, secit.next()))[16..]); @@ -83,10 +85,12 @@ fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { if (!hasRule(rules.items, v)) answer += v; } - std.debug.print("{}\n", .{answer}); + return try std.fmt.allocPrint(allocator, "{}", .{answer}); } -fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + var rules = std.ArrayList(FieldRule).init(allocator); defer rules.deinit(); @@ -96,7 +100,7 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { var othervals = std.ArrayList(u32).init(allocator); defer othervals.deinit(); - var secit = std.mem.split(input, "\n\n"); + var secit = std.mem.split(u8, input, "\n\n"); try parseRules(&rules, try assert([]const u8, secit.next())); try parseVals(&ownvals, (try assert([]const u8, secit.next()))[13..]); try parseVals(&othervals, (try assert([]const u8, secit.next()))[16..]); @@ -106,7 +110,6 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { { var index: u32 = 0; while (index < othervals.items.len) { - const ri = index % rulecount; var i: u32 = 0; while (i < rulecount) : (i += 1) { if (!hasRule(rules.items, othervals.items[index + i])) @@ -117,15 +120,15 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { index += @intCast(u32, rulecount); } else { if (aoc.debug) - std.debug.print("REMOVE: ", .{}); + aoc.debugfmt("REMOVE: ", .{}); i = 0; while (i < rulecount) : (i += 1) { if (aoc.debug) - std.debug.print("{} ", .{othervals.items[index]}); + aoc.debugfmt("{} ", .{othervals.items[index]}); _ = othervals.orderedRemove(index); } if (aoc.debug) - std.debug.print("\n", .{}); + aoc.debugfmt("\n", .{}); } } } @@ -135,7 +138,7 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { std.mem.set(u1, candidates, 1); if (aoc.debug) - std.debug.print("{}\n", .{othervals.items.len}); + aoc.debugfmt("{}\n", .{othervals.items.len}); { var index: u32 = 0; while (index < othervals.items.len) : (index += 1) { @@ -164,13 +167,13 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { } if (aoc.debug) { printCandidates(candidates, rulecount, ri); - std.debug.print("\n", .{}); + aoc.debugfmt("\n", .{}); } } } } - std.debug.print("{}\n", .{answer}); + return try std.fmt.allocPrint(allocator, "{}", .{answer}); } -pub const main = aoc.gen_main(part1, part2); +pub const main = aoc.main(part1, part2, .{ "25972", "622670335901" }); diff --git a/src/day16/part1 b/src/16/part1 index 7de3ae1..7de3ae1 100644 --- a/src/day16/part1 +++ b/src/16/part1 diff --git a/src/day16/part2 b/src/16/part2 index 71dfbaf..71dfbaf 100644 --- a/src/day16/part2 +++ b/src/16/part2 diff --git a/src/day17/input b/src/17/input index 1ea63ff..1ea63ff 100644 --- a/src/day17/input +++ b/src/17/input diff --git a/src/day17/input-test b/src/17/input-test index eedd3d2..eedd3d2 100644 --- a/src/day17/input-test +++ b/src/17/input-test diff --git a/src/day17/main.zig b/src/17/main.zig index 39127c3..a6e1622 100644 --- a/src/day17/main.zig +++ b/src/17/main.zig @@ -26,7 +26,7 @@ fn forNeighbors(comptime dims: u32, map: *std.AutoHashMap(PosVec(dims), Active), fn posStr(comptime dims: u32, p: PosVec(dims)) void { var d: u32 = 0; while (d < dims) : (d += 1) { - std.debug.print("{} ", .{p.data[d]}); + aoc.debugfmt("{} ", .{p.data[d]}); } } @@ -57,14 +57,14 @@ fn addNew(comptime dims: u32) fn (oldmap: *std.AutoHashMap(PosVec(dims), Active) return impl.func; } -fn simulateRound(comptime dims: u32, map: *std.AutoHashMap(PosVec(dims), Active), allocator: *std.mem.Allocator) !void { +fn simulateRound(comptime dims: u32, map: *std.AutoHashMap(PosVec(dims), Active), allocator: std.mem.Allocator) !void { var newmap = std.AutoHashMap(PosVec(dims), Active).init(allocator); errdefer newmap.deinit(); var mapit = map.iterator(); while (mapit.next()) |kv| { - try forNeighbors(dims, map, kv.key, &newmap, addNew(dims)); - try addNew(dims)(map, kv.key, &newmap); + try forNeighbors(dims, map, kv.key_ptr.*, &newmap, addNew(dims)); + try addNew(dims)(map, kv.key_ptr.*, &newmap); } std.mem.swap(std.AutoHashMap(PosVec(dims), Active), map, &newmap); @@ -85,13 +85,13 @@ fn printMap(comptime dims: u32, map: *std.AutoHashMap(PosVec(dims), Active)) voi var mapit = map.iterator(); while (mapit.next()) |kv| { - if (min == null) min = kv.key; - if (max == null) max = kv.key; + if (min == null) min = kv.key_ptr.*; + if (max == null) max = kv.key_ptr.*; var d: u32 = 0; while (d < dims) : (d += 1) { - if (min.?.data[d] > kv.key.data[d]) min.?.data[d] = kv.key.data[d]; - if (max.?.data[d] < kv.key.data[d]) max.?.data[d] = kv.key.data[d]; + if (min.?.data[d] > kv.key_ptr.data[d]) min.?.data[d] = kv.key_ptr.data[d]; + if (max.?.data[d] < kv.key_ptr.data[d]) max.?.data[d] = kv.key_ptr.data[d]; } } @@ -114,12 +114,12 @@ fn printMap(comptime dims: u32, map: *std.AutoHashMap(PosVec(dims), Active)) voi } d = 2; - std.debug.print("Slice at: ", .{}); + aoc.debugfmt("Slice at: ", .{}); while (d < dims) : (d += 1) { - if (d > 2) std.debug.print(",", .{}); - std.debug.print("{}.Dim = {} ", .{ d + 1, np.data[d] }); + if (d > 2) aoc.debugfmt(",", .{}); + aoc.debugfmt("{}.Dim = {} ", .{ d + 1, np.data[d] }); } - std.debug.print("\n", .{}); + aoc.debugfmt("\n", .{}); var y = min.?.data[1]; while (y <= max.?.data[1]) : (y += 1) { @@ -130,19 +130,21 @@ fn printMap(comptime dims: u32, map: *std.AutoHashMap(PosVec(dims), Active)) voi v.data[1] = y; var c: u8 = '.'; if (map.get(v) != null) c = '#'; - std.debug.print("{c}", .{c}); + aoc.debugfmt("{c}", .{c}); } - std.debug.print("\n", .{}); + aoc.debugfmt("\n", .{}); } - std.debug.print("\n", .{}); + aoc.debugfmt("\n", .{}); } } -fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + var map = std.AutoHashMap(PosVec(3), Active).init(allocator); defer map.deinit(); - var lineit = std.mem.tokenize(input, "\n"); + var lineit = std.mem.tokenize(u8, input, "\n"); var y: i64 = 0; while (lineit.next()) |line| : (y += 1) { for (line) |c, x| { @@ -155,20 +157,21 @@ fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { while (i < 6) : (i += 1) { try simulateRound(3, &map, allocator); if (aoc.debug) { - std.debug.print("AFTER ROUND {}:\n", .{i + 1}); + aoc.debugfmt("AFTER ROUND {}:\n", .{i + 1}); printMap(3, &map); } } - var answer: u32 = map.count(); - std.debug.print("{}\n", .{answer}); + return try std.fmt.allocPrint(allocator, "{}", .{map.count()}); } -fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + var map = std.AutoHashMap(PosVec(4), Active).init(allocator); defer map.deinit(); - var lineit = std.mem.tokenize(input, "\n"); + var lineit = std.mem.tokenize(u8, input, "\n"); var y: i64 = 0; while (lineit.next()) |line| : (y += 1) { for (line) |c, x| { @@ -181,13 +184,12 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { while (i < 6) : (i += 1) { try simulateRound(4, &map, allocator); if (aoc.debug) { - std.debug.print("AFTER ROUND {}:\n", .{i + 1}); + aoc.debugfmt("AFTER ROUND {}:\n", .{i + 1}); printMap(4, &map); } } - var answer: u32 = map.count(); - std.debug.print("{}\n", .{answer}); + return try std.fmt.allocPrint(allocator, "{}", .{map.count()}); } -pub const main = aoc.gen_main(part1, part2); +pub const main = aoc.main(part1, part2, .{ "304", "1868" }); diff --git a/src/day17/part1 b/src/17/part1 index 0e7db4a..0e7db4a 100644 --- a/src/day17/part1 +++ b/src/17/part1 diff --git a/src/day17/part2 b/src/17/part2 index a824996..a824996 100644 --- a/src/day17/part2 +++ b/src/17/part2 diff --git a/src/day18/input b/src/18/input index 2ceeee9..2ceeee9 100644 --- a/src/day18/input +++ b/src/18/input diff --git a/src/day18/input-test b/src/18/input-test index 792bc09..792bc09 100644 --- a/src/day18/input-test +++ b/src/18/input-test diff --git a/src/day18/input-test2 b/src/18/input-test2 index ea858ad..ea858ad 100644 --- a/src/day18/input-test2 +++ b/src/18/input-test2 diff --git a/src/day18/main.zig b/src/18/main.zig index fb5e294..913a657 100644 --- a/src/day18/main.zig +++ b/src/18/main.zig @@ -4,23 +4,19 @@ const std = @import("std"); const aoc = @import("aoc"); const OpType = enum { ADD, MULT, COUNT }; -const Op = struct { - prio: u32, optype: OpType -}; -const Item = union(enum) { - num: i64, op: OpType -}; +const Op = struct { prio: u32, optype: OpType }; +const Item = union(enum) { num: i64, op: OpType }; const op_names = [_][]const u8{ "ADD", "MULT" }; fn printStack(stack: []Item) void { for (stack) |item| { switch (item) { - .op => std.debug.print("{} ", .{op_names[@enumToInt(item.op)]}), - .num => std.debug.print("{} ", .{item.num}), + .op => aoc.debugfmt("{s} ", .{op_names[@enumToInt(item.op)]}), + .num => aoc.debugfmt("{} ", .{item.num}), } } - std.debug.print("\n", .{}); + aoc.debugfmt("\n", .{}); } fn parseToken(line: []const u8, i: *usize, depth: *u32, op_prio: []const u32, mathstack: *std.ArrayList(Item), opstack: *std.ArrayList(Op)) !bool { @@ -63,6 +59,8 @@ fn parseToken(line: []const u8, i: *usize, depth: *u32, op_prio: []const u32, ma } fn reduceStack(mathstack: *std.ArrayList(Item), opstack: *std.ArrayList(Op), tmpstack: *std.ArrayList(Item)) !void { + _ = opstack; + while (mathstack.items.len > 0) { while (mathstack.items[mathstack.items.len - 1] != .op and tmpstack.items.len > 0) @@ -97,14 +95,14 @@ fn reduceStack(mathstack: *std.ArrayList(Item), opstack: *std.ArrayList(Op), tmp } } -fn printRPN(allocator: *std.mem.Allocator, input: []u8, prios: []const u32) !void { +fn printRPN(allocator: std.mem.Allocator, input: []u8, prios: []const u32) !void { var mathstack = std.ArrayList(Item).init(allocator); defer mathstack.deinit(); var opstack = std.ArrayList(Op).init(allocator); defer opstack.deinit(); - var lineit = std.mem.tokenize(input, "\n"); + var lineit = std.mem.tokenize(u8, input, "\n"); while (lineit.next()) |line| { var depth: u32 = 0; var i: usize = 0; @@ -114,11 +112,11 @@ fn printRPN(allocator: *std.mem.Allocator, input: []u8, prios: []const u32) !voi printStack(mathstack.items); } -fn partProto(allocator: *std.mem.Allocator, input: []u8, prios: []const u32) !void { +fn partProto(allocator: std.mem.Allocator, input: []u8, prios: []const u32) !?[]u8 { var answer: i64 = 0; if (aoc.debug) { - std.debug.print("Reverse Polish Notation:\n", .{}); + aoc.debugfmt("Reverse Polish Notation:\n", .{}); try printRPN(allocator, input, prios); } @@ -131,7 +129,7 @@ fn partProto(allocator: *std.mem.Allocator, input: []u8, prios: []const u32) !vo var tmpstack = std.ArrayList(Item).init(allocator); defer tmpstack.deinit(); - var lineit = std.mem.tokenize(input, "\n"); + var lineit = std.mem.tokenize(u8, input, "\n"); while (lineit.next()) |line| { var depth: u32 = 0; var i: usize = 0; @@ -149,17 +147,23 @@ fn partProto(allocator: *std.mem.Allocator, input: []u8, prios: []const u32) !vo try tmpstack.resize(0); } - std.debug.print("{}\n", .{answer}); + return try std.fmt.allocPrint(allocator, "{}", .{answer}); } -fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + const prios = [_]u32{ 1, 1 }; - try partProto(allocator, input, prios[0..]); + + return try partProto(allocator, input, prios[0..]); } -fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + const prios = [_]u32{ 2, 1 }; - try partProto(allocator, input, prios[0..]); + + return try partProto(allocator, input, prios[0..]); } test "unions" { @@ -170,4 +174,4 @@ test "unions" { expect(item2 == .num); } -pub const main = aoc.gen_main(part1, part2); +pub const main = aoc.main(part1, part2, .{ "5783053349377", "74821486966872" }); diff --git a/src/day18/part1 b/src/18/part1 index 7bff52b..7bff52b 100644 --- a/src/day18/part1 +++ b/src/18/part1 diff --git a/src/day18/part2 b/src/18/part2 index 4110043..4110043 100644 --- a/src/day18/part2 +++ b/src/18/part2 diff --git a/src/day19/input b/src/19/input index f812951..f812951 100644 --- a/src/day19/input +++ b/src/19/input diff --git a/src/day19/input-test b/src/19/input-test index 1500b6f..1500b6f 100644 --- a/src/day19/input-test +++ b/src/19/input-test diff --git a/src/day19/input-test2 b/src/19/input-test2 index 04b5e07..04b5e07 100644 --- a/src/day19/input-test2 +++ b/src/19/input-test2 diff --git a/src/day19/input-test3 b/src/19/input-test3 index 8c931b1..8c931b1 100644 --- a/src/day19/input-test3 +++ b/src/19/input-test3 diff --git a/src/day19/main.zig b/src/19/main.zig index 45e8348..56c01d4 100644 --- a/src/day19/main.zig +++ b/src/19/main.zig @@ -9,9 +9,9 @@ const Rule = union(enum) { fn printSlice(slice: []const usize) void { for (slice) |v, i| { if (i > 0) { - std.debug.print(" ", .{}); + aoc.debugfmt(" ", .{}); } - std.debug.print("{}", .{v}); + aoc.debugfmt("{}", .{v}); } } @@ -48,22 +48,22 @@ fn checkRule(rules: *std.AutoHashMap(usize, Rule), rid: usize, msg: []const u8, return false; } -fn freeRules(allocator: *std.mem.Allocator, rules: *std.AutoHashMap(usize, Rule), ignore: ?[]const usize) void { +fn freeRules(allocator: std.mem.Allocator, rules: *std.AutoHashMap(usize, Rule), ignore: ?[]const usize) void { var mapit = rules.iterator(); while (mapit.next()) |kv| { - if (ignore != null and std.mem.indexOfScalar(usize, ignore.?, kv.key) != null) continue; - if (kv.value == .combos) { - for (kv.value.combos) |rs| { + if (ignore != null and std.mem.indexOfScalar(usize, ignore.?, kv.key_ptr.*) != null) continue; + if (kv.value_ptr.* == .combos) { + for (kv.value_ptr.combos) |rs| { allocator.free(rs); } - allocator.free(kv.value.combos); + allocator.free(kv.value_ptr.combos); } } rules.deinit(); } -fn parseRule(allocator: *std.mem.Allocator, rules: *std.AutoHashMap(usize, Rule), line: []const u8) !void { - const indexsep = try aoc.assertV(std.mem.indexOf(u8, line, ": ")); +fn parseRule(allocator: std.mem.Allocator, rules: *std.AutoHashMap(usize, Rule), line: []const u8) !void { + const indexsep = try aoc.unwrap(std.mem.indexOf(u8, line, ": ")); const index = try std.fmt.parseInt(usize, line[0..indexsep], 10); var chrsep = std.mem.indexOf(u8, line, "\""); @@ -74,12 +74,12 @@ fn parseRule(allocator: *std.mem.Allocator, rules: *std.AutoHashMap(usize, Rule) var vals = std.ArrayList([]usize).init(allocator); errdefer vals.deinit(); - var ruleit = std.mem.split(line[indexsep + 2 ..], " | "); + var ruleit = std.mem.split(u8, line[indexsep + 2 ..], " | "); while (ruleit.next()) |r| { var ruleids = std.ArrayList(usize).init(allocator); errdefer ruleids.deinit(); - var spaceit = std.mem.tokenize(r, " "); + var spaceit = std.mem.tokenize(u8, r, " "); while (spaceit.next()) |word| { try ruleids.append(try std.fmt.parseInt(usize, word, 10)); } @@ -93,13 +93,15 @@ fn parseRule(allocator: *std.mem.Allocator, rules: *std.AutoHashMap(usize, Rule) } } -fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + var rules = std.AutoHashMap(usize, Rule).init(allocator); defer freeRules(allocator, &rules, null); - var partit = std.mem.split(input, "\n\n"); + var partit = std.mem.split(u8, input, "\n\n"); - var lineit = std.mem.tokenize(try aoc.assertV(partit.next()), "\n"); + var lineit = std.mem.tokenize(u8, try aoc.unwrap(partit.next()), "\n"); while (lineit.next()) |line| { try parseRule(allocator, &rules, line); } @@ -111,7 +113,7 @@ fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { stacktrace = std.ArrayList(usize).init(allocator); defer stacktrace.deinit(); - lineit = std.mem.tokenize(try aoc.assertV(partit.next()), "\n"); + lineit = std.mem.tokenize(u8, try aoc.unwrap(partit.next()), "\n"); var count: usize = 0; while (lineit.next()) |line| { count += 1; @@ -120,16 +122,18 @@ fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { try nextstack.resize(0); } - std.debug.print("{} / {} matching\n", .{ answer, count }); + return try std.fmt.allocPrint(allocator, "{}", .{answer}); } -fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + var rules = std.AutoHashMap(usize, Rule).init(allocator); defer freeRules(allocator, &rules, ([_]usize{ 8, 11 })[0..]); - var partit = std.mem.split(input, "\n\n"); + var partit = std.mem.split(u8, input, "\n\n"); - var lineit = std.mem.tokenize(try aoc.assertV(partit.next()), "\n"); + var lineit = std.mem.tokenize(u8, try aoc.unwrap(partit.next()), "\n"); while (lineit.next()) |line| { if (std.mem.eql(u8, line[0..2], "8:")) { const p1: []const usize = ([_]usize{42})[0..]; @@ -152,26 +156,26 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { defer stacktrace.deinit(); var count: usize = 0; - lineit = std.mem.tokenize(try aoc.assertV(partit.next()), "\n"); + lineit = std.mem.tokenize(u8, try aoc.unwrap(partit.next()), "\n"); while (lineit.next()) |line| { count += 1; const valid = try checkRule(&rules, 0, line, &nextstack); answer += @boolToInt(valid); if (aoc.debug) { - std.debug.print("TRACE: [", .{}); + aoc.debugfmt("TRACE: [", .{}); std.mem.reverse(usize, stacktrace.items); printSlice(stacktrace.items); - std.debug.print("]\n", .{}); + aoc.debugfmt("]\n", .{}); if (valid) { - std.debug.print("MATCH {}\n\n", .{line}); + aoc.debugfmt("MATCH {s}\n\n", .{line}); } else { - std.debug.print("NO MATCH {}\n\n", .{line}); + aoc.debugfmt("NO MATCH {s}\n\n", .{line}); } } try nextstack.resize(0); } - std.debug.print("{} / {} matching\n", .{ answer, count }); + return try std.fmt.allocPrint(allocator, "{}", .{answer}); } -pub const main = aoc.gen_main(part1, part2); +pub const main = aoc.main(part1, part2, .{ "134", "377" }); diff --git a/src/day19/part1 b/src/19/part1 index 22eba35..22eba35 100644 --- a/src/day19/part1 +++ b/src/19/part1 diff --git a/src/day19/part2 b/src/19/part2 index 6f056a4..6f056a4 100644 --- a/src/day19/part2 +++ b/src/19/part2 diff --git a/src/day19/trace b/src/19/trace index 5373679..5373679 100644 --- a/src/day19/trace +++ b/src/19/trace diff --git a/src/day20/input b/src/20/input index 251acfc..251acfc 100644 --- a/src/day20/input +++ b/src/20/input diff --git a/src/day20/input-test b/src/20/input-test index 546e71d..546e71d 100644 --- a/src/day20/input-test +++ b/src/20/input-test diff --git a/src/day20/input-test2 b/src/20/input-test2 index 546e71d..546e71d 100644 --- a/src/day20/input-test2 +++ b/src/20/input-test2 diff --git a/src/day20/main.zig b/src/20/main.zig index 1077cdf..badad27 100644 --- a/src/day20/main.zig +++ b/src/20/main.zig @@ -29,28 +29,28 @@ fn printGrid(tiles: []Tile, grid: []?usize, grid_width: u32) void { var y: u32 = 0; while (y < grid_width * tile_width) : (y += 1) { var x: u32 = 0; - if (y > 0 and y % tile_width == 0) std.debug.print("\n", .{}); + if (y > 0 and y % tile_width == 0) aoc.debugfmt("\n", .{}); while (x < grid_width * tile_width) : (x += 1) { - if (x > 0 and x % tile_width == 0) std.debug.print(" ", .{}); + if (x > 0 and x % tile_width == 0) aoc.debugfmt(" ", .{}); var tile_index: ?usize = grid[@divFloor(y, tile_width) * grid_width + @divFloor(x, tile_width)]; - if (tile_index) |ti| { + if (tile_index != null) { var tile = tiles[tile_index.?]; var c: u8 = if (tile.data[(y % tile_width) * tile_width + (x % tile_width)] == 1) '#' else '.'; - std.debug.print("{c}", .{c}); + aoc.debugfmt("{c}", .{c}); } else { - std.debug.print("?", .{}); + aoc.debugfmt("?", .{}); } } - std.debug.print("\n", .{}); + aoc.debugfmt("\n", .{}); } - std.debug.print("\n\n", .{}); + aoc.debugfmt("\n\n", .{}); } fn printTile(data: []u1, width: u32) void { var y: u32 = 0; while (y < width) : (y += 1) { printTileSlice(data[y * width .. (y + 1) * width]); - std.debug.print("\n", .{}); + aoc.debugfmt("\n", .{}); } } @@ -58,7 +58,7 @@ fn printTileSlice(slice: []const u1) void { var i: u32 = 0; while (i < slice.len) : (i += 1) { var c: u8 = if (slice[i] == 1) '#' else '.'; - std.debug.print("{c}", .{c}); + aoc.debugfmt("{c}", .{c}); } } @@ -71,21 +71,21 @@ fn couldMatch(side1: [tile_width]u1, side2: [tile_width]u1) bool { } fn parseTiles(tiles: *std.ArrayList(Tile), input: []u8) !void { - var tileit = std.mem.split(input, "\n\n"); + var tileit = std.mem.split(u8, input, "\n\n"); while (tileit.next()) |tilestr| { if (tilestr.len <= 1) continue; var tile: Tile = undefined; - var lineit = std.mem.tokenize(tilestr, "\n"); + var lineit = std.mem.tokenize(u8, tilestr, "\n"); // read tile id - var numstr = (try aoc.assertV(lineit.next()))[5..9]; + var numstr = (try aoc.unwrap(lineit.next()))[5..9]; tile.id = try std.fmt.parseInt(u32, numstr, 10); // read tile data var i: u32 = 0; var line: []const u8 = undefined; while (i < tile_width * tile_width) : (i += 1) { - if (i % tile_width == 0) line = try aoc.assertV(lineit.next()); + if (i % tile_width == 0) line = try aoc.unwrap(lineit.next()); tile.data[i] = @boolToInt(line[i % tile_width] == '#'); } @@ -130,24 +130,24 @@ fn matchTiles(tiles: *std.ArrayList(Tile)) void { } if (aoc.debug) { - std.debug.print("{}:\nSIDES ", .{tile.id}); + aoc.debugfmt("{}:\nSIDES ", .{tile.id}); for (tile.sides) |side| { for (side) |b| { var c: u8 = if (b == 1) '#' else '.'; - std.debug.print("{c}", .{c}); + aoc.debugfmt("{c}", .{c}); } - std.debug.print(" ", .{}); + aoc.debugfmt(" ", .{}); } - std.debug.print("\n", .{}); + aoc.debugfmt("\n", .{}); for (tile.adj) |adj, i| { if (tile.adj[i] == null) { - std.debug.print("null ", .{}); + aoc.debugfmt("null ", .{}); } else { var adjtile = tiles.items[adj.?]; - std.debug.print("{} ", .{adjtile.id}); + aoc.debugfmt("{} ", .{adjtile.id}); } } - std.debug.print("\n\n", .{}); + aoc.debugfmt("\n\n", .{}); } } } @@ -156,28 +156,28 @@ fn checkSides(tiles: []Tile, grid: []?usize, grid_width: u32, p: aoc.Pos, adj: [ var rot: u32 = 0; while (rot < 4) : (rot += 1) { const np = p.add(dirs[rot]); - std.debug.print("{}\n", .{@intToEnum(aoc.Dir.Name, @intCast(u2, rot))}); - std.debug.print("{} {}\n", .{ np.x, np.y }); + aoc.debugfmt("{}\n", .{@intToEnum(aoc.Dir.Name, @intCast(u2, rot))}); + aoc.debugfmt("{} {}\n", .{ np.x, np.y }); - var side: ?[tile_width]u1 = undefined; if (np.x < 0 or np.x >= grid_width or np.y < 0 or np.y >= grid_width) { - std.debug.print("Side is null\n", .{}); + aoc.debugfmt("Side is null\n", .{}); if (adj[rot] == null) { continue; } else { - std.debug.print("Side {} should be NULL!\n", .{@intToEnum(aoc.Dir.Name, @intCast(u2, rot))}); + aoc.debugfmt("Side {} should be NULL!\n", .{@intToEnum(aoc.Dir.Name, @intCast(u2, rot))}); return false; } } - var ti: ?usize = grid[@intCast(u32, np.y * @intCast(i64, grid_width) + np.x)] orelse continue; + var ti: ?usize = grid[@intCast(u32, np.y * @intCast(i64, grid_width) + np.x)]; + if (ti == null) continue; var otherside = tiles[ti.?].sides[aoc.Dir.nextCW(rot, 2)]; std.mem.reverse(u1, otherside[0..]); printTileSlice(otherside[0..]); - std.debug.print(" vs ", .{}); + aoc.debugfmt(" vs ", .{}); printTileSlice(sides[rot][0..]); - std.debug.print("\n", .{}); + aoc.debugfmt("\n", .{}); if (!std.mem.eql(u1, otherside[0..], sides[rot][0..])) return false; } @@ -231,7 +231,7 @@ fn flipData(data: []u1, width: u32, flip: Flip) void { } } -fn manipulateAndPlace(allocator: *std.mem.Allocator, tiles: []Tile, grid: []?usize, grid_width: u32, p: aoc.Pos, tile_index: usize) !void { +fn manipulateAndPlace(allocator: std.mem.Allocator, tiles: []Tile, grid: []?usize, grid_width: u32, p: aoc.Pos, tile_index: usize) !void { var tile = &tiles[tile_index]; for (dirs) |_, rot| { @@ -269,12 +269,12 @@ fn manipulateAndPlace(allocator: *std.mem.Allocator, tiles: []Tile, grid: []?usi tmp_adj[i] = adj[aoc.Dir.nextCCW(i, rot)]; } - std.debug.print("\nROT: {}\n", .{rot}); - std.debug.print("FLIP: {}\n", .{flip}); - std.debug.print("SIDES: \n", .{}); + aoc.debugfmt("\nROT: {}\n", .{rot}); + aoc.debugfmt("FLIP: {}\n", .{flip}); + aoc.debugfmt("SIDES: \n", .{}); for (tmp_sides) |side| { printTileSlice(side[0..]); - std.debug.print("\n", .{}); + aoc.debugfmt("\n", .{}); } const valid = checkSides(tiles, grid, grid_width, p, tmp_adj, tmp_sides); @@ -282,11 +282,11 @@ fn manipulateAndPlace(allocator: *std.mem.Allocator, tiles: []Tile, grid: []?usi if (valid) { // for (tile.sides) |side| { // printTileSlice(side[0..]); - // std.debug.print("\n", .{}); + // aoc.debugfmt("\n", .{}); // } - std.debug.print("{} {} {}\n", .{ tile.id, flip, rot }); + aoc.debugfmt("{} {} {}\n", .{ tile.id, flip, rot }); printTile(tile.data[0..], tile_width); - std.debug.print("\n", .{}); + aoc.debugfmt("\n", .{}); tile.sides = tmp_sides; tile.pos = p; tile.adj = tmp_adj; @@ -306,7 +306,9 @@ fn manipulateAndPlace(allocator: *std.mem.Allocator, tiles: []Tile, grid: []?usi return aoc.Error.InvalidInput; } -fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + var tiles = std.ArrayList(Tile).init(allocator); defer tiles.deinit(); @@ -323,10 +325,12 @@ fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { if (unknown == 2) answer *= tile.id; } - std.debug.print("{}\n", .{answer}); + return try std.fmt.allocPrint(allocator, "{}", .{answer}); } -fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + var tiles = std.ArrayList(Tile).init(allocator); defer tiles.deinit(); @@ -335,6 +339,7 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { matchTiles(&tiles); var grid = try allocator.alloc(?usize, tiles.items.len); + std.mem.set(?usize, grid, null); defer allocator.free(grid); var grid_width = @intCast(u32, std.math.sqrt(tiles.items.len)); @@ -363,22 +368,22 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { if (aoc.debug) printGrid(tiles.items, grid, grid_width); for (grid) |_, gi| { - std.debug.print("POS {}\n", .{gi}); + aoc.debugfmt("POS {}\n", .{gi}); const tile_index: ?usize = grid[gi] orelse continue; - std.debug.print("POS {} YES\n", .{gi}); + aoc.debugfmt("POS {} YES\n", .{gi}); var placed = &tiles.items[tile_index.?]; for (placed.adj) |want, dir| { if (want == null) continue; - // std.debug.print("LOOKING FOR {}\n", .{want.?}); + // aoc.debugfmt("LOOKING FOR {}\n", .{want.?}); // for (left.items) |item| { - // std.debug.print("> {}\n", .{item}); + // aoc.debugfmt("> {}\n", .{item}); // } if (std.mem.indexOfScalar(usize, left.items, want.?)) |left_index| { const new_index = left.items[left_index]; const new_tile = tiles.items[new_index]; var npos = placed.pos.add(dirs[dir]); - std.debug.print("TRYING TO PLACE {} NEW AT: {} {}\n", .{ new_tile.id, npos.x, npos.y }); + aoc.debugfmt("TRYING TO PLACE {} NEW AT: {} {}\n", .{ new_tile.id, npos.x, npos.y }); try manipulateAndPlace(allocator, tiles.items, grid, grid_width, npos, new_index); @@ -416,7 +421,7 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { y = 0; while (y < grid_width * (tile_width - 2)) : (y += 1) { printTileSlice(image[y * grid_width * (tile_width - 2) .. (y + 1) * grid_width * (tile_width - 2)]); - std.debug.print("\n", .{}); + aoc.debugfmt("\n", .{}); } // rotate image till we find a sea monster @@ -453,11 +458,13 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { while (sy < seamonster.len) : (sy += 1) { seamonster_count += std.mem.count(u8, seamonster[sy], "#"); } - std.debug.print("{}\n", .{image_count - count * seamonster_count}); - return; + const answer = image_count - count * seamonster_count; + return try std.fmt.allocPrint(allocator, "{}", .{answer}); } } } + + return null; } -pub const main = aoc.gen_main(part1, part2); +pub const main = aoc.main(part1, part2, .{ "54755174472007", "1692" }); diff --git a/src/day20/part1 b/src/20/part1 index 9b3cc8c..9b3cc8c 100644 --- a/src/day20/part1 +++ b/src/20/part1 diff --git a/src/day20/part2 b/src/20/part2 index c3b48ba..c3b48ba 100644 --- a/src/day20/part2 +++ b/src/20/part2 diff --git a/src/day21/input b/src/21/input index 9301dc4..9301dc4 100644 --- a/src/day21/input +++ b/src/21/input diff --git a/src/day21/input-test b/src/21/input-test index 1c0ea2f..1c0ea2f 100644 --- a/src/day21/input-test +++ b/src/21/input-test diff --git a/src/day21/main.zig b/src/21/main.zig index 2e59e7d..d64252a 100644 --- a/src/day21/main.zig +++ b/src/21/main.zig @@ -10,7 +10,7 @@ const Pair = struct { ingredient: []const u8, allergen: []const u8 }; fn freeListmap(listmap: *std.AutoHashMap(u32, std.ArrayList(u32))) void { var mapit = listmap.iterator(); while (mapit.next()) |kv| { - kv.value.deinit(); + kv.value_ptr.deinit(); } listmap.deinit(); } @@ -18,13 +18,13 @@ fn freeListmap(listmap: *std.AutoHashMap(u32, std.ArrayList(u32))) void { fn getAllergen(allergens: *std.AutoHashMap(u32, std.ArrayList(u32)), ingredient: u32) ?u32 { var allergenit = allergens.iterator(); while (allergenit.next()) |alg| { - if (alg.value.items.len == 1 and alg.value.items[0] == ingredient) - return alg.key; + if (alg.value_ptr.items.len == 1 and alg.value_ptr.items[0] == ingredient) + return alg.key_ptr.*; } return null; } -fn updateAllergens(allocator: *std.mem.Allocator, allergens: *std.AutoHashMap(u32, std.ArrayList(u32)), allergen: u32) !void { +fn updateAllergens(allocator: std.mem.Allocator, allergens: *std.AutoHashMap(u32, std.ArrayList(u32)), allergen: u32) !void { var next = std.ArrayList(u32).init(allocator); defer next.deinit(); @@ -32,15 +32,15 @@ fn updateAllergens(allocator: *std.mem.Allocator, allergens: *std.AutoHashMap(u3 while (next.items.len > 0) { const key = next.items[0]; - var ingredient = allergens.getEntry(key).?.value.items[0]; + var ingredient = allergens.getEntry(key).?.value_ptr.items[0]; var mapit = allergens.iterator(); while (mapit.next()) |alg| { - if (alg.key == key) continue; - const ind = std.mem.indexOfScalar(u32, alg.value.items, ingredient); + if (alg.key_ptr.* == key) continue; + const ind = std.mem.indexOfScalar(u32, alg.value_ptr.items, ingredient); if (ind != null) { - _ = alg.value.swapRemove(ind.?); - if (alg.value.items.len == 1) { - try next.append(alg.key); + _ = alg.value_ptr.swapRemove(ind.?); + if (alg.value_ptr.items.len == 1) { + try next.append(alg.key_ptr.*); } } } @@ -48,17 +48,17 @@ fn updateAllergens(allocator: *std.mem.Allocator, allergens: *std.AutoHashMap(u3 } } -fn parseAllergens(allocator: *std.mem.Allocator, allergens: *std.AutoHashMap(u32, std.ArrayList(u32)), unhash: *std.AutoHashMap(u32, []const u8), ingredient_counts: *std.AutoHashMap(u32, u32), input: []const u8) !void { +fn parseAllergens(allocator: std.mem.Allocator, allergens: *std.AutoHashMap(u32, std.ArrayList(u32)), unhash: *std.AutoHashMap(u32, []const u8), ingredient_counts: *std.AutoHashMap(u32, u32), input: []const u8) !void { var ingredients = std.ArrayList(u32).init(allocator); defer ingredients.deinit(); - var lineit = std.mem.tokenize(input, "\n"); + var lineit = std.mem.tokenize(u8, input, "\n"); while (lineit.next()) |line| { var allergen = false; try ingredients.resize(0); - var spaceit = std.mem.tokenize(line, " "); + var spaceit = std.mem.tokenize(u8, line, " "); while (spaceit.next()) |word| { if (std.mem.eql(u8, word, "(contains")) { allergen = true; @@ -72,16 +72,16 @@ fn parseAllergens(allocator: *std.mem.Allocator, allergens: *std.AutoHashMap(u32 var algentry = allergens.getEntry(allergen_hash); if (algentry) |algent| { var i: u32 = 0; - while (i < algent.value.items.len) { - if (std.mem.indexOfScalar(u32, ingredients.items, algent.value.items[i]) == null) { - _ = algent.value.swapRemove(i); + while (i < algent.value_ptr.items.len) { + if (std.mem.indexOfScalar(u32, ingredients.items, algent.value_ptr.items[i]) == null) { + _ = algent.value_ptr.swapRemove(i); } else { i += 1; } } - if (algent.value.items.len == 1) { - try updateAllergens(allocator, allergens, algent.key); + if (algent.value_ptr.items.len == 1) { + try updateAllergens(allocator, allergens, algent.key_ptr.*); } } else { var copy = std.ArrayList(u32).init(allocator); @@ -101,7 +101,7 @@ fn parseAllergens(allocator: *std.mem.Allocator, allergens: *std.AutoHashMap(u32 if (entry == null) { try ingredient_counts.put(ingredient_hash, 1); } else { - entry.?.value += 1; + entry.?.value_ptr.* += 1; } } } @@ -109,12 +109,15 @@ fn parseAllergens(allocator: *std.mem.Allocator, allergens: *std.AutoHashMap(u32 } fn ascendingAllergen(ctx: void, p1: Pair, p2: Pair) bool { + _ = ctx; var i: u32 = 0; while (i < std.math.min(p1.allergen.len, p2.allergen.len) and p1.allergen[i] == p2.allergen[i]) : (i += 1) {} return p1.allergen[i] < p2.allergen[i]; } -fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + var allergens = std.AutoHashMap(u32, std.ArrayList(u32)).init(allocator); defer freeListmap(&allergens); @@ -130,22 +133,24 @@ fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { var algit = allergens.iterator(); while (algit.next()) |alg| { - std.debug.print("{} - ", .{alg.key}); - for (alg.value.items) |v| { - std.debug.print("{} ", .{v}); + aoc.debugfmt("{} - ", .{alg.key_ptr.*}); + for (alg.value_ptr.items) |v| { + aoc.debugfmt("{} ", .{v}); } - std.debug.print("\n", .{}); + aoc.debugfmt("\n", .{}); } var mapit = ingredient_counts.iterator(); while (mapit.next()) |kv| { - if (getAllergen(&allergens, kv.key) == null) answer += kv.value; + if (getAllergen(&allergens, kv.key_ptr.*) == null) answer += kv.value_ptr.*; } - std.debug.print("{}\n", .{answer}); + return try std.fmt.allocPrint(allocator, "{}", .{answer}); } -fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + var allergens = std.AutoHashMap(u32, std.ArrayList(u32)).init(allocator); defer freeListmap(&allergens); @@ -162,19 +167,22 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { var mapit = ingredient_counts.iterator(); while (mapit.next()) |kv| { - const alg = getAllergen(&allergens, kv.key); + const alg = getAllergen(&allergens, kv.key_ptr.*); if (alg != null) { - try pairs.append(Pair{ .ingredient = unhash.get(kv.key).?, .allergen = unhash.get(alg.?).? }); + try pairs.append(Pair{ .ingredient = unhash.get(kv.key_ptr.*).?, .allergen = unhash.get(alg.?).? }); } } std.sort.sort(Pair, pairs.items, {}, ascendingAllergen); + var parts = std.ArrayList([]const u8).init(allocator); + defer parts.deinit(); for (pairs.items) |p, i| { - if (i > 0) std.debug.print(",", .{}); - std.debug.print("{}", .{p.ingredient}); + if (i > 0) try parts.append(","); + try parts.append(p.ingredient); } - std.debug.print("\n", .{}); + return try std.mem.join(allocator, "", parts.items); } -pub const main = aoc.gen_main(part1, part2); +const sol2 = "fqhpsl,zxncg,clzpsl,zbbnj,jkgbvlxh,dzqc,ppj,glzb"; +pub const main = aoc.main(part1, part2, .{ "2584", sol2 }); diff --git a/src/day21/part1 b/src/21/part1 index 1f4bd6c..1f4bd6c 100644 --- a/src/day21/part1 +++ b/src/21/part1 diff --git a/src/day21/part2 b/src/21/part2 index 0e35f5d..0e35f5d 100644 --- a/src/day21/part2 +++ b/src/21/part2 diff --git a/src/day22/input b/src/22/input index cb0c4df..cb0c4df 100644 --- a/src/day22/input +++ b/src/22/input diff --git a/src/day22/input-test b/src/22/input-test index 391cd24..391cd24 100644 --- a/src/day22/input-test +++ b/src/22/input-test diff --git a/src/day22/input-test2 b/src/22/input-test2 index a29e13a..a29e13a 100644 --- a/src/day22/input-test2 +++ b/src/22/input-test2 diff --git a/src/day22/main.zig b/src/22/main.zig index 777e461..dc646b0 100644 --- a/src/day22/main.zig +++ b/src/22/main.zig @@ -2,16 +2,14 @@ const std = @import("std"); const aoc = @import("aoc"); const Player = enum { P1, P2 }; -const GameResult = struct { - score: u64, winner: Player -}; +const GameResult = struct { score: u64, winner: Player }; fn parseInput(input: []u8, decks: [2]*std.ArrayList(u32)) !void { - var partit = std.mem.split(input, "\n\n"); + var partit = std.mem.split(u8, input, "\n\n"); var decknum: u32 = 0; while (partit.next()) |part| : (decknum += 1) { if (decknum == 2) break; - var lineit = std.mem.tokenize(part, "\n"); + var lineit = std.mem.tokenize(u8, part, "\n"); _ = lineit.next(); while (lineit.next()) |line| { try decks[decknum].append(try std.fmt.parseInt(u32, line, 10)); @@ -22,23 +20,23 @@ fn parseInput(input: []u8, decks: [2]*std.ArrayList(u32)) !void { fn printDeck(deck: *std.ArrayList(u32)) void { for (deck.items) |v, i| { - if (i > 0) std.debug.print(", ", .{}); - std.debug.print("{}", .{v}); + if (i > 0) aoc.debugfmt(", ", .{}); + aoc.debugfmt("{}", .{v}); } } fn printRoundInfo(roundnum: u32, decks: [2]*std.ArrayList(u32)) void { - std.debug.print("\n-- ROUND {} --\n", .{roundnum}); - std.debug.print("Player 1's deck: ", .{}); + aoc.debugfmt("\n-- ROUND {} --\n", .{roundnum}); + aoc.debugfmt("Player 1's deck: ", .{}); printDeck(decks[0]); - std.debug.print("\nPlayer 2's deck: ", .{}); + aoc.debugfmt("\nPlayer 2's deck: ", .{}); printDeck(decks[1]); - std.debug.print("\n", .{}); + aoc.debugfmt("\n", .{}); } fn printPickedCards(card1: u32, card2: u32) void { - std.debug.print("Player 1 plays: {}\n", .{card1}); - std.debug.print("Player 2 plays: {}\n", .{card2}); + aoc.debugfmt("Player 1 plays: {}\n", .{card1}); + aoc.debugfmt("Player 2 plays: {}\n", .{card2}); } fn calcScore(deck: *std.ArrayList(u32)) u64 { @@ -49,7 +47,7 @@ fn calcScore(deck: *std.ArrayList(u32)) u64 { return score; } -fn copyList(allocator: *std.mem.Allocator, list: []u32) !std.ArrayList(u32) { +fn copyList(allocator: std.mem.Allocator, list: []u32) !std.ArrayList(u32) { var newlist = std.ArrayList(u32).init(allocator); errdefer newlist.deinit(); @@ -60,28 +58,25 @@ fn copyList(allocator: *std.mem.Allocator, list: []u32) !std.ArrayList(u32) { return newlist; } -fn part1Round( - allocator: *std.mem.Allocator, - roundnum: u32, - decks: [2]*std.ArrayList(u32), -) anyerror!void { +fn part1Round(allocator: std.mem.Allocator, roundnum: u32, decks: [2]*std.ArrayList(u32)) anyerror!void { + _ = allocator; if (aoc.debug) printRoundInfo(roundnum, decks); const card1: u32 = decks[0].orderedRemove(0); const card2: u32 = decks[1].orderedRemove(0); if (aoc.debug) printPickedCards(card1, card2); if (card1 > card2) { - if (aoc.debug) std.debug.print("Player1 wins\n", .{}); + if (aoc.debug) aoc.debugfmt("Player1 wins\n", .{}); try decks[0].append(card1); try decks[0].append(card2); } else if (card2 > card1) { - if (aoc.debug) std.debug.print("Player2 wins\n", .{}); + if (aoc.debug) aoc.debugfmt("Player2 wins\n", .{}); try decks[1].append(card2); try decks[1].append(card1); } else return aoc.Error.InvalidInput; } fn part2Round( - allocator: *std.mem.Allocator, + allocator: std.mem.Allocator, roundnum: u32, decks: [2]*std.ArrayList(u32), ) anyerror!void { @@ -92,7 +87,7 @@ fn part2Round( var winner = @intToEnum(Player, @boolToInt(card2 > card1)); if (card1 <= decks[0].items.len and card2 <= decks[1].items.len) { - if (aoc.debug) std.debug.print("\nStarting subgame..\n", .{}); + if (aoc.debug) aoc.debugfmt("\nStarting subgame..\n", .{}); var tmp_deck1 = try copyList(allocator, decks[0].items[0..card1]); defer tmp_deck1.deinit(); @@ -103,26 +98,26 @@ fn part2Round( winner = result.winner; if (aoc.debug) { const wp: u32 = if (winner == Player.P1) 1 else 2; - std.debug.print("Player{} wins the subgame\n...anyway, back to previous game\n\n", .{wp}); + aoc.debugfmt("Player{} wins the subgame\n...anyway, back to previous game\n\n", .{wp}); } } if (winner == Player.P1) { - if (aoc.debug) std.debug.print("Player1 wins the round\n", .{}); + if (aoc.debug) aoc.debugfmt("Player1 wins the round\n", .{}); try decks[0].append(card1); try decks[0].append(card2); } else if (winner == Player.P2) { - if (aoc.debug) std.debug.print("Player2 wins the round\n", .{}); + if (aoc.debug) aoc.debugfmt("Player2 wins the round\n", .{}); try decks[1].append(card2); try decks[1].append(card1); } else return aoc.Error.InvalidInput; } fn playGame( - allocator: *std.mem.Allocator, + allocator: std.mem.Allocator, decks: [2]*std.ArrayList(u32), - roundFunc: fn ( - alloc: *std.mem.Allocator, + roundFunc: *const fn ( + alloc: std.mem.Allocator, num: u32, decks: [2]*std.ArrayList(u32), ) anyerror!void, @@ -149,7 +144,9 @@ fn playGame( }; } -fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + var deck1 = std.ArrayList(u32).init(allocator); defer deck1.deinit(); @@ -160,10 +157,13 @@ fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { try parseInput(input, decks); const result = try playGame(allocator, decks, part1Round); - std.debug.print("{}\n", .{result.score}); + + return try std.fmt.allocPrint(allocator, "{}", .{result.score}); } -fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + var deck1 = std.ArrayList(u32).init(allocator); defer deck1.deinit(); @@ -174,7 +174,8 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { try parseInput(input, decks); const result = try playGame(allocator, decks, part2Round); - std.debug.print("{}\n", .{result.score}); + + return try std.fmt.allocPrint(allocator, "{}", .{result.score}); } -pub const main = aoc.gen_main(part1, part2); +pub const main = aoc.main(part1, part2, .{ "32272", "33206" }); diff --git a/src/day22/part1 b/src/22/part1 index 56e6e13..56e6e13 100644 --- a/src/day22/part1 +++ b/src/22/part1 diff --git a/src/day22/part2 b/src/22/part2 index 4f85a8a..4f85a8a 100644 --- a/src/day22/part2 +++ b/src/22/part2 diff --git a/src/day23/input b/src/23/input index db9c9c6..db9c9c6 100644 --- a/src/day23/input +++ b/src/23/input diff --git a/src/day23/input-test b/src/23/input-test index ab40847..ab40847 100644 --- a/src/day23/input-test +++ b/src/23/input-test diff --git a/src/day23/main.zig b/src/23/main.zig index 619c173..0754ab8 100644 --- a/src/day23/main.zig +++ b/src/23/main.zig @@ -43,7 +43,7 @@ fn printLinks(start: *RingLink, end: *RingLink, skipfirst: bool) void { var b = skipfirst; while (b or link != end) : (link = link.next) { b = false; - std.debug.print("{} ", .{link.data}); + aoc.debugfmt("{} ", .{link.data}); } } @@ -67,6 +67,7 @@ fn parseInput(input: []const u8, lookup: []RingLink, list: *?*RingLink) !u32 { } fn doRound(list: *RingLink, len: u32, lookup: []RingLink, current: **RingLink, max: u32, round: usize) !void { + _ = list; var target = (current.*.data + max - 2) % max + 1; var k: usize = 0; var check = current.*.next; @@ -79,28 +80,28 @@ fn doRound(list: *RingLink, len: u32, lookup: []RingLink, current: **RingLink, m var closest = &lookup[target]; if (aoc.debug) { - std.debug.print("\n-- move {} --\ncups:", .{round}); + aoc.debugfmt("\n-- move {} --\ncups:", .{round}); if (aoc.debuglvl == 1) { var start = current.*.advance(len - ((round - 1) % len)); var link = start; while (true) { if (link == current.*) { - std.debug.print(" ({})", .{link.data}); + aoc.debugfmt(" ({})", .{link.data}); } else { - std.debug.print(" {} ", .{link.data}); + aoc.debugfmt(" {} ", .{link.data}); } link = link.next; if (link == start) break; } - std.debug.print("\n", .{}); + aoc.debugfmt("\n", .{}); } else { - std.debug.print("\n{} {} {}\n", .{ current.*.data, target, closest.data }); - std.debug.print(".. ", .{}); + aoc.debugfmt("\n{} {} {}\n", .{ current.*.data, target, closest.data }); + aoc.debugfmt(".. ", .{}); printLinks(current.*, current.*.next.next.next.next, false); - std.debug.print("..\n.. ", .{}); + aoc.debugfmt("..\n.. ", .{}); printLinks(closest, closest.next.next.next.next, false); - std.debug.print("..\n", .{}); + aoc.debugfmt("..\n", .{}); } } @@ -114,7 +115,7 @@ fn doRound(list: *RingLink, len: u32, lookup: []RingLink, current: **RingLink, m current.* = current.*.next; } -fn createLookup(allocator: *std.mem.Allocator, len: usize) ![]RingLink { +fn createLookup(allocator: std.mem.Allocator, len: usize) ![]RingLink { var lookup = try allocator.alloc(RingLink, len + 1); errdefer allocator.free(lookup); @@ -126,7 +127,9 @@ fn createLookup(allocator: *std.mem.Allocator, len: usize) ![]RingLink { return lookup; } -fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + var lookup = try createLookup(allocator, 9); defer allocator.free(lookup); @@ -143,18 +146,24 @@ fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { var start = &lookup[1]; var link = start.next; + + var parts = std.ArrayList(u8).init(allocator); + defer parts.deinit(); while (link != start) : (link = link.next) { - std.debug.print("{}", .{link.data}); + try parts.append('0' + @intCast(u8, link.data)); } - std.debug.print("\n", .{}); + + return try std.fmt.allocPrint(allocator, "{s}", .{parts.items}); } -fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + var lookup = try createLookup(allocator, 1000000); defer allocator.free(lookup); var list: ?*RingLink = null; - const max = try parseInput(input, lookup, &list); + _ = try parseInput(input, lookup, &list); if (list == null) return aoc.Error.InvalidInput; const real_len = list.?.length(); @@ -168,13 +177,18 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { var round: usize = 0; var current = list.?; while (round < 10000000) : (round += 1) { - if (round % 1000000 == 0) std.debug.print(".", .{}); + if (round % 1000000 == 0) aoc.debugfmt(".", .{}); try doRound(list.?, 1000000, lookup, ¤t, 1000000, round + 1); } - std.debug.print("\n", .{}); + aoc.debugfmt("\n", .{}); var link = (&lookup[1]).next; - std.debug.print("{} * {} = {}\n", .{ @intCast(u64, link.data), @intCast(u64, link.next.data), @intCast(u64, link.data) * @intCast(u64, link.next.data) }); + + const a = @intCast(u64, link.data); + const b = @intCast(u64, link.next.data); + aoc.debugfmt("{} * {} = {}\n", .{ a, b, a * b }); + + return try std.fmt.allocPrint(allocator, "{}", .{a * b}); } -pub const main = aoc.gen_main(part1, part2); +pub const main = aoc.main(part1, part2, .{ "97245386", "156180332979" }); diff --git a/src/day23/notes b/src/23/notes index 0fc11ad..0fc11ad 100644 --- a/src/day23/notes +++ b/src/23/notes diff --git a/src/day23/part1 b/src/23/part1 index 933857b..933857b 100644 --- a/src/day23/part1 +++ b/src/23/part1 diff --git a/src/day23/part2 b/src/23/part2 index 71e4559..71e4559 100644 --- a/src/day23/part2 +++ b/src/23/part2 diff --git a/src/day24/input b/src/24/input index 8b15285..8b15285 100644 --- a/src/day24/input +++ b/src/24/input diff --git a/src/day24/input-test b/src/24/input-test index 3dc2f67..3dc2f67 100644 --- a/src/day24/input-test +++ b/src/24/input-test diff --git a/src/day24/main.zig b/src/24/main.zig index 81fef3d..d32abfb 100644 --- a/src/day24/main.zig +++ b/src/24/main.zig @@ -24,7 +24,7 @@ const Tile = struct { }; fn parseInput(tiles: *std.AutoHashMap(aoc.Pos, Tile), input: []const u8) !void { - var lineit = std.mem.tokenize(input, "\n"); + var lineit = std.mem.tokenize(u8, input, "\n"); while (lineit.next()) |line| { var pos = aoc.Pos{ .x = 0, .y = 0 }; @@ -44,7 +44,7 @@ fn parseInput(tiles: *std.AutoHashMap(aoc.Pos, Tile), input: []const u8) !void { var tile = tiles.getEntry(pos); if (tile != null) { - tile.?.value.color = if (tile.?.value.color == Color.WHITE) Color.BLACK else Color.WHITE; + tile.?.value_ptr.color = if (tile.?.value_ptr.color == Color.WHITE) Color.BLACK else Color.WHITE; } else { try tiles.put(pos, Tile{ .color = Color.BLACK }); } @@ -71,16 +71,16 @@ fn applyRule(pos: aoc.Pos, before: *std.AutoHashMap(aoc.Pos, Tile), after: *std. } } -fn doRound(allocator: *std.mem.Allocator, tiles: *std.AutoHashMap(aoc.Pos, Tile)) !void { +fn doRound(allocator: std.mem.Allocator, tiles: *std.AutoHashMap(aoc.Pos, Tile)) !void { var result = std.AutoHashMap(aoc.Pos, Tile).init(allocator); defer result.deinit(); var mapit = tiles.iterator(); while (mapit.next()) |kv| { for (dirs) |d| { - try applyRule(kv.key.add(d), tiles, &result); + try applyRule(kv.key_ptr.add(d), tiles, &result); } - try applyRule(kv.key, tiles, &result); + try applyRule(kv.key_ptr.*, tiles, &result); } std.mem.swap(std.AutoHashMap(aoc.Pos, Tile), &result, tiles); @@ -90,21 +90,25 @@ fn countBlack(tiles: *std.AutoHashMap(aoc.Pos, Tile)) u32 { var count: u32 = 0; var mapit = tiles.iterator(); while (mapit.next()) |kv| { - if (kv.value.color == Color.BLACK) count += 1; + if (kv.value_ptr.color == Color.BLACK) count += 1; } return count; } -fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + var tiles = std.AutoHashMap(aoc.Pos, Tile).init(allocator); defer tiles.deinit(); try parseInput(&tiles, input); - std.debug.print("{}\n", .{countBlack(&tiles)}); + return try std.fmt.allocPrint(allocator, "{}", .{countBlack(&tiles)}); } -fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + var tiles = std.AutoHashMap(aoc.Pos, Tile).init(allocator); defer tiles.deinit(); @@ -115,7 +119,7 @@ fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { try doRound(allocator, &tiles); } - std.debug.print("{}\n", .{countBlack(&tiles)}); + return try std.fmt.allocPrint(allocator, "{}", .{countBlack(&tiles)}); } -pub const main = aoc.gen_main(part1, part2); +pub const main = aoc.main(part1, part2, .{ "317", "3804" }); diff --git a/src/day24/part1 b/src/24/part1 index 8ad7455..8ad7455 100644 --- a/src/day24/part1 +++ b/src/24/part1 diff --git a/src/day24/part2 b/src/24/part2 index 784cf49..784cf49 100644 --- a/src/day24/part2 +++ b/src/24/part2 diff --git a/src/day25/input b/src/25/input index fe514d8..fe514d8 100644 --- a/src/day25/input +++ b/src/25/input diff --git a/src/day25/input-test b/src/25/input-test index 9cbfc23..9cbfc23 100644 --- a/src/day25/input-test +++ b/src/25/input-test diff --git a/src/day25/main.zig b/src/25/main.zig index 96664ce..0008550 100644 --- a/src/day25/main.zig +++ b/src/25/main.zig @@ -23,12 +23,12 @@ fn bfLoops(subject_num: u64, pubkey: u64) ?u64 { } fn parseInput(door_pubkey: *u64, card_pubkey: *u64, input: []const u8) !void { - var lineit = std.mem.tokenize(input, "\n"); - door_pubkey.* = try std.fmt.parseInt(u64, try aoc.assertV(lineit.next()), 10); - card_pubkey.* = try std.fmt.parseInt(u64, try aoc.assertV(lineit.next()), 10); + var lineit = std.mem.tokenize(u8, input, "\n"); + door_pubkey.* = try std.fmt.parseInt(u64, try aoc.unwrap(lineit.next()), 10); + card_pubkey.* = try std.fmt.parseInt(u64, try aoc.unwrap(lineit.next()), 10); } -fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { +fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { var door_pubkey: u64 = undefined; var card_pubkey: u64 = undefined; @@ -37,20 +37,22 @@ fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { if (args.len == 0 or std.mem.eql(u8, args[0], "bf_door")) { if (bfLoops(7, door_pubkey)) |door_loops| { std.debug.print("{}\n", .{transform(card_pubkey, door_loops)}); - return; + return null; } } else if (args.len > 0 and std.mem.eql(u8, args[0], "bf_card")) { if (bfLoops(7, card_pubkey)) |card_loops| { - std.debug.print("{}\n", .{transform(door_pubkey, card_loops)}); - return; + const answer = transform(door_pubkey, card_loops); + return try std.fmt.allocPrint(allocator, "{}", .{answer}); } } + return null; } -fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { - var answer: u32 = 0; +fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = input; + _ = args; - std.debug.print("{}\n", .{answer}); + return try std.fmt.allocPrint(allocator, "", .{}); } -pub const main = aoc.gen_main(part1, part2); +pub const main = aoc.main(part1, part2, .{ "15217943", "" }); diff --git a/src/day25/part1 b/src/25/part1 index 27518f4..27518f4 100644 --- a/src/day25/part1 +++ b/src/25/part1 diff --git a/src/day25/part2 b/src/25/part2 index 2d18d3f..2d18d3f 100644 --- a/src/day25/part2 +++ b/src/25/part2 diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..fa2190c --- /dev/null +++ b/src/Makefile @@ -0,0 +1,21 @@ +DAYS = $(shell seq 1 25 | xargs printf "%02i\n") + +.PHONY: all +all:: + +define make-day +all:: $1/run +.PHONY: $1/run +$1/main: $1/main.zig + zig build-exe \ + --pkg-begin "aoc" "common/aoc.zig" --pkg-end \ + --pkg-begin "console8" "common/console8.zig" --pkg-end \ + --cache-dir .cache/zig -femit-bin=$1/main -freference-trace \ + $1/main.zig +$1/run: $1/main + @echo "== day $1 ==" + @echo -en "\npart 1: " && cd $1 && time ./main 1 + @echo -en "\npart 2: " && cd $1 && time ./main 2 + @echo "" +endef +$(foreach day,$(DAYS),$(eval $(call make-day,$(day)))) diff --git a/lib/aoc.zig b/src/common/aoc.zig index ea373a5..cd35c43 100644 --- a/lib/aoc.zig +++ b/src/common/aoc.zig @@ -1,34 +1,41 @@ const std = @import("std"); -pub const input = @import("input.zig"); pub const Error = error{InvalidInput}; pub var debug = false; pub var debuglvl: u32 = 0; -const part_type = fn (alloc: *std.mem.Allocator, input: []u8, args: [][]u8) anyerror!void; -pub fn gen_main(comptime part1: part_type, comptime part2: part_type) fn () anyerror!void { +pub fn debugfmt(comptime fmt: []const u8, args: anytype) void { + if (debug) + std.debug.print(fmt, args); +} + +const part_type = fn (alloc: std.mem.Allocator, input: []u8, args: [][]u8) anyerror!?[]u8; +pub fn main(comptime part1: part_type, comptime part2: part_type, comptime sols: [2]?[]const u8) fn () anyerror!void { const impl = struct { fn main() !void { - // create a default allocator + const envvar = std.os.getenv("AOC_DEBUG"); + if (envvar != null) { + debuglvl = try std.fmt.parseInt(u32, envvar.?, 10); + debug = debuglvl != 0; + } + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); - var heapalloc = &gpa.allocator; + var heapalloc = gpa.allocator(); - // parse args const args = try std.process.argsAlloc(heapalloc); defer heapalloc.free(args); if (args.len < 2) return; - const part = try std.fmt.parseInt(u8, args[1], 10); - var filename: []const u8 = std.mem.spanZ("input"); + var filename: []const u8 = std.mem.span("input"); for (std.os.environ) |v| { - const kv = std.mem.spanZ(v); + const kv = std.mem.span(v); if (std.mem.indexOfScalar(u8, kv, '=')) |sep| { if (sep == kv.len - 1) continue; - if (std.mem.eql(u8, kv[0..sep], "AOCINPUT")) { + if (std.mem.eql(u8, kv[0..sep], "AOC_INPUT")) { filename = kv[sep + 1 ..]; - std.debug.print("Using input file: {}\n", .{filename}); + std.debug.print("Using input file: {s}\n", .{filename}); break; } else if (std.mem.eql(u8, kv[0..sep], "AOCDEBUG")) { debug = true; @@ -37,17 +44,36 @@ pub fn gen_main(comptime part1: part_type, comptime part2: part_type) fn () anye } } - // read all input into mem (files are always small so no problem) const file = try std.fs.cwd().openFile(filename, .{}); const text = try file.reader().readAllAlloc(heapalloc, std.math.maxInt(u32)); defer heapalloc.free(text); - // exec part - try switch (part) { - 1 => part1(heapalloc, text, args[2..]), - 2 => part2(heapalloc, text, args[2..]), - else => std.debug.print("Invalid part number!\n", .{}), - }; + var part = try std.fmt.parseInt(u8, args[1], 10); + var answer: ?[]u8 = null; + switch (part) { + 1 => answer = try part1(heapalloc, text, args[2..]), + 2 => answer = try part2(heapalloc, text, args[2..]), + else => { + std.debug.print("Invalid part number!\n", .{}); + std.os.exit(1); + }, + } + part = part - 1; + + if (answer != null) { + const stdout = std.io.getStdOut().writer(); + try std.fmt.format(stdout, "{s}\n", .{answer.?}); + if (sols[part] != null and !std.mem.eql(u8, answer.?, sols[part].?)) { + std.debug.print("Invalid answer\n", .{}); + std.os.exit(1); + } + heapalloc.free(answer.?); + } + + if (sols[part] == null) { + std.debug.print("Missing solution\n", .{}); + std.os.exit(1); + } } }; return impl.main; @@ -73,9 +99,7 @@ pub const Dir = struct { pub const South = Pos{ .x = 0, .y = -1 }; pub const North = Pos{ .x = 0, .y = 1 }; - pub const Name = enum { - NORTH = 0, EAST = 1, SOUTH = 2, WEST = 3 - }; + pub const Name = enum(u8) { NORTH = 0, EAST = 1, SOUTH = 2, WEST = 3 }; pub const dirs = [_]Pos{ North, East, South, West }; pub fn get(name: Name) Pos { @@ -108,7 +132,6 @@ pub const Dir = struct { pub fn rotCCW(pos: Pos, offset: usize) Pos { const constrained = offset % 4; - std.debug.print("{}\n", .{constrained}); return Pos{ .x = cos90vs[constrained] * pos.x - sin90vs[constrained] * pos.y, .y = sin90vs[constrained] * pos.x + cos90vs[constrained] * pos.y, @@ -116,7 +139,7 @@ pub const Dir = struct { } }; -pub fn assertV(v: anytype) !@TypeOf(v.?) { +pub fn unwrap(v: anytype) !@TypeOf(v.?) { if (v == null) return Error.InvalidInput; return v.?; } diff --git a/lib/console8.zig b/src/common/console8.zig index cfd8266..c778955 100644 --- a/lib/console8.zig +++ b/src/common/console8.zig @@ -1,10 +1,9 @@ const std = @import("std"); +const aoc = @import("aoc.zig"); pub const OpError = error{ InstructionPointerOOB, InvalidFormat, InstructionUnknown }; pub const OpFuncSig = fn (ctx: *Console, arg: i64) OpError!void; -pub const Instruction = struct { - opcode: []const u8, opfunc: OpFuncSig, argval: i64 -}; +pub const Instruction = struct { opcode: []const u8, opfunc: *const OpFuncSig, argval: i64 }; pub const Console = struct { accumulator: i64 = 0, @@ -14,14 +13,14 @@ pub const Console = struct { code: []const u8, instlist: [][]const u8, - allocator: *std.mem.Allocator, + allocator: std.mem.Allocator, const Self = @This(); - pub fn init(code: []const u8, allocator: *std.mem.Allocator) !Self { + pub fn init(code: []const u8, allocator: std.mem.Allocator) !Self { var instvec = std.ArrayList([]const u8).init(allocator); errdefer instvec.deinit(); - var instit = std.mem.tokenize(code, "\n"); + var instit = std.mem.tokenize(u8, code, "\n"); while (instit.next()) |inst| { try instvec.append(inst); } @@ -41,7 +40,7 @@ pub const Console = struct { self.instructptr = 0; } - const instructionMap = std.ComptimeStringMap(OpFuncSig, .{ + const instructionMap = std.ComptimeStringMap(*const OpFuncSig, .{ .{ "jmp", jumpInstruction }, .{ "acc", accInstruction }, .{ "nop", nopInstruction }, @@ -60,6 +59,7 @@ pub const Console = struct { } pub fn nopInstruction(self: *Self, arg: i64) OpError!void { + _ = arg; self.instructptr += 1; } @@ -74,13 +74,13 @@ pub const Console = struct { const opcode = inststr[0..sep.?]; if (instructionMap.get(opcode)) |opfunc| { const arg = inststr[sep.? + 1 ..]; - const val = std.fmt.parseInt(i64, arg, 10) catch |err| { - std.debug.print("Failed to parse arg value: {}\n", .{arg}); + const val = std.fmt.parseInt(i64, arg, 10) catch { + aoc.debugfmt("Failed to parse arg value: {s}\n", .{arg}); return OpError.InvalidFormat; }; return Instruction{ .opcode = opcode, .opfunc = opfunc, .argval = val }; } else { - std.debug.print("Unknown instruction: {}\n", .{inststr}); + aoc.debugfmt("Unknown instruction: {s}\n", .{inststr}); return OpError.InstructionUnknown; } } |
