From a06f330d2cc98f581a09e0c39327733d1945fea4 Mon Sep 17 00:00:00 2001 From: Kienan Stewart Date: Wed, 9 Mar 2022 15:36:18 -0500 Subject: [PATCH] Uncomitted attempts --- day12/build.zig | 27 ++++ day12/examples/1.dot | 16 ++ day12/examples/2.dot | 20 +++ day12/examples/3.dot | 28 ++++ day12/graph.dot | 39 +++++ day12/input | 24 +++ day12/src/main.zig | 362 +++++++++++++++++++++++++++++++++++++++++++ day14/build.zig | 27 ++++ day14/input | 102 ++++++++++++ day14/src/main.zig | 50 ++++++ day15/build.zig | 27 ++++ day15/input | 101 ++++++++++++ day15/src/main.zig | 135 ++++++++++++++++ day16/build.zig | 34 ++++ day16/src/main.zig | 9 ++ day8/build.zig | 27 ++++ day8/input | 201 ++++++++++++++++++++++++ day8/src/main.zig | 128 +++++++++++++++ 18 files changed, 1357 insertions(+) create mode 100644 day12/build.zig create mode 100644 day12/examples/1.dot create mode 100644 day12/examples/2.dot create mode 100644 day12/examples/3.dot create mode 100644 day12/graph.dot create mode 100644 day12/input create mode 100644 day12/src/main.zig create mode 100644 day14/build.zig create mode 100644 day14/input create mode 100644 day14/src/main.zig create mode 100644 day15/build.zig create mode 100644 day15/input create mode 100644 day15/src/main.zig create mode 100644 day16/build.zig create mode 100644 day16/src/main.zig create mode 100644 day8/build.zig create mode 100644 day8/input create mode 100644 day8/src/main.zig diff --git a/day12/build.zig b/day12/build.zig new file mode 100644 index 0000000..8b34cc3 --- /dev/null +++ b/day12/build.zig @@ -0,0 +1,27 @@ +const std = @import("std"); + +pub fn build(b: *std.build.Builder) void { + // Standard target options allows the person running `zig build` to choose + // what target to build for. Here we do not override the defaults, which + // means any target is allowed, and the default is native. Other options + // for restricting supported target set are available. + const target = b.standardTargetOptions(.{}); + + // Standard release options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. + const mode = b.standardReleaseOptions(); + + const exe = b.addExecutable("day12", "src/main.zig"); + exe.setTarget(target); + exe.setBuildMode(mode); + exe.install(); + + const run_cmd = exe.run(); + run_cmd.step.dependOn(b.getInstallStep()); + if (b.args) |args| { + run_cmd.addArgs(args); + } + + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); +} diff --git a/day12/examples/1.dot b/day12/examples/1.dot new file mode 100644 index 0000000..e2fe554 --- /dev/null +++ b/day12/examples/1.dot @@ -0,0 +1,16 @@ +digraph D { +start [shape=box] +end [shape=box] +A [shape=box] +b [shape=box] +c [shape=box] +d [shape=box] +start -> {A, b} +end -> {A, b} +A -> {c, b, end} +A -> {start} +b -> {d, end} +b -> {start, A} +c -> {A} +d -> {b} +} diff --git a/day12/examples/2.dot b/day12/examples/2.dot new file mode 100644 index 0000000..dd3fb0d --- /dev/null +++ b/day12/examples/2.dot @@ -0,0 +1,20 @@ +digraph D { +start [shape=box] +end [shape=box] +dc [shape=box] +HN [shape=box] +kj [shape=box] +LN [shape=box] +sa [shape=box] +start -> {kj} +start -> {HN, dc} +end -> {dc, HN} +dc -> {end, start, HN} +dc -> {LN, kj} +HN -> {start, end} +HN -> {dc, kj} +kj -> {sa, HN, dc} +kj -> {start} +LN -> {dc} +sa -> {kj} +} diff --git a/day12/examples/3.dot b/day12/examples/3.dot new file mode 100644 index 0000000..7f26a48 --- /dev/null +++ b/day12/examples/3.dot @@ -0,0 +1,28 @@ +digraph D { +start [shape=box] +end [shape=box] +fs [shape=box] +he [shape=box] +DX [shape=box] +pj [shape=box] +zg [shape=box] +sl [shape=box] +RW [shape=box] +WI [shape=box] +start -> {DX, pj, RW} +end -> {zg} +end -> {fs} +fs -> {end, he, DX} +fs -> {pj} +he -> {DX, WI} +he -> {fs, pj, RW, zg} +DX -> {he, start, pj, fs} +pj -> {DX, he, RW, fs} +pj -> {zg, start} +zg -> {sl, pj, RW, he} +zg -> {end} +sl -> {zg} +RW -> {he} +RW -> {pj, zg, start} +WI -> {he} +} diff --git a/day12/graph.dot b/day12/graph.dot new file mode 100644 index 0000000..0b4f0c2 --- /dev/null +++ b/day12/graph.dot @@ -0,0 +1,39 @@ +digraph D { +start [shape=box] +end [shape=box] +mj [shape=box] +TZ [shape=box] +LY [shape=box] +TX [shape=box] +ez [shape=box] +uw [shape=box] +TH [shape=box] +vn [shape=box] +sb [shape=box] +RR [shape=box] +mt [shape=box] +start -> {LY, TZ} +start -> {mj} +end -> {LY} +end -> {TH, sb} +mj -> {TZ, start, TH} +mj -> {LY} +TZ -> {sb} +TZ -> {mj, ez, start, uw} +LY -> {mj, ez} +LY -> {start, uw, end} +TX -> {ez, mt} +TX -> {sb} +ez -> {TZ, TH} +ez -> {TX, uw, LY} +uw -> {ez, LY, RR, vn, TZ} +uw -> {sb} +TH -> {vn, end} +TH -> {mj, ez} +vn -> {sb} +vn -> {TH, uw} +sb -> {uw, TX, end} +sb -> {TZ, vn} +RR -> {uw} +mt -> {TX} +} diff --git a/day12/input b/day12/input new file mode 100644 index 0000000..a1434b7 --- /dev/null +++ b/day12/input @@ -0,0 +1,24 @@ +mj-TZ +start-LY +TX-ez +uw-ez +ez-TZ +TH-vn +sb-uw +uw-LY +LY-mj +sb-TX +TH-end +end-LY +mj-start +TZ-sb +uw-RR +start-TZ +mj-TH +ez-TH +sb-end +LY-ez +TX-mt +vn-sb +uw-vn +uw-TZ diff --git a/day12/src/main.zig b/day12/src/main.zig new file mode 100644 index 0000000..d6c85fd --- /dev/null +++ b/day12/src/main.zig @@ -0,0 +1,362 @@ +const std = @import("std"); + +const example = + \\start-A + \\start-b + \\A-c + \\A-b + \\b-d + \\A-end + \\b-end +; + +const example2 = + \\dc-end + \\HN-start + \\start-kj + \\dc-start + \\dc-HN + \\LN-dc + \\HN-end + \\kj-sa + \\kj-HN + \\kj-dc +; + +const example3 = + \\fs-end + \\he-DX + \\fs-he + \\start-DX + \\pj-DX + \\end-zg + \\zg-sl + \\zg-pj + \\pj-he + \\RW-he + \\fs-DX + \\pj-RW + \\zg-RW + \\start-pj + \\he-WI + \\zg-he + \\pj-fs + \\start-RW +; + +const input = @embedFile("../input"); + +const Graph = struct { + alloc: std.mem.Allocator, + nodes: std.ArrayListUnmanaged(*Node) = .{}, + const Self = @This(); + + pub fn deinit(self: *Self) void { + for (self.nodes.items) |n| { + n.parents.deinit(self.alloc); + n.children.deinit(self.alloc); + self.alloc.destroy(n); + } + self.nodes.deinit(self.alloc); + } + + pub fn build_graph(alloc: std.mem.Allocator, text: []const u8) !Self { + var self: Self = .{ + .alloc = alloc, + }; + var start = try alloc.create(Node); + errdefer alloc.destroy(start); + start.id = "start"; + start.children = .{}; + start.parents = .{}; + var end = try alloc.create(Node); + errdefer alloc.destroy(end); + end.id = "end"; + end.children = .{}; + end.parents = .{}; + + // Our input text does not necessarrily contain the Nodes in an order + // that will just assemble into a graph. We temporarily hold an array + // of pointers to all the nodes we're making to facilitate building + // the graph. + try self.nodes.append(self.alloc, start); + try self.nodes.append(self.alloc, end); + + var lit = std.mem.tokenize(u8, text, "\n"); + while (lit.next()) |line| { + var it = std.mem.tokenize(u8, line, "-"); + var first: ?*Node = null; + var second: ?*Node = null; + while (it.next()) |id| { + var n = self.node_exists(id); + if (n == null) { + n = try alloc.create(Node); + errdefer alloc.destroy(n.?); + n.?.id = id; + var big = true; + for (id) |c| { + if (!std.ascii.isUpper(c)) { + big = false; + break; + } + } + n.?.big = big; + n.?.children = .{}; + n.?.parents = .{}; + try self.nodes.append(self.alloc, n.?); + } + if (first == null) { + first = n; + } + else if (second == null) { + second = n; + } + } + // @LEAK When there is an error, the unmanaged array lists aren't properly freed + //std.log.debug("Adding '{s}' as parent of '{s}'", .{first.?.id, second.?.id}); + try second.?.parents.append(self.alloc, first.?); + try first.?.children.append(self.alloc, second.?); + } + + //std.debug.assert(start.parent == null); + //std.debug.assert(end.parent != null); + return self; + } + + pub fn paths_part1(self: *Self) !Paths { + var paths = try self.node_exists("start").?.walk(alloc, null); + return paths; + } + + pub fn node_exists(self: *Self, id: []const u8) ?*Node { + for (self.nodes.items) |n| { + if (std.mem.eql(u8, id, n.id)) { + return n; + } + } + return null; + } + + pub fn to_dot(self: *Self, f: std.fs.File) !void { + defer f.close(); + _ = try f.writeAll("digraph D {\n"); + for (self.nodes.items) |n| { + _ = try f.write(n.id); + _ = try f.write(" [shape=box]\n"); + } + for(self.nodes.items) |n| { + if (n.children.items.len > 0) { + _ = try f.write(n.id); + _ = try f.write(" -> {"); + for (n.children.items) |c, k| { + if (std.mem.eql(u8, c.id, n.id)) { + continue; + } + _ = try f.write(c.id); + if (k != n.children.items.len - 1) { + _ = try f.write(", "); + } + } + _ = try f.write("}\n"); + } + if (n.parents.items.len > 0) { + _ = try f.write(n.id); + _ = try f.write(" -> {"); + for (n.parents.items) |c, k| { + if (std.mem.eql(u8, c.id, n.id)) { + continue; + } + _ = try f.write(c.id); + if (k != n.parents.items.len - 1) { + _ = try f.write(", "); + } + } + _ = try f.write("}\n"); + } + } + try f.writeAll("}\n"); + } +}; + +const Path = std.ArrayListUnmanaged(*Node); +const Paths = struct { + alloc: std.mem.Allocator, + paths: std.ArrayListUnmanaged(*Path), + const Self = @This(), + + pub fn init(alloc: std.mem.Allocator) !Self { + return Self { + .alloc = alloc, + .paths = .{} + }; + } +} +const Node = struct { + id: [] const u8, + big: bool = false, + parents: std.ArrayListUnmanaged(*Node) = .{}, + children: std.ArrayListUnmanaged(*Node) = .{}, + const Self = @This(); + + fn valid_child_part1(self: *Self, path: std.ArrayList(*Node)) bool { + if (self.big) { + return true; + } + for (path.items) |p| { + if (std.mem.eql(u8, p.id, self.id)) { + return false; + } + } + return true; + } + + pub fn walk(self: *Self, alloc: std.mem.Allocator, visited: ?Path) !Paths { + var paths = Path.init(alloc); + // Depth first + if (visited == null) { + var new_path = try alloc.create(Path); + errdefer alloc.destroy(new_path); + try new_path.append(self); + } + else { + try visited.append(alloc, self); + } + return paths; + } + + // pub fn paths(self: *Self, alloc: std.mem.Allocator) !std.ArrayList(*Path) { + // var ps = std.ArrayList(*Path).init(alloc); + // if (ps.items.len == 0) { + // var path = try alloc.create(Path); + // errdefer alloc.destroy(path); + // path.* = Path.init(alloc); + // try ps.append(path); + // } + // for (ps.items) |path| { + // try path.append(self); + // } + // if (std.mem.eql(u8, self.id, "end")) { + // return ps; + // } + // return ps; + // } + + // pub fn paths_from(self: *Self, alloc: std.mem.Allocator, from: Path) !std.ArrayList(Path) { + // var ps = std.ArrayList(*Path).init(alloc); + // for (self.children.items) |c| { + // if (!c.valid_child_part1(from)) { + // continue; + // } + // var path = alloc.create(std.ArrayList(Path)); + // path.* = std.ArrayList(Path).init(alloc); + // for (from.items) |f| { + // try path.append(f); + // } + // try path.append(c); + // try ps.append(path); + // } + // return ps; + // } + +}; + +test "example 1" { + var graph = try Graph.build_graph(std.testing.allocator, example); + defer graph.deinit(); + var end = graph.node_exists("end"); + var end_paths = try end.?.paths(std.testing.allocator); + for (end_paths.items) |p, k| { + std.log.warn("Path number {}", .{k}); + for (p.items) |n| { + std.log.warn("\t{s}", .{n.id}); + } + } + + // Cleanup + for (end_paths.items) |p| { + p.deinit(); + std.testing.allocator.destroy(p); + } + end_paths.deinit(); + + var paths = try graph.paths_part1(); + for (paths.items) |p, k| { + std.log.warn("Path number {}", .{k}); + for (p.items) |n| { + std.log.warn("\t{s}", .{n.id}); + } + } + for (paths.items) |p| { + p.deinit(); + std.testing.allocator.destroy(p); + } + paths.deinit(); +} + +test "example 1 graph" { + var graph = try Graph.build_graph(std.testing.allocator, example); + defer graph.deinit(); + + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + + var file = try tmp.dir.createFile("example1.dot", .{}); + // this closes the file descriptor. + try graph.to_dot(file); + var expected = @embedFile("../examples/1.dot"); + var f = try tmp.dir.openFile("example1.dot", .{}); + defer f.close(); + const actual = try f.readToEndAlloc(std.testing.allocator, std.math.maxInt(u16)); + defer std.testing.allocator.free(actual); + try std.testing.expect(std.mem.eql(u8, expected, actual)); +} + +test "example 2 graph" { + var graph = try Graph.build_graph(std.testing.allocator, example2); + defer graph.deinit(); + + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + + var file = try tmp.dir.createFile("example2.dot", .{}); + // this closes the file descriptor. + try graph.to_dot(file); + var expected = @embedFile("../examples/2.dot"); + var f = try tmp.dir.openFile("example2.dot", .{}); + defer f.close(); + const actual = try f.readToEndAlloc(std.testing.allocator, std.math.maxInt(u16)); + defer std.testing.allocator.free(actual); + try std.testing.expect(std.mem.eql(u8, expected, actual)); +} + +test "example 3 graph" { + var graph = try Graph.build_graph(std.testing.allocator, example3); + defer graph.deinit(); + + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + + var file = try tmp.dir.createFile("example3.dot", .{}); + // this closes the file descriptor. + try graph.to_dot(file); + var expected = @embedFile("../examples/3.dot"); + var f = try tmp.dir.openFile("example3.dot", .{}); + defer f.close(); + const actual = try f.readToEndAlloc(std.testing.allocator, std.math.maxInt(u16)); + defer std.testing.allocator.free(actual); + try std.testing.expect(std.mem.eql(u8, expected, actual)); +} + +pub fn main() anyerror!void { + var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); + defer arena.deinit(); + const alloc = arena.allocator(); + + // Read our input + var graph = try Graph.build_graph(alloc, input); + defer graph.deinit(); + + var f = try std.fs.cwd().createFile("graph.dot", .{}); + try graph.to_dot(f); + +} diff --git a/day14/build.zig b/day14/build.zig new file mode 100644 index 0000000..f13ff9a --- /dev/null +++ b/day14/build.zig @@ -0,0 +1,27 @@ +const std = @import("std"); + +pub fn build(b: *std.build.Builder) void { + // Standard target options allows the person running `zig build` to choose + // what target to build for. Here we do not override the defaults, which + // means any target is allowed, and the default is native. Other options + // for restricting supported target set are available. + const target = b.standardTargetOptions(.{}); + + // Standard release options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. + const mode = b.standardReleaseOptions(); + + const exe = b.addExecutable("day14", "src/main.zig"); + exe.setTarget(target); + exe.setBuildMode(mode); + exe.install(); + + const run_cmd = exe.run(); + run_cmd.step.dependOn(b.getInstallStep()); + if (b.args) |args| { + run_cmd.addArgs(args); + } + + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); +} diff --git a/day14/input b/day14/input new file mode 100644 index 0000000..9c69fce --- /dev/null +++ b/day14/input @@ -0,0 +1,102 @@ +ONHOOSCKBSVHBNKFKSBK + +HO -> B +KB -> O +PV -> B +BV -> C +HK -> N +FK -> H +NV -> C +PF -> K +FV -> B +NH -> P +CO -> N +HV -> P +OH -> H +BC -> H +SP -> C +OK -> F +KH -> N +HB -> V +FP -> N +KP -> O +FB -> O +FH -> F +CN -> K +BP -> P +SF -> O +CK -> K +KN -> O +VK -> C +HP -> N +KK -> V +KO -> C +OO -> P +BH -> B +OC -> O +HC -> V +HS -> O +SH -> V +SO -> C +FS -> N +CH -> O +PC -> O +FC -> S +VO -> H +NS -> H +PH -> C +SS -> F +BN -> B +BF -> F +NC -> F +CS -> F +NN -> O +FF -> P +OF -> H +NF -> O +SC -> F +KC -> F +CP -> H +CF -> K +BS -> S +HN -> K +CB -> P +PB -> V +VP -> C +OS -> C +FN -> B +NB -> V +BB -> C +BK -> V +VF -> V +VC -> O +NO -> K +KF -> P +FO -> C +OB -> K +ON -> S +BO -> V +KV -> H +CC -> O +HF -> N +VS -> S +PN -> P +SK -> F +PO -> V +HH -> F +VV -> N +VH -> N +SV -> S +CV -> B +KS -> K +PS -> V +OV -> S +SB -> V +NP -> K +SN -> C +NK -> O +PK -> F +VN -> P +PP -> K +VB -> C +OP -> P diff --git a/day14/src/main.zig b/day14/src/main.zig new file mode 100644 index 0000000..4183f4a --- /dev/null +++ b/day14/src/main.zig @@ -0,0 +1,50 @@ +const std = @import("std"); + +pub fn main() anyerror!void { + var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); + defer arena.deinit(); + const alloc = &arena.allocator; + + // Read our input + var f = try std.fs.cwd().openFile("input", .{}); + defer f.close(); + var contents = try f.readToEndAlloc(alloc, std.math.maxInt(u32)); + defer alloc.free(contents); + + var it = std.mem.tokenize(contents, "\n"); + var template: []const u8 = it.next().?; + var replacements = std.ArrayList(Replacement).init(alloc); + defer replacements.deinit(); + while (it.next()) |line| { + var lit = std.mem.tokenize(line, " "); + var pair = lit.next().?; + _ = lit.next(); // skip + var insert = lit.next().?; + var r: Replacement = undefined; + r.pair[0] = pair[0]; + r.pair[1] = pair[1]; + r.insert = insert[0]; + try replacements.append(r); + } + + var compound = std.ArrayList(u8).init(alloc); + defer compound.deinit(); + + // Clone our template + for (template) |t| { + try compound.append(t); + } + + var step: u8 = 0; + while (step < 10) : (step += 1) { + var index: usize = 0; + while (index < (compound.items.len - 1)) : (index += 1) { + + } + } +} + +const Replacement = struct { + pair: [2]u8, + insert: u8, +}; diff --git a/day15/build.zig b/day15/build.zig new file mode 100644 index 0000000..ceae36a --- /dev/null +++ b/day15/build.zig @@ -0,0 +1,27 @@ +const std = @import("std"); + +pub fn build(b: *std.build.Builder) void { + // Standard target options allows the person running `zig build` to choose + // what target to build for. Here we do not override the defaults, which + // means any target is allowed, and the default is native. Other options + // for restricting supported target set are available. + const target = b.standardTargetOptions(.{}); + + // Standard release options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. + const mode = b.standardReleaseOptions(); + + const exe = b.addExecutable("day15", "src/main.zig"); + exe.setTarget(target); + exe.setBuildMode(mode); + exe.install(); + + const run_cmd = exe.run(); + run_cmd.step.dependOn(b.getInstallStep()); + if (b.args) |args| { + run_cmd.addArgs(args); + } + + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); +} diff --git a/day15/input b/day15/input new file mode 100644 index 0000000..d4be84a --- /dev/null +++ b/day15/input @@ -0,0 +1,101 @@ +7257173117871274148119191397411866741961893915191233591781237171134183111991588567743913598398999129 +9199956198317542321811795167539333593959845233586245178771259721629672984999396181817424129792113429 +5678391114239515741618386378916979112716269165195269215584939279279177393199175187199511833999819385 +7149919968927397171639311441114199789621399783958844626227879671633692776519178119978312379854918472 +9571133571872511883799281362819193417112413972551887416264299171789218539132957196979811132721291672 +8172584925581929988661688649146133971138896125391412411926754121134983167188929529959966385437588844 +4914518468152915388917729744941583692326581473756415317665271248313684872717734981831164961169161441 +2519894881987711813817528992187872878321614865385871812543911299816979364869972221856158852969592112 +9859391668949943293812278997458981968712239911133451788394169935992685191958894671638974814216612719 +2192529153611518191595896814996141119517992288611125714717119399491112913829241231917539211119881849 +2337843263693226789699931111398112969887535286492296226999311848178157169949315516494848924316823474 +1191281328169873275751982595447194592918411459316261266123821567822849396139339812112776322962112239 +4587143466247931931363896317279914687959418292417719618921579781314932674121288194141812419924513491 +9188217988495399118711317519126435116785399685763129885168131465437119937687542179912685827611129973 +9281999423754831489181352222362255825969879819312357829319722961312991751988497399311813231979551234 +8411642818749914959745792191319718911388132253292482346295961171634916811938584616484592994128913896 +3388255416913192166863912671592797172349778144861261162626679169122118178816999781511482128586841931 +1544111521579847285289829899211419185418871795132727999177828715458885628798278694521919225186212479 +6112394533149912224244478554154888237766677192511968451313528663845219413639188496229811993163929889 +7198784899159217522265428239842119185341314211991273329136173711277137989451533985143943379599711279 +5322174898961411153515736312827315967799457744138498991267929611147897735269199298727139863199825449 +9389598982325839969741219429682597992913484299849122148988986313319119891881251311761168419988993984 +9731391996727591141584769372292618329676991274389449899178113115589128811112539136473142798111999659 +5158499781719185191335698726337225419942769281296219215168381368296377583192114793345154114939919138 +9489436472297254387595375823195948188155998979224885397929111219894329444244314131186498373329899628 +5115948294552681116994961691838819122926817811428719719487293692225795236769313128246954783896581419 +7842378538599781529689123131749873922371451111511113114844145628981899497289212258729489151386198379 +5657182954384884192389177113923946799271142897712314759172399912237297817994565459889643847449996591 +2114463882686822117918787199822775881529895139151499196692246274147217497821149527235387719991114714 +2911917115481432912158219697929535145897818898214661343833193385121919978318122826712681212138729941 +7919482212881442617559399592111184197611192148899237895295211389594619418194519266268597128875922164 +5122782781762591222295946745991662147485323299849117125858933893496129327282821421189644352917799563 +1136291298829627566691527929262125187126922289732289227716821815619551911944779995415278137991622552 +4929328679851289388213321419985631998929472889692146329429231293698273199118713262892523784612611999 +1327734151191996421344997185478394257228713274441955249899986342244731153572974191897991393662691931 +2781117154312271979199389368319489151943763236781711191122134726691762925242929616867585411791129159 +1439186637181196485238285593819594866112889871679966657834249176324314497819969991879491995926214924 +8198715159568382591431231227339698151116662552918333813978498567586137499214111391961922138831958891 +1791636999843894411347725615937522999231159118674816321332412988177251964743241117549699136494419193 +2347191949569898652196299127952395252161168176339428456658821198913454439135327112298841851793131139 +6517373396832351522818699933865532731822114597879322225665521379149517111835298818315911951121182358 +9989219811198438961112789214341437915858986541454335893139567183897549391541933121816392249992757195 +2751242393169519965822749148113198418478749175225597965669585119898274413133594511599239813819261729 +2333615561781383963589211869624423159983279225123169449222598319329695159532597919931391498257683522 +2326925593466238521963381246934599431321918392819124294951311331116521125288316176431186199751635491 +9341367399225381779967242896675118399271292593248972922271792198875912499185119129993955719191131219 +9419488192386241298831213479619891591112812195761439362996925799233613913141343831817117189287994755 +9695968821621727482419983392371417945181396516317947384995853931879231912925789698357961179754119511 +9819267839831973911319938469292179778511998125121931891146223692296192299952153342285175781729419271 +1863816696271622173271985549176326189359343784972161815896794319879938399995919111197381311974788614 +9951198498951459991236295523134837224482241663114944798913138999819143627393989956711324837996945589 +5636493872291432645969776969437321939689259195165871611951271619514731193819693277839717689838523535 +1599873877797591958659883217591162792939133915996748148738729819416279661719716399869947198411871934 +8794639995351381527537286278892222682139243726394685977188691436889598769141824739519232576663818623 +9312694112319895396113225123691699114916381799538178372616115397895462387932975997638239393799315212 +5832652557695274383198568431192817489141326899824429893139485722158832888768989154554522478498498714 +8782845924111831912127921565587919113818125774499381223562975114549551996121626761137916739478221568 +9811929922686225152399211122397597158513891191615161416432783564767191169849849511985178913416153852 +1137711214593792114391112795839813891552389836983814596893489181536998977653386261925692938856897897 +2891454257881276297539979515161138819723112914812122617911912212922972482812982987377897974782821188 +7671589249839457412545235186153935156941884381134221489611219191921272892597772389234492565914198258 +1552483399116635372974898931188883743171689242311789612968114416972112951486134993116143393399793998 +8711188851122588629852724495172527356897321495229616597821333465358285422331924491418522517979118993 +9593315278649975951219952268211871741223727356181132397183838894499829221111978873973591418997289513 +3969997988729933184995613429919894232321166598283911973373115779211228919166877393889434215381781389 +5588221472469964598623295656797377199853717996631331495111278692241929496187813341637872397899624175 +9921637195158912596622263381891921897978588191413981249169189173199699294541171879989121615634351342 +3798931227871974751799777995169123924251123997219388112822621636398239724484812154741112812741472923 +1318745799964813125829815417129787311384489283415939129112478189111179617966392125599951899588399933 +9919168243159595589113819319611976889991646271868112472392818222893941139743434115977476121333191818 +3115972825613468816687921319481739413919883837749478584158912513417673612383549994987313463899975111 +9819245879871957475393932424291294189961391917998628819131487621819218799996929997391516748191973218 +2543119159599118542114782197275925148321199297951316238291197471953953113117231732381348855871885341 +8597192929968786896989279839331134411223192189898926119875521124153282436126568329811999323445191679 +3975785449971831818312568579176154978689122133184732928183381881141862482849263341279915278911683894 +3179111996949988298153199151389479257689125228811639199771592599998978715664289996372311512944263139 +3717731949295466943999325285695444691229948881923328813265999285999127111938229969918689613331778988 +3899774572142914257631398127999644341916848491762691872194259535697139312281868155191287342929193183 +3419531741121147372119481389754229993641415577459396295899232815823975392974927395711568972974293925 +1722176361244419285779288543598751211392531919462574932268633933695192352441119172662173735119515712 +8259287379497359112197399192874789929899857534134534892997117372192299398799696792833998919968157692 +9113588331521199442922126152433321148852971919741151158559612459789766141191421226791434779238851542 +9934485995397918323116671821935121371411639119196141711221111598878781853442188129954293234742142941 +5863996738774189559631311914763988372133998564893667341114799688887419869498499571261945352924543659 +1219173111621632848325722799121771234351313226119137299611516914998734631224397849155891733316457236 +2581119126696683198948223933432829124786552193519396275241779267529656466959196852322344769712989425 +9967141179556989992264146994141761832265411668193157248414232474791714884711961318296968187812563731 +2489879759889926897312354996751186797873869898953112749555984922414591917338857833991412625172181498 +9389457158881973888911321813181681241492541783691881299169396894618761158125679495966134894392982199 +9619992269841241296891999961333629572281414928995452682633291959449286127129612124227252269791663654 +1614835267511194414153578263179929291589313129387133978158911722592272783869578732691311815861241491 +9652116592834327221168619761719533625339966299211384947128885411279177514867942937963481178919941711 +2933298721252991578245341817819977199267549571111491876711439883927615991991196211296294697272313774 +1431715794741427753127381461152185596778586461419451428969132784392522194651111917169662896769767812 +5919195742129923589897349316296264144952749521471522921641452152978765849165829872135669664214112327 +6339474432243943991959119983361889756918344251994119969825144749941178381321286493218961118191911875 +5517228343299391617317192214769719192189853799115811159493116197825637676714396432979816281155693495 +6388461935842284623142112416943162883259817194297799981283439458172297191119123521515932483495126961 +7843191912999868941538811232841971381689242136381989791436989848478992513449168132392761291119899184 +1424114349573937523917871789194999469999119751164761884221519813273935995996288159921682217898434989 + diff --git a/day15/src/main.zig b/day15/src/main.zig new file mode 100644 index 0000000..267319b --- /dev/null +++ b/day15/src/main.zig @@ -0,0 +1,135 @@ +const std = @import("std"); + +pub fn main() anyerror!void { + var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); + defer arena.deinit(); + const alloc = arena.allocator(); + + // Read our input + var f = try std.fs.cwd().openFile("input", .{}); + defer f.close(); + var contents = try f.readToEndAlloc(alloc, std.math.maxInt(u32)); + defer alloc.free(contents); + + var it = std.mem.tokenize(u8, contents, "\n"); + var map = std.ArrayList(u8).init(alloc); + defer map.deinit(); + var width: u8 = 0; + var height: u8 = 0; + while (it.next()) |line| { + height += 1; + if (@intCast(u8, line.len) > width) { + width = @intCast(u8, line.len); + } + for (line) |_, i| { + try map.append(try std.fmt.parseInt(u8, line[i..i+1], 10)); + } + } + + var path = try find_path(alloc, map.items, width, height); + defer path.deinit(); +} + +const Point = struct { + x: u8 = 0, + y: u8 = 0, + + pub fn get_index(p: *Point, width: u8, height: u8) usize { + _ = width; + return @as(usize, height) * @as(usize, p.y) + @as(usize, p.x); + } + + pub fn from_index(index: usize, width: u8, height: u8) Point { + return Point { + .x = @intCast(u8, index % width), + .y = @intCast(u8, index / height), + }; + } +}; + +fn find_path(alloc: std.mem.Allocator, map: []u8, width: u8, + height: u8) !std.ArrayList(Point) { + var path = std.ArrayList(Point).init(alloc); + var open_set = std.AutoHashMap(Point, void).init(alloc); + defer open_set.deinit(); + // Our starting point + try open_set.put(.{.x = width - 1, .y = height - 1}, undefined); + + var came_from = std.AutoHashMap(Point, void).init(alloc); + defer came_from.deinit(); + + while (open_set.count() > 0) { + // Find lowest score in openset + var lowest: Point = undefined; + var score: u8 = std.math.maxInt(u8); + var oit = open_set.iterator(); + while(oit.next()) |kv| { + var p = kv.key_ptr.*; + _ = kv.value_ptr.*; + // We might have to modify this, since our open_set may + // contain points that aren't directly adjacent? + if (map[p.get_index(width, height)] < score) { + lowest = p; + score = map[p.get_index(width, height)]; + } + } + std.log.debug("Went to point {}, {}: score {}", + .{lowest.x, lowest.y, score}); + try path.append(lowest); + try came_from.put(lowest, undefined); + + if (lowest.x == 0 and lowest.y == 0) { + // We have arrived + return path; + } + + _ = open_set.remove(lowest); + + // Let's clear our open_set, but this might be an error + open_set.clearRetainingCapacity(); + var adjacents = get_adjacent_points(lowest.get_index(width, height), + width, height); + for (adjacents) |adj| { + if (adj) |a| { + var p = Point.from_index(a, width, height); + if (came_from.contains(p)) { + continue; + } + try open_set.put(p, undefined); + } + } + } + // Failure to find path + unreachable; +} + +fn get_adjacent_points(i: usize, width: usize, height: usize) [4]?usize { + var adjacents = [4] ?usize { + null, + null, + null, + null, + }; + var ai: usize = 0; + var bottom = i < ((height - 1) * width); + var top = i >= width; + var left = (i % width) != 0; + var right = (i % width) != (width - 1); + if (top) { + adjacents[ai] = i - width; + ai += 1; + } + if (left) { + adjacents[ai] = i - 1; + ai += 1; + } + if (right) { + adjacents[ai] = i + 1; + ai += 1; + } + if (bottom) { + adjacents[ai] = i + width; + ai += 1; + } + return adjacents; +} diff --git a/day16/build.zig b/day16/build.zig new file mode 100644 index 0000000..07ad289 --- /dev/null +++ b/day16/build.zig @@ -0,0 +1,34 @@ +const std = @import("std"); + +pub fn build(b: *std.build.Builder) void { + // Standard target options allows the person running `zig build` to choose + // what target to build for. Here we do not override the defaults, which + // means any target is allowed, and the default is native. Other options + // for restricting supported target set are available. + const target = b.standardTargetOptions(.{}); + + // Standard release options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. + const mode = b.standardReleaseOptions(); + + const exe = b.addExecutable("day16", "src/main.zig"); + exe.setTarget(target); + exe.setBuildMode(mode); + exe.install(); + + const run_cmd = exe.run(); + run_cmd.step.dependOn(b.getInstallStep()); + if (b.args) |args| { + run_cmd.addArgs(args); + } + + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); + + const exe_tests = b.addTest("src/main.zig"); + exe_tests.setTarget(target); + exe_tests.setBuildMode(mode); + + const test_step = b.step("test", "Run unit tests"); + test_step.dependOn(&exe_tests.step); +} diff --git a/day16/src/main.zig b/day16/src/main.zig new file mode 100644 index 0000000..a7a7c95 --- /dev/null +++ b/day16/src/main.zig @@ -0,0 +1,9 @@ +const std = @import("std"); + +pub fn main() anyerror!void { + std.log.info("All your codebase are belong to us.", .{}); +} + +test "basic test" { + try std.testing.expectEqual(10, 3 + 7); +} diff --git a/day8/build.zig b/day8/build.zig new file mode 100644 index 0000000..a1c048f --- /dev/null +++ b/day8/build.zig @@ -0,0 +1,27 @@ +const std = @import("std"); + +pub fn build(b: *std.build.Builder) void { + // Standard target options allows the person running `zig build` to choose + // what target to build for. Here we do not override the defaults, which + // means any target is allowed, and the default is native. Other options + // for restricting supported target set are available. + const target = b.standardTargetOptions(.{}); + + // Standard release options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. + const mode = b.standardReleaseOptions(); + + const exe = b.addExecutable("day8", "src/main.zig"); + exe.setTarget(target); + exe.setBuildMode(mode); + exe.install(); + + const run_cmd = exe.run(); + run_cmd.step.dependOn(b.getInstallStep()); + if (b.args) |args| { + run_cmd.addArgs(args); + } + + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); +} diff --git a/day8/input b/day8/input new file mode 100644 index 0000000..a1bcd3a --- /dev/null +++ b/day8/input @@ -0,0 +1,201 @@ +gbdfcae ebcg cfg gc facegb fecab acfge cbfgda fedag caebfd | ecbg bfcagd faegc gcf +eacgf efcab fgc fedagc gdeaf cged aebfgd adcgfbe gc bdgcaf | fgbacd cfega ecdg cg +dfgae gcadef efb eb dcabf bgde edfba bcfaeg egcdfab fbgade | bged eafdb eb gfdea +aefdb cafdgeb egdfac egdcba fcbd efd eadcb caefbd df aegbf | cfadeg abfedgc fde bfcd +dc deafg ecd dbaefc adcfeg cfged ecbfg acdg cafdegb gfeadb | dcga edc adfebcg ecgfb +befdc bcfge befad degfab cde aecbfd gcedaf eafcgdb dc bcad | bdeafc cde ebdcafg daebgcf +cd egdacfb fdc fecgb gabdf fbcdg fcgdeb gdcfea debc ecfbga | gfecb fbgecd bcgef dc +fagcdbe gec gbdea ce bgedc aecd cgbeaf cfbdg gebcda dbfgae | cfbaged cgbeaf ceg gadfbe +gcbaf gfdb bdafc df adf adcbefg dcfeag cebad adfgcb faecgb | afd agfedc dcbfa gdfabc +fbae cef fecdbg afbcg cefbga fe acdfgb gadce fbdacge fcage | fe abef gfbac cef +cfdebg bg beg cabfeg fbgd acged ecgbd fdcbe cfbeda cebgadf | fbedc gadce gdaec bcfed +gba ab fgbcd gedaf bcadge afbdgc cdgfbe agdfcbe bcfa bdfga | gab gdbfc ba fgdbcae +fabdgce bfdgac cbaed dcb bgade fbaec fcde edbfac gfabec cd | dc cefd dc cd +afc dbecafg fagce fbega fbgc edgca agecbf bafged eacdfb cf | cf bfdcaeg gfbc gfbc +gedcb ebda cgbdfa fcaeg gab bfgdce ba agbce bdgecfa dgebca | bdgfce cebagd cbdgfae dcfbeg +egbacf acdfe gfecb egdfcb dfgaceb dfb dgcb fdbce db bdeafg | fdb db db bdf +cfeg eabcf gdefcab bef geabc bfcad egbadf cfgbae egcdba fe | fgec gaecfdb cagbef ef +bafde dbgeac feacgbd cbaef bed bfgaed gafecd bgdf bd eafgd | db cfgdae caegdfb fabed +afg bacg cdbgf gcdfab fdcae gafdeb dbgfce ga dcafg cedabfg | gaf bdafgc agbc ga +edgca bafdcg acbdg cbdeag dce fgcea gdcfeb aebd ed gfdabce | ebgcad daeb edcga ced +agecf abfegc dbefcga ad dafc fegdb adg afgde bdcega ecagdf | fadeg dfac dga da +ceagdb gedac acfdbg cda bgecafd adgeb dcefg ca beac abgfde | aegbd baegd bdcagf abcdeg +egaf bagdce dcgabf ea eab cgbfa cdfebga fbcae dfcbe fgbace | gdfaceb gfea dbcfe eba +cfbega bge eg cgdebaf gedbc adeg bceadg bfadcg agbcd cdefb | ge ge cfedb fbgadec +afc ca cabe gbefcd aebcgdf acedf gbafdc eagfd edcfab dfbce | gbfdcea fca aecbfd ceba +af bcdag agbfde fdbca ebdfc adf dbefcga abedfc aefc dbgcfe | af acfe adf adcfbe +badfe agebd gfaced gbdcafe aecfd fab dbfc geacbf adbcfe bf | fb ebagfc beadf dface +bfeda gabefd agbedc ec fcadeb dfbaecg cea fdec afceb agfcb | fbcae cfde eac fedc +ebcf eafgcb egdfba dfcag bacge faceg afe gdceab ef dgcbaef | fcbe febc bcaeg cbfe +dafbc afdebg egfc ce eagbcd ceafgbd fgeba cbefa eac fcgeba | afegb cdagbe cbdaf gfaeb +ecd dface cdgafe gcabfd edbgcfa gdacf fecg cdbaeg ec efbad | edbaf gcafd dcgbaf dafgceb +gd fagbec bdfg gde cfgbe cefdbg gbdeacf agdcef bdceg bcdea | dg gcfeba dg ebdcg +bg bgdeac abdg cgb fcbade gdbceaf fbdgce dbeca gaebc aegfc | cgb befgdc fedgcb gbc +gac cgeda egcbafd gfdbce fadec ag degbca agecbf dagb gebcd | cdegb degcbaf cedaf edafc +ecfab bfacdg bdga gb cfgba bfg gefdacb cegdfb fagdc cafedg | bfg cefba gb aedbcgf +dgfce aeg cfbdgae acfgeb ebcafd ag gafed gaebfd bdga dbfea | gae cfbdae abedfg bgfdae +cgbad cbaf fbgeda dfgcab fdcebg dbfcg decag cdagbfe bga ab | dfcabg cbfa fcedgab adfcbeg +cegbadf edfgba cafeg dc agdebc dbgfe cdbefg dec dbcf edfcg | bdfc facdegb adfegb egbfd +bedf cefadg ced ed bcegd gefabc agcdbfe cdgab gcfbe fgcdbe | ed cde bdgec dbecfg +egfcab debfa ecd bfgec cdgb cd bdcef dgebcf ecdabgf egcadf | becfd cbgd gcdb fcbed +bc dbfge gbecf caefdg gefabc abgdcf bfc baec efcag cebgafd | baec fegbc agfec cgbef +bacegfd fbdgca egbcf fabce bdaefc dcaef dagefc ba dbae cab | daeb cbefa cab ab +fgab cgdfae fcgebd gb bcg dbaegcf cabed gadcb gcafd gbfadc | gbc adcgf dgecbf cgb +gdaeb df cgaebdf dfbe cbdagf eabdcg gfd aecgf aefdgb agedf | dfg gfcea febd ebdag +ecbaf cagfdb aecgf gac ebga defagcb baedfc gfcde ag gfbeca | cadbfg gaecbf agbfec fecba +edbgaf fbd egdbfca dagf gabde bdfge fd bgcfe efbcda eacdbg | fbd ebcdaf dbgaefc gefbda +acdbg aefcdg cdg cegb acebfd abgdf cg fbegadc gcbdae eabdc | gc edafbcg gadbc cg +fgcd bgdafe bacge dg facgde acfde agdec gdefbca deg aebcfd | gd eagdc ged ged +egfa efdcba dfbgc cbfea ga abcfg edgcab bga fecgdab aebfcg | bga faegcbd bfeadc badcge +bfeda ceagdb bf ebfacd edacb acefbdg begcdf bafc agfde feb | fb fb fbac bcegad +bfdcea dcbgaf cbega dbage cea ce cbafg gefc bgacfed bfecag | abgfc ec cegf acdefb +adgfce cdag cd fgecab cfgae ced fdbae gebdfc agbcefd ecafd | dfcae dc fdcae ebafcg +dbeag begfad bfdegac dcb cb gcfda ecdgfb ceab gcabd dcebag | cbd cb cegbda bgcdef +cdfaeb fgebad ea dcbgfa gcaebdf cegbd bdcae fabcd cfae eba | caef bafdce afec gfdbea +bcfega gcbea bdfgeca gabcd bd gdcabe dafcg ecdb dba ebfadg | fceadbg fbaceg db bd +gadcbe dgb debcaf fabgcd bcgfe bdgcf acfedbg gd cafdb gdaf | eafdcb bcfade badceg bdacfg +gafce cabde cdf aefbdgc efacgd fgeacb ceafd df efgd badfgc | edfg acbegf dfc gedf +afgce gbdecf bae adebcgf bgedca abecg abcd baefgd ba gbdec | ab dfagcbe gbeca ab +de dcgaf gcfbed gdebfca acegb cde bcgafe abed gdacbe gecda | ed afgcedb deagc gcdaf +gcdef ebfadc da dabefg daf fbaeg gaefcb dfeag gbdfcea bgad | gadfbe bafgce dgba afd +bdfeg gecf fc egdfcb gcbedaf bfc dfcbe cbdfga baced abedfg | fcge fgdecb dcgfabe fgdeb +bdf acbdfg bdaefg aefgbc abcde dagebfc fd dfcg cabdf cgfab | bfadc cabfg gfcdab fbaegc +af cdfbega edbfac dfbgc fac adef adecb becagd acdfb efcbga | bcagfde bdfcae gfdecab eacbd +egfcad bdgeac fdac fed cdage fd gcbfed baegf gfedcab gedfa | adceg fd afebg egdca +fegba ecbafgd cfegba cgdab efbgad ec fadcge ecg bcef cegba | bgadc ce fecgdba baceg +bdfaeg abgedc efadg bafd gfeba fcdebga adg aecgbf da ecfdg | ad ad dfba befcgad +efadcg dacfb cfgab bfde eafdc decfbag decbfa bd bcd acebgd | debf cafde gcdafe fcgebad +agd cgadef gcdebfa gd facge bdfgea eagbfc dcgaf cafdb ecdg | cbgfae ebfcga agcef bfdeag +dfbge abefgd aefbcg dcfegb ba fgbad gedafbc bdea fba dcafg | agfdb fab fba fcbgde +fcdab agbecd eacgfb gc fgeabd gdabc dgbea cbfdeag cag dceg | fcadb fbcad degba gca +eag cbefg dfeagbc cdfbga egcba dcegba dace agedbf ae gadbc | cegba age agbdc dbcafg +cdbag gfb dgceba cbfa gfdbac faebcgd deafg dgbaf fbegcd bf | eagdf febgcd fbdag gdfbce +cdbgf afcebg acfdbe ed ced bdefagc dcgbea fbeac cbdfe adef | cfbde de dec ecd +dfc fbcde dfcbga ecbgd egbdca fd eabfc edbfgc efdg cdfegab | df dgef fgbaedc df +fcg egcbd gacefb dfgbc abfcgde bfdga cfed cf gdebac gbecdf | bfdcg fgc fbcdg agdbf +fcabg acfbedg bfcgde ecbad agef ecfgba ecf fe dafbgc baecf | aegfbc cfe dbace fec +edbagc cefbda bfdeg af agcf gfebdca fgdcab dgcba baf fabdg | bcadef dfgcab abdcfe gedfb +acegf efbgcd dgbefac cgd bedga cbad agcedb dc egadc feagdb | badcgef cd dcgbfae cd +bfc cdafeb bcega gdcf afgdbe egbfd caebfdg gcefb cf debcgf | gcebf bgedf dfbgea fc +dgfaceb gcdf dabcg fd dfgba aefcbd bdacge gfabe afd dcagbf | daecgfb gbecda dacbfe gdcba +fbgedc bfcda cfg dafbge fecdbag cagfd aecg agfde cg adgcfe | ecdfagb gfceda gaebdf gbfdea +afbe gfa gfdcb af febcadg faegcb bcgea aedcbg eagfcd afgcb | bagcf gfdeca abfe af +caefg efd fd abedc fcgd egdcaf egafbd gfcbea dafec abgedcf | gaebcf fcedga def egcafbd +dcfab gefdcb bda da baefc baedcfg gfda dbceag cbfagd fbgdc | cdebga efdgbc fbcgead beafc +bgfed fdaegcb bedagc da bdacfg gda eacfgb cadf dafgb cabgf | dgceab da dfbeg bcegad +cedab fc adbfec faegcb adbgf cfa ecdf ebagcfd dcbfa ecgabd | becdaf cefd abdfc efcd +cdgbea aeg agdec dafgbc gbed aegbcf ge aecfbgd cagbd cafde | ceafd bagcd gacdb cgbad +dgfeb bedag cgdefa df gfdaeb cbdega gfdceba gbfec dafb gfd | fabecdg bdfa bdaf dfba +gdfe acdbf ed bdegcf cegbfa cebfd deb badegfc fbcge bgaced | ebd efgcabd bfcda dfeg +abfcedg gec fcgdbe gaebc gebfac efbdca gfea acbfe gbdca ge | agfe cge cdbgfe bcfdea +fgbaed dacegb dacef egafd aecbgf adfcebg bdfg ged agfbe gd | agdfceb gd dg fbgd +egdb acgbef edfac bd fgecb gbadfec bcafgd bcd bedcf gdecbf | bdc bedg gdbe efbgc +cagbef acgbf fgcabde cgd gbda dg dbfgec ecafd dgfca dagbcf | agbcfe dcgfa dbagcf cgdafeb +cb efbgc cgfae cgb gceafd beca fbcgae gefdb cbgadf dcfabeg | bgeacf bacedfg cbdgfa dfgbe +cgbedf facgdb agfdc cga cafed gfcbd gcdbeaf degacb fbag ag | gbaf acfed gafcd cgfda +egbdfca cadf bdafgc cbgfea cbgde bdcfg gfc cf fdbag gebdfa | eacdgfb fcda dfac adefcgb +fdgea abcd ac cfabge cdbfeg gdebac acg gcdebaf gadce bcedg | gac abefgc ac gcaed +eafdc eafbc agbe begcadf ba gcefba fgcbda gdbfce cba ecbgf | ba ab abeg adcgbf +afg ga bdafe eadfg dagc gefcab edbcgf ceagfd ecgdf aedcbfg | agdc daefb debcafg fdaeb +baf cbdgf afbecd gdcabfe dcfabg fadeg gbac gbadf ab cedgbf | fcdeba cbga afbdg bgca +ebdfgca ebfdag gb bgf fdeagc gdbe cdfgba egfab abcef agdfe | gb gfbdea gdeb eafbg +agbcdf fe gebad fea becf ecfbga cgabf aegfb fedgca acgbedf | ebgda ebdgacf fcbe aef +gdbef bgfadec gfeab eab gdcfbe ab bfadeg fdab dcaebg acgfe | fgadbe fbedgc dgefb aeb +adcbf fbdg bcf dcbag fecad cgafbd fb acdefgb bgefac dgcabe | dfcab cfb bf cdeaf +dfgbca cdebagf cfe fcbga aefdg geafc bdecgf ce bgefac aceb | bace aebc gfbac efc +adfegcb gcbaf fa adgcb acf efgacb fgcbed eadfbc agef fbgce | aefg acf gafe cfa +fdegbc fcbad ec ebgaf gecfdba feacb caefgb degfba ecb geac | ecb ecb cfadb ebafc +fegabc ebcgafd gfacde ge fcgeb ceg bagcf dcfbe ebga bdcfga | cefabdg fdceb abgcf gfeadc +fdabcg bgafc fcbeag edbfg gadfceb ad adb dfca dcbgae dfabg | bdegf faedbcg da caegfbd +cgfae gdbeca decaf fd dfbc fad dabegf daebc bedafgc efbcda | decfa fgcbdae fcbead dbegcaf +dbeaf cb fabecdg bcfda gabfdc cbfg agdfce abc edgcba cgadf | bcdefag bcgf dafbc badcgf +gbdcfa becfdg fdbcgea fgc cg fecdg efdgb ecadf gceb gadefb | gcbe fgcebda daecgbf aebcfgd +cdega dgcaeb cbgea egdcaf gfacedb begfda cebfg cbda bag ba | ab egdac beafdcg faegcdb +gab cgfab cfage fbaedc bcdg gb gacfbd defcagb gdebaf fadbc | bg fbacde gfabc dceagbf +ge ebg cgdfb cedba bedgc ecag deagcb bfceda gdcfeab egdbfa | bge beg bafged cadbeg +eabd ebacf ecd fdcbg dbafegc ed ceagdf fgceab ecfbda cdbef | dec dfcabe dec befca +afbgdec cedb dc agdbf cegbf bfcedg cdg bdgfc baecgf ecfdag | cd dgefbc fdgcb cd +gebdaf dgfae afe gafcbed agcfd febd edabg gbfeca bcdega fe | ebdag fedb egafd aegcbf +gebdcf abefdcg deg bgdfc fcde gbfea gedabc edbgf ed bgfcda | efdbg fedacbg gbfacd fbdeg +bcgd edagc gaebd bcdaeg fabedc bgefa fcadge agbfcde db adb | cgbdfae bdagcef badegc dab +df fdb bfadceg bgeadf afbdce efdg dgbae dafbg cafbg abdgec | gdfe afcgb ebcgafd fbgacde +gabce gdfaeb bcad cebdgfa fgcbae efgcd dga da cgdea cdeabg | ad agd gda gfedc +fcabge edbfg fcaebd ea dcbagef gace gafcdb bafcg eaf fgbea | dcfbage aceg afbdceg ebcafd +bafe egfcda be bfgaec gbe egcfbad fcaeg bdcaeg bgecf cfbdg | fabgced bfea gbcef gcfeb +badfeg fgecb af cgaebd acfdbge gaf afcged gfaeb dfab egbad | fa fga abfd gabde +afceg bcdfge fcaebg baegdcf cfg eabcdg bgfa efcad gcabe fg | abfg gf fbgecd acebgd +acbedgf fb efgdc bgf gbcfd bgfdac dagcb fdbeag ebcdga fcab | bf gdebaf fb bf +aecf cgbdefa bacged efbcga fdgcab febag af agf dgfeb ecabg | bcgdaf af cadfbge efca +egfda fgceab acd bfcadeg dc dcega bfdcea gcdb cdbega eacbg | gabecd cd befgcad gdcb +gecda dfegacb cgfae cdbfag fcbeag afg gf fbge dafecb caebf | dfaebc cfeab gf agdfbc +gdafe cdgaeb egfdba da bfdgec gbfed eagcf egdbafc fbad dag | agfde dga dcgbfe abcdfeg +acgdb gadfce cbefdga becad bcegdf bacgdf afbg ga dag dbcgf | abdgc gdeacf acdfbg ecdab +gdfa eadbc gcafe dgc dg fcagde bcgfea decgfb egcda fbecagd | cegda cgdea gd dg +ga egdbcfa aegcb cag egbcd gadceb gbcefd decafg badg cabfe | bagd cagfed gacdfeb cbegd +cb deafc bcgd agfceb dbfega ecb deacb aebgd cbafdge ebdacg | dcgb adceb edgba ceb +edgfab acgb becfdag bgf cgbdf cgfad dacbfg bg ecdbf afegdc | gfb bg gcfade gfbdeac +caebg cgaf acgdfbe begcaf cf fdgeb agedcb ebgfc cfe fbcdea | dbfeagc cfe fbeagdc cf +ageb bg adcbfge fcgea cbgaef cbdfa gfb fedacg bfgca fdcgeb | fbg afdcb ecgbdfa cgfdbe +dgbfe aeg ea dcea dbeagfc fbacgd ecbdag gadbc bcafge adgbe | aedc ae gbdef daec +dbafcg dbgaefc adfc egdab fdg df agefbc bdgcfe fbacg gdfba | bedgacf gcbaef cdfgab cafd +eg fdgcabe gcadbe aebdc gced bagfde gea gbeac afbcg ecdbaf | agfbc fgdabe fcdgaeb feabcd +fedb gfe defabgc caedg cgadbf feagd bdagfe fgbda efagbc ef | fe fgcbea ef dbfe +bc agcb dfceg bafgdc gfabd fbdcg bcd ecgfbda ebadgf fbaedc | cbd fabcgd cgbfd bc +ebfgc dbeg ed gefdc cde gfbecd fgdca caedfb gdbafec gefcab | fgdecb dgfce fbecg cabgfe +ab ebda bfagdc becgd aecfg bag daecgb ebcag gbacdfe cfgbde | abedgc eafgc ebacg ab +ecdbag dagbfe badef fcaed adc cd efbagdc cafge cfaebd dfbc | dcfb dca acd gcdabe +cedbgaf ecbfg dagfec dgbea bdca gfdaeb ecgbd dbecga cd edc | ecbdga cde edbag dc +egbfcd gbcafe ed defga dbgfa bcgdeaf cdea def eacdgf gacfe | bgfad ecda gbfad gcdefb +dafge cea fdace agdc dbgfea ebdfgca dcbfe acedfg abecfg ca | defac gcad ac ca +ecdba fbac eabcdf beadgfc acd efacgd ac agedbf bfdea ecgbd | aecbd agbefd bdgec dgebc +bfegda bdage gdf gfdae bdfgace fgeb afdec fgdacb gf deagcb | fbaegcd fegb aedgb dfg +cdaf fd bcdfge agebf dcgfae gfead baedcg gdcea egdbacf dgf | efgab df eagfd afgbe +egfca egcb ceafdb fbc fbcaeg dgbfa cdfeagb fgcba agdcef cb | cb gcdfae eabgfc fgbca +gdbca fdecba gdecfb feagbd ceb adfbgec bdgec ec efgdb cfge | ec ecbgd dacefb abfged +edbcag cabd ecfdgb aegfd eagfcbd ac cga feagcb ebdgc dgeca | acfbdeg dfgea cga dgeca +ebgacf adcf bcgedfa gdcabe abedf aef edabc fa befgd bfdaec | aefdb af cebad fae +dageb de gde dgfbce cgdba gabefd afbge cgdebfa aefd ceafbg | efgbda agbdecf bfaegcd edg +ce dec becf efdgb edfgcb ecbdag gdaefb beafgdc dagfc fecgd | ce fdbage gcdfeba gbcadef +ecgfa ebadcf ed gcdfab cdefa gfdaeb dbec dbfgace cdbfa dfe | dceb cdfab ed ceagf +cadg efgcdb gbcfad feabc agfcb afcdgbe dgbfa gcb fagbde gc | gdecbf adfgbc fbcgade gafbc +gdcefa gefdb bcfeg eabc fagdbc ecagf bcf afcebg cb bfgaedc | fcgdba agbecf fcgeba fgdceab +afgbc be cbeaf gfeb ebcdfag cfdbag cdfae cfegba cbeagd abe | gdecbaf cagfb bfgaec cabfdge +eb egdb dcagb abe gcdabe agbce dgabcf fedbca fdcebag gfeca | gdbe agdcb gfcae cgadbf +dbagc bcaedfg gade cadeb efgabc cabedg gca fgdbc ecafdb ag | dage cagdb dgcaeb cdgab +gbecd eacdg cgb egacdbf gfdbec efbg bfced bg bdfcea bafgdc | acbfde bg gb gb +fgbadc fbdag dbecg fbae dacgef ega ae afcbged ebgfad bgaed | ae bacfgd bfea agdbef +dacfeg gecfd ad gadbfe gadbfec gda acde fcedgb acbfg fdcag | gefcd cdegf agd ad +caegbd cfdeba edbac cfbegad fe fbea fdagc cef acdef bedgcf | ef cdafgeb fce edfca +dgcae cedbfag facbdg fdc eacdbf bfegda dfcae fdbae cf cfeb | cf febgcda dfc cdf +edfag dbeacg dbecafg agc gcefa gc bcfea cdfg fbdgae gefadc | dfacge fagbecd feacbdg bfaged +abefdg fgdec ecadf efcdab ac dbeaf acdb acf cdegfab gecbfa | fca fdbega decaf cgafdeb +bceafd fegcad edcagbf dbcage decba bafd df fcd bcfge cbfed | ebgfc eacbdf gadebc df +egba cgdafbe begfc gacdfb dceaf cefbdg gfa ag gcebaf aecfg | acbgfd gabe fbgeac agfec +cdgeb fdecag agdbefc gefbac cdaf ade fegdab cgead ad caefg | ecbgdaf dgceb cfgae dea +cafbde fbgac edcg agdfeb eg dacgef fgcea aefdc dceagfb eag | afgec fbcga ceafgd bgceadf +cefbg ceabd gd edbacgf aedfbc gaedcb cdbeg abfdcg gdc gead | cgd dgea gd cebad +fadbg dagecf fea fbecga adce ae bedfcg egacbdf fdgec fdage | dcae caed dbefcg adgfb +acged aec efgdcb ac fagc abdge abfdegc fdeacg cfaedb gedfc | egbcafd gaefcdb cae cagdfe +fcbdage bafgec cab fcbae ac abdegf dbfec fbgae eagc cbfadg | acb dafegb acb bfeagdc +af egfda gebdf gebdfa aefcdbg fbecda fgba daf cdeag dgbcfe | eagdc cefbgda daf daf +dgebcf gcf agcbd fbecd fbcdg ebacgf aebfcd fdeg fegdabc fg | dacgb dfge fcdbe gedf +bfacdg gafcb gefbd cd dgcfb dagc bdfagec bceadf fagceb bdc | begfd gbdfe afgcdbe fbeacgd +bagf afe bdcaeg fdcbe af dafbe cbedgaf gfebad gdaeb aefcdg | baedcgf fa af cgadbef +acdgf becgfa acd cagbf agcdbe fdba da bdcfag gfdec dcgafeb | bagfc da dca fdbacg +cdabfe adbfc ecbaf cdae cdbfge dabgfc bfgae bec ce gadcbef | faecbd beadcfg dcae cgfedab +bceag agd aced gdbafce bcfgae agdeb ad fabdcg fdbge egcbad | gaceb agd bdagec da +cag dfgbeac ecdbgf ag gfabc ebag fabgec deafgc adcfb fcegb | dgaecf aebg bgea dcbfa +afdb degbc agedcf da aed beacfg dgfecba efcab bdface cbdea | fbeca aecbd gdecaf ade +cafebg afedcb gfeabcd fgcb cfa afdeg fc ecgab dacebg cfeag | bcgf bgcae gdfea bgcf +feg gfcaed cfabdge gf degafb fdegc edfcab edgbc agcf cfade | gf gf gacf egf +ceadf ecdgf eacbd cebagd dcabfge eaf fdceab fbad fgebca af | cefadb cefad bfad abedc +gefabd cabdfeg dgceab dagef bdfacg egfdc edgab af feba fda | geadcb dagfceb fda af +dfcea adgce dfebc gecfdba fadgbe dcefga cfga aebcdg efa fa | fa fae af gaedbf +agbfd acbgedf faecb cegbad dgabfe gbc fcdg cfagb gc afdbgc | cg cg dfgbae cdfg +efg bfacg feab efcagd fe bcagedf edgbc ebgcf afbegc bcfgda | dgecfa ef fgcba efba +aebfdc bcfage dgbacf gface gbaecfd dfgce geab acfbg ea eac | cabfg ea cfagb eagb +gba gdcea cdbfg acgdb gbecadf bfgecd dgcfab abfc ba fdeagb | ab febagdc dfgbac fdagcb +dfcb gcebd dfgaec efdgc eagcb dfaebg afedcbg gbd gbcfde bd | gadebfc db febdga bgd +eabd adcbg ceafg ecb be eacdbg acegb gbfcde adgcbf ecfdgba | cgfebad cbe ebacg dfgceb +cefbgd cgdfa bagfced dabe fgceab abfeg gdbaef gbfad db gdb | aefdgb bd abdfegc fgabed + diff --git a/day8/src/main.zig b/day8/src/main.zig new file mode 100644 index 0000000..5e4036f --- /dev/null +++ b/day8/src/main.zig @@ -0,0 +1,128 @@ +const std = @import("std"); + +pub fn main() anyerror!void { + var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); + defer arena.deinit(); + const alloc = &arena.allocator; + + // Read our input + var f = try std.fs.cwd().openFile("input", .{}); + defer f.close(); + var contents = try f.readToEndAlloc(alloc, std.math.maxInt(u32)); + defer alloc.free(contents); + + var digits_in_output: u16 = 0; + var it = std.mem.tokenize(contents, "\n"); + while (it.next()) |line| { + var lit = std.mem.tokenize(line, "|"); + _ = lit.next(); // skip output + var oit = std.mem.tokenize(lit.next().?, " "); + while (oit.next()) |digit| { + // 1: 2 segments + // 7: 3 segments + // 4: 4 segments + // 8: 7 segments + //std.log.debug("'{s}': {} digits", .{digit, digit.len}); + if (digit.len <= 4 or digit.len == 7) { + digits_in_output += 1; + } + } + } + std.log.info("[Part 1] Output values contain {} digits that are 1, 4, 7 or 8", + .{digits_in_output}); + + it = std.mem.tokenize(contents, "\n"); + var output: u32 = 0; + while (it.next()) |line| { + // Each line in 'input | output', and has a different mapping + var lit = std.mem.tokenize(line, "|"); + var o: u32 = try find_output_value(alloc, lit.next().?, lit.next().?); + output += 0; + break; + } +} + +fn find_output_value(alloc: *std.mem.Allocator, inputs: []const u8, outputs: []const u8) !u32 { + // we use ips for storing both inputs and outputs to try and + // solve which letters correspond to which display segment index + var ips: [14][]const u8 = undefined; + var ops: [4][]const u8 = undefined; + + // get pointers to our ips and ops + var it = std.mem.tokenize(inputs, " "); + var i: usize = 0; + while (it.next()) |ip| { + ips[i] = ip; + i += 1; + } + it = std.mem.tokenize(outputs, " "); + i = 0; + while (it.next()) |op| { + ops[i] = op; + ips[i+10] = op; + i += 1; + } + + for (ips) |v, k| { + std.log.debug("Input {}: {s}", .{k, v}); + } + for (ops) |v, k| { + std.log.debug("Output {}: {s}", .{k, v}); + } + // segments indices: + // 0 + // 1 2 + // 3 + // 4 5 + // 6 + // for each index, we can have some number of options + var segment_possibilities: [7]std.AutoHashMap(u8, void) = undefined; + i = 0; + while (i < 7) : (i += 1) { + segment_possibilities[i] = std.AutoHashMap(u8, void).init(alloc); + try segment_possibilities[i].ensureCapacity(7); + } + for (ips) |v| { + if (v.len == 2) { + segment_possibilities[2].putAssumeCapacity(v[0], undefined); + segment_possibilities[2].putAssumeCapacity(v[1], undefined); + segment_possibilities[5].putAssumeCapacity(v[0], undefined); + segment_possibilities[5].putAssumeCapacity(v[1], undefined); + } + if (v.len == 3) { + segment_possibilities[0].putAssumeCapacity(v[0], undefined); + segment_possibilities[0].putAssumeCapacity(v[1], undefined); + segment_possibilities[0].putAssumeCapacity(v[2], undefined); + segment_possibilities[2].putAssumeCapacity(v[0], undefined); + segment_possibilities[2].putAssumeCapacity(v[1], undefined); + segment_possibilities[2].putAssumeCapacity(v[2], undefined); + segment_possibilities[5].putAssumeCapacity(v[0], undefined); + segment_possibilities[5].putAssumeCapacity(v[1], undefined); + segment_possibilities[5].putAssumeCapacity(v[2], undefined); + } + if (v.len == 4) { + segment_possibilities[1].putAssumeCapacity(v[0], undefined); + segment_possibilities[1].putAssumeCapacity(v[1], undefined); + segment_possibilities[1].putAssumeCapacity(v[2], undefined); + segment_possibilities[1].putAssumeCapacity(v[3], undefined); + segment_possibilities[2].putAssumeCapacity(v[0], undefined); + segment_possibilities[2].putAssumeCapacity(v[1], undefined); + segment_possibilities[2].putAssumeCapacity(v[2], undefined); + segment_possibilities[2].putAssumeCapacity(v[3], undefined); + segment_possibilities[4].putAssumeCapacity(v[0], undefined); + segment_possibilities[4].putAssumeCapacity(v[1], undefined); + segment_possibilities[4].putAssumeCapacity(v[2], undefined); + segment_possibilities[4].putAssumeCapacity(v[3], undefined); + segment_possibilities[5].putAssumeCapacity(v[0], undefined); + segment_possibilities[5].putAssumeCapacity(v[1], undefined); + segment_possibilities[5].putAssumeCapacity(v[2], undefined); + segment_possibilities[5].putAssumeCapacity(v[3], undefined); + + } + } + i = 0; + while (i < 7) : (i += 1) { + segment_possibilities[i].deinit(); + } + return 0; +}