From b9991fda618f03bc4c631743fde45144637811d2 Mon Sep 17 00:00:00 2001 From: ptrcnull Date: Sat, 18 Dec 2021 00:27:56 +0100 Subject: [PATCH] 14: part1+part2 --- 14/input | 102 +++++++++++++++++++++++++++++++++++++++++ 14/solve.zig | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++ 14/testinput | 18 ++++++++ 3 files changed, 245 insertions(+) create mode 100644 14/input create mode 100644 14/solve.zig create mode 100644 14/testinput diff --git a/14/input b/14/input new file mode 100644 index 0000000..036aaf0 --- /dev/null +++ b/14/input @@ -0,0 +1,102 @@ +PSVVKKCNBPNBBHNSFKBO + +CF -> H +PP -> H +SP -> V +NO -> C +SF -> F +FS -> H +OF -> P +PN -> B +SH -> V +BO -> K +ON -> V +VP -> S +HN -> B +PS -> P +FV -> H +NC -> N +FN -> S +PF -> F +BF -> F +NB -> O +HS -> C +SC -> V +PC -> K +KF -> K +HC -> C +OK -> H +KS -> P +VF -> C +NV -> S +KK -> F +HV -> H +SV -> V +KC -> N +HF -> P +SN -> F +VS -> P +VN -> F +VH -> C +OB -> K +VV -> O +VC -> O +KP -> V +OP -> C +HO -> S +NP -> K +HB -> C +CS -> S +OO -> S +CV -> K +BS -> F +BH -> P +HP -> P +PK -> B +BB -> H +PV -> N +VO -> P +SS -> B +CC -> F +BC -> V +FF -> S +HK -> V +OH -> N +BV -> C +CP -> F +KN -> K +NN -> S +FB -> F +PH -> O +FH -> N +FK -> P +CK -> V +CN -> S +BP -> K +CH -> F +FP -> K +HH -> N +NF -> C +VB -> B +FO -> N +PB -> C +KH -> K +PO -> K +OV -> F +NH -> H +KV -> B +OS -> K +OC -> K +FC -> H +SO -> H +KO -> P +NS -> F +CB -> C +CO -> F +KB -> V +BK -> K +NK -> O +SK -> C +SB -> B +VK -> O +BN -> H \ No newline at end of file diff --git a/14/solve.zig b/14/solve.zig new file mode 100644 index 0000000..68c1b68 --- /dev/null +++ b/14/solve.zig @@ -0,0 +1,125 @@ +const std = @import("std"); +const print = std.debug.print; + +const PairMap = struct { + map: std.AutoHashMap(u16, u64), + alloc: *std.mem.Allocator, + + fn squash(pair: []const u8) u16 { + return @intCast(u16, pair[0]) << 8 | pair[1]; + } + + fn init(alloc: *std.mem.Allocator) PairMap { + return PairMap{ + .map = std.AutoHashMap(u16, u64).init(alloc), + .alloc = alloc, + }; + } + + fn deinit(self: *PairMap) void { + self.map.deinit(); + } + + fn get(self: *PairMap, pair: []const u8) u64 { + return self.map.get(squash(pair)) orelse 0; + } + + fn add(self: *PairMap, pair: []const u8, value: u64) void { + self.map.put(squash(pair), self.get(pair) + value) catch {}; + } + + fn result(self: *PairMap) u64 { + var counts = std.AutoHashMap(u8, u64).init(self.alloc); + defer counts.deinit(); + + var keys = self.map.iterator(); + while (keys.next()) |entry| { + var key = entry.key_ptr.*; + var value = entry.value_ptr.*; + + var left = @intCast(u8, (key & 0xff00) >> 8); + var right = @intCast(u8, key & 0xff); + counts.put(left, (counts.get(left) orelse 0) + value) catch {}; + counts.put(right, (counts.get(right) orelse 0) + value) catch {}; + } + + var min: u64 = std.math.maxInt(u64); + var max: u64 = 0; + var it = counts.valueIterator(); + while (it.next()) |value| { + var actual = @divFloor(value.* + 1, 2); + min = std.math.min(min, actual); + max = std.math.max(max, actual); + } + + return max - min; + } +}; + +pub fn main() !void { + var alloc = std.heap.GeneralPurposeAllocator(.{}){}; + defer std.debug.assert(!alloc.deinit()); + var gpa = &alloc.allocator; + + const stdin = std.io.getStdIn().reader(); + + var res: u32 = 0; + + var mapping = std.StringHashMap(u8).init(gpa); + defer mapping.deinit(); + defer { + var it = mapping.keyIterator(); + while (it.next()) |key| { + gpa.free(key.*); + } + } + + var pairs = PairMap.init(gpa); + defer pairs.deinit(); + + var tmp: [256]u8 = undefined; + var maybe_polymer = try stdin.readUntilDelimiterOrEof(&tmp, '\n'); + if (maybe_polymer) |polymer| { + var i: u32 = 0; + while (i < polymer.len - 1) : (i += 1) { + var pair = polymer[i..i+2]; + pairs.add(pair, 1); + } + } + try stdin.skipBytes(1, .{}); + while (try stdin.readUntilDelimiterOrEof(&tmp, '\n')) |line| { + var key = try gpa.alloc(u8, 2); + std.mem.copy(u8, key, line[0..2]); + try mapping.put(key, line[6]); + } + + var step: u32 = 0; + while (step < 40) : (step += 1) { + var newPairs = PairMap.init(gpa); + defer { + pairs.deinit(); + pairs = newPairs; + } + + var it = mapping.iterator(); + while (it.next()) |entry| { + var key = entry.key_ptr.*; + var insert = entry.value_ptr.*; + var current = pairs.get(key); + + tmp[0] = key[0]; + tmp[1] = insert; + tmp[2] = key[1]; + + var left = tmp[0..2]; + var right = tmp[1..3]; + + newPairs.add(left, current); + newPairs.add(right, current); + } + + if (step == 10) print("{}\n", .{pairs.result()}); + } + + print("{}\n", .{pairs.result()}); +} diff --git a/14/testinput b/14/testinput new file mode 100644 index 0000000..6c1c3a1 --- /dev/null +++ b/14/testinput @@ -0,0 +1,18 @@ +NNCB + +CH -> B +HH -> N +CB -> H +NH -> C +HB -> C +HC -> B +HN -> C +NN -> C +BH -> H +NC -> B +NB -> B +BN -> B +BB -> N +BC -> B +CC -> N +CN -> C \ No newline at end of file