aoc-2020-zig

git clone https://git.sinitax.com/sinitax/aoc-2020-zig
Log | Files | Refs | README | sfeed.txt

commit 9755ba3236266ea10d532d2de17f718b206fcaf5
parent 9ef6524cd12f950ff324f885843b25e86266dc92
Author: Louis Burda <quent.burda@gmail.com>
Date:   Fri, 11 Dec 2020 09:45:20 +0100

day 10 prepare and day 11 solution - unpolished

Diffstat:
Maoc | 54+++++++++++++++++++++++++-----------------------------
Asrc/day10/input | 100+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/day10/main.zig | 16++++++++++++++++
Asrc/day10/part1 | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/day11/input | 90+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/day11/main.zig | 161+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/day11/part1 | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/day11/part2 | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8 files changed, 574 insertions(+), 29 deletions(-)

diff --git a/aoc b/aoc @@ -4,39 +4,35 @@ function rmprefix() { echo "${1:${#2}}" } -SCRIPTPATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" -REPOROOT=$(git -C "$SCRIPTPATH" rev-parse --show-toplevel) - -if [[ "${BASH_SOURCE[0]}" != "$0" ]]; then - if [ -z "$PS1_PREFIX" ]; then - echo "Enabling AoC env.." - - export AOCBIN="$REPOROOT/aoc" - - export PS1_PREFIX="[🎄]:" - export PS1="$PS1_PREFIX$PS1" - - export PATH_PREFIX="$REPOROOT:" - export PATH="$PATH_PREFIX$PATH" - - export REPOROOT="$REPOROOT" - else - echo "Disabling AoC env.." - +function aoc() { + [ $# -lt 1 ] && exit; + if [ $1 == "deactivate" ]; then export PS1=$(rmprefix "$PS1" "$PS1_PREFIX") export PS1_PREFIX= - export PATH=$(rmprefix "$PATH" "$PATH_PREFIX") - export PATH_PREFIX= - - export AOCBIN= export REPOROOT= - fi -else - [ $# -lt 1 ] && exit 0 - if [ -e "$REPOROOT/scripts/$1" ]; then - "$REPOROOT/scripts/$1" ${@:2} + unset aoc else - echo "No such script" + [ $# -lt 1 ] && exit 0 + if [ -e "$REPOROOT/scripts/$1" ]; then + "$REPOROOT/scripts/$1" ${@:2} + else + echo "No such script" + fi + fi +} + +SCRIPTPATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +REPOROOT=$(git -C "$SCRIPTPATH" rev-parse --show-toplevel) + +if [[ "${BASH_SOURCE[0]}" != "$0" ]]; then + echo "Enabling AoC env.." + if [ ! -z "$PS1_PREFIX" ]; then + echo "Already initialized!" + return 0 fi + export PS1_PREFIX="[🎄]:" + export PS1="$PS1_PREFIX$PS1" + + export REPOROOT="$REPOROOT" fi diff --git a/src/day10/input b/src/day10/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/day10/main.zig b/src/day10/main.zig @@ -0,0 +1,16 @@ +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/src/day10/part1 b/src/day10/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 jolts. 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 output joltage (your puzzle +input). Any given adapter can take an input 1, 2, or 3 jolts lower than its rating and +still produce its rated output joltage. + +In addition, your device has a built-in joltage adapter rated for 3 jolts higher 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 use every adapter in your bag 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 = +22 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 1). - From your 1-jolt rated adapter, the only +choice is your 4-jolt rated adapter (difference of 3). - 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 1). - Similarly, the next choices +would need to be the adapter rated 6 and then the adapter rated 7 (with difference of 1 +and 1). - The only adapter that works with the 7-jolt rated adapter is the one rated 10 +jolts (difference of 3). - From 10, the choices are 11 or 12; choose 11 (difference of +1) and then 12 (difference of 1). - After 12, only valid adapter has a +rating of 15 (difference of 3), then 16 (difference of 1), then 19 +(difference of 3). - 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 3). + +In this example, when using every adapter, there are 7 differences of 1 jolt and +5 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 22 +differences of 1 jolt and 10 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. What is the number of 1-jolt differences multiplied by the number of 3-jolt +differences? + + diff --git a/src/day11/input b/src/day11/input @@ -0,0 +1,90 @@ +LLLLL.L.LLLLLLL.LL.LLLLL.LLLLLLL..LLLLL.LLLLL.L..LLLLL..LLLLLLLL.LLLL.LLL.LLLLLLL.LLLLLLLLLL +LLLLLLL.LLLLL..LLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLL.LLLLLL.LL.LLLLLLLLL.LLLLLL.LLLLLLLLLL +LLLL.LLLLLLLL.LLLLLLLLL.LLLLLLLL.LLLLLLLLLLLLLLL.LLLLL.LLLLLLLLL.LLLLLLLLL.LLLLLL.LLLL.LLLLL +LLLLLL.LLLLLLLLLLLLLLLL.LLLLLL.LLLLLLLL.L.LLL.LLLLLLLL.LLLLLL.LLLLLLLLLLLL.LLLLLL.LLLL.LLLLL +LLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLLL.LLL.L.LLLLLLLL.LLLLLLLLL.LLLLLLLL..LLLLLLLLLLLLL.LLL +LLLLLLL.LLLLL.LLLLLLLLL.LLLLLLLL.LLLLLL.LLLLL.LL.LLLLLL.LLLLL.LL.LLLLLLLLL..LLLLLLLLLLLLLLLL +L.....L.....LL.LL.L.L..L............L.L.L.........L...L...LLLL..L.....L.L.L.....L.LLLL.....L +LLLLLLL.LLLLL.LLLLLLLLL.LLLLLLLL..LLLLL.LLLLLLLL.LLLLL.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+LLLLLLL.LLLLL.LLLLLLLLL.LLLLLLLL.LLLLLL.LLLLLLLL.LLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLL.LLLLLLL +LLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLL.LLLLL..LLLLLLLLLLLLLL.LLLLLLLLL.L.LLLLLLLLLLLLLL.LLLLLLLLLL +LLLLLLL.LLLLL.LLLLLLLLL.L.LLL.LL.LLLLLL.LLLLLLLL.LL..LLLLLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLLLLLL +.....LL.....L.L...............L..........LLL................LLLL.....L.L............L..L..LL +LLLLLL..LLL.L.LLLLLLLLLLLLLL.LLL.LL.LLL.LLLLLLLLLLL.LL.LLLLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLLLLL +LLL.LLL.LLLLL.LLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLL.LLL.LLLL.LLLLL.LLLLLLLLLL..LLLLL.LLLLLLLL.L +LLLLLL..LLLLL.LLLL.LLLL.LLLLLLLL.LLLL.L.LLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLLLLL +LLLLLLL.LLLLLLLLL.LLLLL.LLLLLLLL.LLLLLL.LLLLLLLL.LLLLLLLLLLLLLLLLLL..LLLLLLLLLLLL.LLLLLLLLLL +LLLLLLL.LLLLL.LLLLLLLLL.LLLLLLLLLLLLLLL.L.LLLLLL.LLLLL.LLLLLLLLL..LLL.LLLLLLLLLLLLLLLLLLLLLL +.L.L.L.......L...LL....LLL.L.....LL..L.L..LLLL......L.LL......L.L.L.....LL...L....L..L.LL..L +LLLLLLL.L.LLL.LLLLLL.LL.LLLL.LLL.LLLLLL.LLLLLLLL.L..L.L.LLLLLLLL.LLLLLLLLL.LLL..LLLLL.LLLLLL +LLLLLL..LLLLL.LLLLLLLLL.L.LLLLLL.LLLLLL.LLLLLLLL.LLLLL.LLLLLLLLLL..LLLLLLL.LLLLLL.LLLLLLLLLL +LLLLLLLLLLL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LL.LL.LLLLLLLLL.LLLLLLLLLLLLLLL.L.LLLLLL.LLLLL.LLLLLLLLL.LLLLLLL.L.LLLLLLLLLLLLLLLLL diff --git a/src/day11/main.zig b/src/day11/main.zig @@ -0,0 +1,161 @@ +const std = @import("std"); +const aoc = @import("aoc"); + +const SeatState = enum { EMPTY, FLOOR, TAKEN }; +const MapDims = struct { width: usize, height: usize }; +const Dir = struct { x: i8, y: i8 }; +const Pos = struct { x: i128, y: i128 }; + +const adjacent = [_]Dir{ + Dir{ .x = -1, .y = -1 }, + Dir{ .x = -0, .y = -1 }, + Dir{ .x = 1, .y = -1 }, + Dir{ .x = -1, .y = 0 }, + Dir{ .x = 1, .y = 0 }, + Dir{ .x = -1, .y = 1 }, + Dir{ .x = 0, .y = 1 }, + 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"); + var map = std.ArrayList(SeatState).init(allocator); + errdefer map.deinit(); + while (lineit.next()) |line| { + if (dims.width == 0) { + dims.width = line.len; + } else if (dims.width != line.len) { + return aoc.Error.InvalidInput; + } + for (line) |c, i| { + try map.append(switch (c) { + '#' => SeatState.TAKEN, + 'L' => SeatState.EMPTY, + '.' => SeatState.FLOOR, + else => { + return aoc.Error.InvalidInput; + }, + }); + } + dims.height += 1; + } + mapitems.* = map.toOwnedSlice(); +} + +fn printMap(mapitems: []SeatState, dims: MapDims) void { + for (mapitems) |state, i| { + const c: u8 = switch (state) { + SeatState.EMPTY => 'L', + SeatState.TAKEN => '#', + SeatState.FLOOR => '.', + }; + if (i % dims.width == 0) { + std.debug.print("\n", .{}); + } + std.debug.print("{c}", .{c}); + } +} + +fn simulateStrategyOne(before: []SeatState, after: []SeatState, dims: MapDims) u32 { + var seat_changes: u32 = 0; + for (before) |state, i| { + const p = Pos{ .x = i % dims.width, .y = i / dims.width }; + var count: u32 = 0; + for (adjacent) |ap| { + if (p.x + ap.x >= dims.width or p.x + ap.x < 0 or p.y + ap.y >= dims.height or p.y + ap.y < 0) continue; + const ni = @intCast(u64, @intCast(i64, i) + ap.x + ap.y * @intCast(i64, dims.width)); + count += @boolToInt(before[ni] == SeatState.TAKEN); + } + if (state == SeatState.EMPTY and count == 0) { + after[i] = SeatState.TAKEN; + seat_changes += 1; + } else if (state == SeatState.TAKEN and count >= 4) { + after[i] = SeatState.EMPTY; + seat_changes += 1; + } else { + after[i] = before[i]; + } + } + return seat_changes; +} + +fn simulateStrategyTwo(before: []SeatState, after: []SeatState, dims: MapDims) u32 { + var seat_changes: u32 = 0; + for (before) |state, i| { + const p = Pos{ .x = i % dims.width, .y = i / dims.width }; + var count: u32 = 0; + for (adjacent) |ap| { + var iterp = Pos{ .x = p.x + ap.x, .y = p.y + ap.y }; + while (iterp.x < dims.width and iterp.x >= 0 and iterp.y < dims.height and iterp.y >= 0) { + const ni = @intCast(u64, iterp.x + iterp.y * @intCast(i64, dims.width)); + if (before[ni] == SeatState.TAKEN) { + count += 1; + break; + } else if (before[ni] == SeatState.EMPTY) { + break; + } else { + iterp.x += ap.x; + iterp.y += ap.y; + } + } + } + if (state == SeatState.EMPTY and count == 0) { + after[i] = SeatState.TAKEN; + seat_changes += 1; + } else if (state == SeatState.TAKEN and count >= 5) { + after[i] = SeatState.EMPTY; + seat_changes += 1; + } else { + after[i] = before[i]; + } + } + return seat_changes; +} + +fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { + var mapdims = MapDims{ .width = 0, .height = 0 }; + var mapitems: []SeatState = undefined; + try parseMap(&mapitems, &mapdims, input, allocator); + defer allocator.free(mapitems); + + var mapresult = try allocator.alloc(SeatState, mapitems.len); + defer allocator.free(mapresult); + + var round: u32 = 0; + while (simulateStrategyOne(mapitems, mapresult, mapdims) > 0) : (round += 1) { + std.debug.print("\rRound: {}", .{round}); + std.mem.copy(SeatState, mapitems, mapresult); + } + + var taken_count: u64 = 0; + for (mapresult) |state| { + taken_count += @boolToInt(state == SeatState.TAKEN); + } + + std.debug.print("\nSeats Occupied: {}\n", .{taken_count}); +} + +fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { + var mapdims = MapDims{ .width = 0, .height = 0 }; + var mapitems: []SeatState = undefined; + try parseMap(&mapitems, &mapdims, input, allocator); + defer allocator.free(mapitems); + + var mapresult = try allocator.alloc(SeatState, mapitems.len); + defer allocator.free(mapresult); + + var round: u32 = 0; + while (simulateStrategyTwo(mapitems, mapresult, mapdims) > 0) : (round += 1) { + std.debug.print("\rRound: {}", .{round}); + std.mem.copy(SeatState, mapitems, mapresult); + } + + var taken_count: u64 = 0; + for (mapresult) |state| { + taken_count += @boolToInt(state == SeatState.TAKEN); + } + + std.debug.print("\nSeats Occupied: {}\n", .{taken_count}); +} + +pub const main = aoc.gen_main(part1, part2); diff --git a/src/day11/part1 b/src/day11/part1 @@ -0,0 +1,58 @@ +--- Day 11: Seating System --- + +Your plane lands with plenty of time to spare. The final leg of your journey is a ferry that goes +directly to the tropical island where you can finally start your vacation. As you reach the waiting +area to board the ferry, you realize you're so early, nobody else has even arrived yet! + +By modeling the process people use to choose (or abandon) their seat in the waiting area, you're +pretty sure you can predict the best place to sit. You make a quick map of the seat layout (your +puzzle input). + +The seat layout fits neatly on a grid. Each position is either floor (.), an empty seat (L), or an +occupied seat (#). For example, the initial seat layout might look like this: + +L.LL.LL.LL LLLLLLL.LL L.L.L..L.. LLLL.LL.LL L.LL.LL.LL L.LLLLL.LL ..L.L..... LLLLLLLLLL L.LLLLLL.L +L.LLLLL.LL + +Now, you just need to model the people who will be arriving shortly. Fortunately, people are +entirely predictable and always follow a simple set of rules. All decisions are based on the +number of occupied seats adjacent to a given seat (one of the eight positions +immediately up, down, left, right, or diagonal from the seat). The following rules are applied to +every seat simultaneously: + +- If a seat is empty (L) and there are no occupied seats adjacent to it, +the seat becomes occupied. - If a seat is occupied (#) and four +or more seats adjacent to it are also occupied, the seat becomes empty. - +Otherwise, the seat's state does not change. + +Floor (.) never changes; seats don't move, and nobody sits on the floor. + +After one round of these rules, every seat in the example layout becomes occupied: + +#.##.##.## #######.## #.#.#..#.. ####.##.## #.##.##.## #.#####.## ..#.#..... ########## #.######.# +#.#####.## + +After a second round, the seats with four or more occupied adjacent seats become empty again: + +#.LL.L#.## #LLLLLL.L# L.L.L..L.. #LLL.LL.L# #.LL.LL.LL #.LLLL#.## ..L.L..... #LLLLLLLL# #.LLLLLL.L +#.#LLLL.## + +This process continues for three more rounds: + +#.##.L#.## #L###LL.L# L.#.#..#.. #L##.##.L# #.##.LL.LL #.###L#.## ..#.#..... #L######L# #.LL###L.L +#.#L###.## + +#.#L.L#.## #LLL#LL.L# L.L.L..#.. #LLL.##.L# #.LL.LL.LL #.LL#L#.## ..L.L..... #L#LLLL#L# #.LLLLLL.L +#.#L#L#.## + +#.#L.L#.## #LLL#LL.L# L.#.L..#.. #L##.##.L# #.#L.LL.LL #.#L#L#.## ..L.L..... #L#L##L#L# #.LLLLLL.L +#.#L#L#.## + +At this point, something interesting happens: the chaos stabilizes and further applications of these +rules cause no seats to change state! Once people stop moving around, you count 37 +occupied seats. + +Simulate your seating area by applying the seating rules repeatedly until no seats change state. +How many seats end up occupied? + + diff --git a/src/day11/part2 b/src/day11/part2 @@ -0,0 +1,56 @@ +--- Part Two --- + +As soon as people start to arrive, you realize your mistake. People don't just care about adjacent +seats - they care about the first seat they can see in each of those eight directions! + +Now, instead of considering just the eight immediately adjacent seats, consider the first +seat in each of those eight directions. For example, the empty seat below would see +eight occupied seats: + +.......#. ...#..... .#....... ......... ..#L....# ....#.... ......... #........ ...#..... + +The leftmost empty seat below would only see one empty seat, but cannot see any of the +occupied ones: + +............. .L.L.#.#.#.#. ............. + +The empty seat below would see no occupied seats: + +.##.##. #.#.#.# ##...## ...L... ##...## #.#.#.# .##.##. + +Also, people seem to be more tolerant than you expected: it now takes five or more +visible occupied seats for an occupied seat to become empty (rather than four or more +from the previous rules). The other rules still apply: empty seats that see no occupied seats become +occupied, seats matching no rule don't change, and floor never changes. + +Given the same starting layout as above, these new rules cause the seating area to shift around as +follows: + +L.LL.LL.LL LLLLLLL.LL L.L.L..L.. LLLL.LL.LL L.LL.LL.LL L.LLLLL.LL ..L.L..... LLLLLLLLLL L.LLLLLL.L +L.LLLLL.LL + +#.##.##.## #######.## #.#.#..#.. ####.##.## #.##.##.## #.#####.## ..#.#..... ########## #.######.# +#.#####.## + +#.LL.LL.L# #LLLLLL.LL L.L.L..L.. LLLL.LL.LL L.LL.LL.LL L.LLLLL.LL ..L.L..... LLLLLLLLL# #.LLLLLL.L +#.LLLLL.L# + +#.L#.##.L# #L#####.LL L.#.#..#.. ##L#.##.## #.##.#L.## #.#####.#L ..#.#..... LLL####LL# #.L#####.L +#.L####.L# + +#.L#.L#.L# #LLLLLL.LL L.L.L..#.. ##LL.LL.L# L.LL.LL.L# #.LLLLL.LL ..L.L..... LLLLLLLLL# #.LLLLL#.L +#.L#LL#.L# + +#.L#.L#.L# #LLLLLL.LL L.L.L..#.. ##L#.#L.L# L.L#.#L.L# #.L####.LL ..#.#..... LLL###LLL# #.LLLLL#.L +#.L#LL#.L# + +#.L#.L#.L# #LLLLLL.LL L.L.L..#.. ##L#.#L.L# L.L#.LL.L# #.LLLL#.LL ..#.L..... LLL###LLL# #.LLLLL#.L +#.L#LL#.L# + +Again, at this point, people stop shifting around and the seating area reaches equilibrium. Once +this occurs, you count 26 occupied seats. + +Given the new visibility method and the rule change for occupied seats becoming empty, once +equilibrium is reached, how many seats end up occupied? + +