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 /src/10 | |
| parent | 2b5d4232879dc74491dabf54a0ddc958d66ebcec (diff) | |
| download | aoc2020-zig-master.tar.gz aoc2020-zig-master.zip | |
Diffstat (limited to 'src/10')
| -rw-r--r-- | src/10/input | 100 | ||||
| -rw-r--r-- | src/10/main.zig | 119 | ||||
| -rw-r--r-- | src/10/out | 31 | ||||
| -rw-r--r-- | src/10/part1 | 68 | ||||
| -rw-r--r-- | src/10/part2 | 65 | ||||
| -rw-r--r-- | src/10/test-input | 31 | ||||
| -rw-r--r-- | src/10/test-input2 | 11 |
7 files changed, 425 insertions, 0 deletions
diff --git a/src/10/input b/src/10/input new file mode 100644 index 0000000..20133c9 --- /dev/null +++ b/src/10/input @@ -0,0 +1,100 @@ +46 +63 +21 +115 +125 +35 +89 +17 +116 +90 +51 +66 +111 +142 +148 +60 +2 +50 +82 +20 +47 +24 +80 +101 +103 +16 +34 +72 +145 +141 +124 +14 +123 +27 +62 +61 +95 +138 +29 +7 +149 +147 +104 +152 +22 +81 +11 +96 +97 +30 +41 +98 +59 +45 +88 +37 +10 +114 +110 +4 +56 +122 +139 +117 +108 +91 +36 +146 +131 +109 +31 +75 +70 +140 +38 +121 +3 +28 +118 +54 +107 +84 +15 +76 +71 +102 +130 +132 +87 +55 +129 +83 +23 +42 +69 +1 +77 +135 +128 +94 diff --git a/src/10/main.zig b/src/10/main.zig new file mode 100644 index 0000000..993b2cf --- /dev/null +++ b/src/10/main.zig @@ -0,0 +1,119 @@ +const std = @import("std"); +const aoc = @import("aoc"); + +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(u8, 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 null; + + std.sort.sort(u32, intlist.items, {}, comptime std.sort.asc(u32)); + + var diffs = [_]u32{ 0, 0, 0, 0 }; + var last: u32 = 0; + for (intlist.items) |ov| { + if (ov - last > 3 or ov == last) { + 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; + } + + 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 { + var last: u32 = 0; + if (fillcount > 0) { + last = arrangement[fillcount - 1]; + } + + if (last == target) { + count.* += 1; + return; + } + + for (adapters[searchstart..]) |adap, i| { + if (adaptermask[searchstart + i] == 1) continue; + if (adap > last + 3) break; + + adaptermask[searchstart + i] = 1; + arrangement[fillcount] = adap; + countArrangements(arrangement, adapters, adaptermask, target, fillcount + 1, searchstart + @intCast(u32, i) + 1, count); + adaptermask[searchstart + i] = 0; + } +} + +fn groupPermutations(adapters: []const u32) u64 { + if (adapters.len == 1) return 1; + var count: u64 = 0; + for (adapters) |_, i| { + if (i == 0) continue; + if (adapters[i] <= adapters[0] + 3) { + count += groupPermutations(adapters[i..]); + } + } + return count; +} + +fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + + var target: u32 = 0; + + var intlist = std.ArrayList(u32).init(allocator); + defer intlist.deinit(); + + try intlist.append(0); + + var lineit = std.mem.tokenize(u8, 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 aoc.Error.InvalidInput; + + std.sort.sort(u32, intlist.items, {}, comptime std.sort.asc(u32)); + + var gapstart: u32 = 0; + var multiplier: u128 = 1; + + var i: u32 = 0; + while (i < intlist.items.len - 1) : (i += 1) { + // 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]); + 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; + } + } + + return try std.fmt.allocPrint(allocator, "{d}", .{multiplier}); +} + +pub const main = aoc.main(part1, part2, .{ "1998", "347250213298688" }); diff --git a/src/10/out b/src/10/out new file mode 100644 index 0000000..2c31060 --- /dev/null +++ b/src/10/out @@ -0,0 +1,31 @@ +1 +2 +3 +4 +7 +8 +9 +10 +11 +14 +17 +18 +19 +20 +23 +24 +25 +28 +31 +32 +33 +34 +35 +38 +39 +42 +45 +46 +47 +48 +49
\ No newline at end of file diff --git a/src/10/part1 b/src/10/part1 new file mode 100644 index 0000000..3e17466 --- /dev/null +++ b/src/10/part1 @@ -0,0 +1,68 @@ +--- Day 10: Adapter Array --- + +Patched into the aircraft's data port, you discover weather forecasts of a massive tropical storm. +Before you can figure out whether it will impact your vacation plans, however, your device suddenly +turns off! + +Its battery is dead. + +You'll need to plug it in. There's only one problem: the charging outlet near your seat produces the +wrong number of [1m[37mjolts[0m. Always prepared, you make a list of all of the joltage adapters +in your bag. + +Each of your joltage adapters is rated for a specific [1m[37moutput joltage[0m (your puzzle +input). Any given adapter can take an input 1, 2, or 3 jolts [1m[37mlower[0m than its rating and +still produce its rated output joltage. + +In addition, your device has a built-in joltage adapter rated for [1m[37m3 jolts higher[0m than +the highest-rated adapter in your bag. (If your adapter list were 3, 9, and 6, your device's +built-in adapter would be rated for 12 jolts.) + +Treat the charging outlet near your seat as having an effective joltage rating of 0. + +Since you have some time to kill, you might as well test all of your adapters. Wouldn't want to get +to your resort and realize you can't even charge your device! + +If you [1m[37muse every adapter in your bag[0m at once, what is the distribution of joltage +differences between the charging outlet, the adapters, and your device? + +For example, suppose that in your bag, you have adapters with the following joltage ratings: + +16 10 15 5 1 11 7 19 6 12 4 + +With these adapters, your device's built-in joltage adapter would be rated for 19 + 3 = +[1m[37m22[0m jolts, 3 higher than the highest-rated adapter. + +Because adapters can only connect to a source 1-3 jolts lower than its rating, in order to use every +adapter, you'd need to choose them like this: + +- The charging outlet has an effective rating of 0 jolts, so the only adapters that could connect to +it directly would need to have a joltage rating of 1, 2, or 3 jolts. Of these, only one you have is +an adapter rated 1 jolt (difference of [1m[37m1[0m). - From your 1-jolt rated adapter, the only +choice is your 4-jolt rated adapter (difference of [1m[37m3[0m). - From the 4-jolt rated adapter, +the adapters rated 5, 6, or 7 are valid choices. However, in order to not skip any adapters, you +have to pick the adapter rated 5 jolts (difference of [1m[37m1[0m). - Similarly, the next choices +would need to be the adapter rated 6 and then the adapter rated 7 (with difference of [1m[37m1[0m +and [1m[37m1[0m). - The only adapter that works with the 7-jolt rated adapter is the one rated 10 +jolts (difference of [1m[37m3[0m). - From 10, the choices are 11 or 12; choose 11 (difference of +[1m[37m1[0m) and then 12 (difference of [1m[37m1[0m). - After 12, only valid adapter has a +rating of 15 (difference of [1m[37m3[0m), then 16 (difference of [1m[37m1[0m), then 19 +(difference of [1m[37m3[0m). - Finally, your device's built-in adapter is always 3 higher than +the highest adapter, so its rating is 22 jolts (always a difference of [1m[37m3[0m). + +In this example, when using every adapter, there are [1m[37m7[0m differences of 1 jolt and +[1m[37m5[0m differences of 3 jolts. + +Here is a larger example: + +28 33 18 42 31 14 46 20 48 47 24 23 49 45 19 38 39 11 1 32 25 35 8 17 7 9 4 2 34 10 3 + +In this larger example, in a chain that uses all of the adapters, there are [1m[37m22[0m +differences of 1 jolt and [1m[37m10[0m differences of 3 jolts. + +Find a chain that uses all of your adapters to connect the charging outlet to your device's built-in +adapter and count the joltage differences between the charging outlet, the adapters, and your +device. [1m[37mWhat is the number of 1-jolt differences multiplied by the number of 3-jolt +differences?[0m + + diff --git a/src/10/part2 b/src/10/part2 new file mode 100644 index 0000000..6f7e947 --- /dev/null +++ b/src/10/part2 @@ -0,0 +1,65 @@ +--- Part Two --- + +To completely determine whether you have enough adapters, you'll need to figure out how many +different ways they can be arranged. Every arrangement needs to connect the charging outlet to your +device. The previous rules about when adapters can successfully connect still apply. + +The first example above (the one that starts with 16, 10, 15) supports the following arrangements: + +(0), 1, 4, 5, 6, 7, 10, 11, 12, 15, 16, 19, (22) +(0), 1, 4, 5, 6, 7, 10, 12, 15, 16, 19, (22) +(0), 1, 4, 5, 7, 10, 11, 12, 15, 16, 19, (22) +(0), 1, 4, 5, 7, 10, 12, 15, 16, 19, (22) +(0), 1, 4, 6, 7, 10, 11, 12, 15, 16, 19, (22) +(0), 1, 4, 6, 7, 10, 12, 15, 16, 19, (22) +(0), 1, 4, 7, 10, 11, 12, 15, 16, 19, (22) +(0), 1, 4, 7, 10, 12, 15, 16, 19, (22) + +(The charging outlet and your device's built-in adapter are shown in parentheses.) Given the +adapters from the first example, the total number of arrangements that connect the charging outlet +to your device is [1m[37m8[0m. + +The second example above (the one that starts with 28, 33, 18) has many arrangements. Here are a +few: + +(0), 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 17, 18, 19, 20, 23, 24, 25, 28, 31, +32, 33, 34, 35, 38, 39, 42, 45, 46, 47, 48, 49, (52) + +(0), 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 17, 18, 19, 20, 23, 24, 25, 28, 31, +32, 33, 34, 35, 38, 39, 42, 45, 46, 47, 49, (52) + +(0), 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 17, 18, 19, 20, 23, 24, 25, 28, 31, +32, 33, 34, 35, 38, 39, 42, 45, 46, 48, 49, (52) + +(0), 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 17, 18, 19, 20, 23, 24, 25, 28, 31, +32, 33, 34, 35, 38, 39, 42, 45, 46, 49, (52) + +(0), 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 17, 18, 19, 20, 23, 24, 25, 28, 31, +32, 33, 34, 35, 38, 39, 42, 45, 47, 48, 49, (52) + +(0), 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45, +46, 48, 49, (52) + +(0), 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45, +46, 49, (52) + +(0), 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45, +47, 48, 49, (52) + +(0), 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45, +47, 49, (52) + +(0), 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45, +48, 49, (52) + +In total, this set of adapters can connect the charging outlet to your device in [1m[37m19208[0m +distinct arrangements. + +You glance back down at your bag and try to remember why you brought so many adapters; there must be +[1m[37mmore than a trillion[0m valid ways to arrange them! Surely, there must be an efficient way +to count the arrangements. + +[1m[37mWhat is the total number of distinct ways you can arrange the adapters to connect the +charging outlet to your device?[0m + + diff --git a/src/10/test-input b/src/10/test-input new file mode 100644 index 0000000..e6376dc --- /dev/null +++ b/src/10/test-input @@ -0,0 +1,31 @@ +28 +33 +18 +42 +31 +14 +46 +20 +48 +47 +24 +23 +49 +45 +19 +38 +39 +11 +1 +32 +25 +35 +8 +17 +7 +9 +4 +2 +34 +10 +3 diff --git a/src/10/test-input2 b/src/10/test-input2 new file mode 100644 index 0000000..ec4a03f --- /dev/null +++ b/src/10/test-input2 @@ -0,0 +1,11 @@ +16 +10 +15 +5 +1 +11 +7 +19 +6 +12 +4 |
