diff options
Diffstat (limited to 'src/11')
| -rw-r--r-- | src/11/input | 90 | ||||
| -rw-r--r-- | src/11/main.zig | 169 | ||||
| -rw-r--r-- | src/11/part1 | 58 | ||||
| -rw-r--r-- | src/11/part2 | 56 |
4 files changed, 373 insertions, 0 deletions
diff --git a/src/11/input b/src/11/input new file mode 100644 index 0000000..980d6e0 --- /dev/null +++ b/src/11/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.LLL.LLLLL.LLLLLLLLL.LLLLLLLLLL.LLLLLL +.LLLLLLLLLLLL.LLLLLLLLLLLLLLLLLL.LLLL.LLLLLLLLL.LLLLLL.LLL.LLLLL.LLLLLLLLL.LLLLLL.LLLLL.L.LL +LLLL.LL.LLLLLLLLL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.LLLLL.LLLLLLLLL.LLLLLLLL.LLLLLL.LLLLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLLLLL +....L...L.....L.....LLLL......L.L......L........LL...L....LLLL....LL......LL...........L.L.L +LLLLLLL.LLLLLLLLLLLL.LL.L.LL.LLLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLL.LL.LL..LLLLLLLLLL +LLLL.LL.L.LLL.LLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLLLL.L.LLL.LLLLLLLLL.LLLLLLLLL.LLLLLL.LLLLL.LLLL +LLLLLLL.LLLLL..LLLLLLLL.LLLLLLLL.LLLLL..LLLLL.LLL.LLLLLLLLLLLLLL.LLLLLLLLL.LLLL.LLLLLLLLLLLL +LLLLLLL.LLLLLLLLLLLLLLL.LLLLLLLL.LLLLL...LLLLLLLLLL.LL.LLLLLLLLLLLLLLLLL.L..LLLLL.LLLLLLLLLL +LLLLLLL.LLLLLLLLLLLLLLL..LLL.LL..LLL....LLLLLLLL.LLLLL.LLLLLLLLLLLLL.LLLLL.LLLLLL.LLLLL.LLLL +LLLLLLL.LLLLLLLL.LLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLLLLL..LLLLLLL.LLLLLL.LLLLLLLLLL +LL.LLLLLLLLLLLLLLLLLLLL.LLLLLLLL.LLLLLL.LLLLLLLL.LLLLL.LLLLLLL.L.LLLLLLLLLLLLLLLLLLLLLLLLLLL +L.LLLLLLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLLLLLLLL.LLL.LLLLL.LLLL.LLLL.LLLLLLLLL.LL.LL..LLLLLLLLLL +LL......L.....LL....L.L..L.L.L..L..L......LL...L.L.L.LL..LLLL.LLL....L..L...L...L..L.LLL..L. +LLLLLL.LL.LLLLLLLLLLLLL.LLLLLLLL.LLLLLL.LLLLLLLL.LLL.LLLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLLLLL +LLLLLLLLLL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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 +LLLLLLLLLLLL..LLLLLLLLL.L.LLLLLLL.LLLLL.LLLL.LLL.LLLLL.LLLLLLLLL.LLLLLLLLL.LLLLLL.LLLLLLLLLL +LLLLLLL.LLLLL.LLLLLLL.L.LLLLLLLLLLLLLLLLLLLLLLLL.LLLLL.LLLLLLLLL.LL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LLL.L...L.LL..LLL............LL....LL.LL..L..L...L...L...L...LL. +LL.LLLL.LLLLLL.LLLLLL.L.LLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLLLLL.LLLLLLLLLLLLLLL.L +LLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLLLL.LLLLL.LLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLLLL. +LLLLLLL.LLLLL.LLLLLLLLL.LLLLL.L..LLLLLLL.LLLLLL..LLLLL.LLLLLLLLL.LL.LLLLLL.LL.LL.LLLLLLLLLLL +LLL.LLL.LLLLL.LLL.LLLLL.LLLLLLL.LLLLLLL.L.LLLLLL.LLLLLLLLLL.LLLL.LLLLLLLLLL.LLLLLLLLLLLLLLLL +LLLLLLL.LLLLL..L.LLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLLL.LLLLL..LLLLLLLLLL +LLLLLLL.LLLLLLLLLLLLLLL..L.LLL.L.LLLLLL.LLLLLLLLLLL.LLLLLLL.LLLLLLLLLLLLLL.LLLLLL.LLLLLLLLLL +LLLLLLL.LLLLLLLLLLLLLLLLLL.LLLLL.LLLLLL.LLL.L.LLLLLLLL.LLLLLLL.L..LLLLLLLL.LLLL.L.LLLLLLLLLL +LLLLLLLLLLLLL.L.LLLLLL.LLLLLLLLL.LLLLLL.LLLLL.LLLLLLLL.LLLLLLLLLLLLLLLLLLL.LLLLLL.LLLL.LLLL. +LLLLLLLLLLLLLLL.LLLL.LLLLLLLLLLLLLLLLLL.LLLLLLLL.LLLLL.LLLLLLLLLLLLL.LLLLL.LLLLLL..LLLLLLLLL +LLLLLLL.LL.LL.LLLLLLLLL.LLLLLLLLLLLLLLL.L.LLLLLL.LLLLL.LLLLLLLLL.LLLLLLL.L.LLLLLLLLLLLLLLLLL diff --git a/src/11/main.zig b/src/11/main.zig new file mode 100644 index 0000000..857c457 --- /dev/null +++ b/src/11/main.zig @@ -0,0 +1,169 @@ +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(u8, 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| { + 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) { + aoc.debugfmt("\n", .{}); + } + aoc.debugfmt("{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) !?[]u8 { + _ = args; + + 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) { + aoc.debugfmt("\rRound: {}", .{round}); + std.mem.copy(SeatState, mapitems, mapresult); + } + + var taken_count: u64 = 0; + for (mapresult) |state| { + taken_count += @boolToInt(state == SeatState.TAKEN); + } + + aoc.debugfmt("\nSeats Occupied: {}\n", .{taken_count}); + + return try std.fmt.allocPrint(allocator, "{d}", .{taken_count}); +} + +fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + + 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) { + aoc.debugfmt("\rRound: {}", .{round}); + std.mem.copy(SeatState, mapitems, mapresult); + } + + var taken_count: u64 = 0; + for (mapresult) |state| { + taken_count += @boolToInt(state == SeatState.TAKEN); + } + + aoc.debugfmt("\nSeats Occupied: {}\n", .{taken_count}); + + return try std.fmt.allocPrint(allocator, "{d}", .{taken_count}); +} + +pub const main = aoc.main(part1, part2, .{ "2126", "1914" }); diff --git a/src/11/part1 b/src/11/part1 new file mode 100644 index 0000000..1ad3ef1 --- /dev/null +++ b/src/11/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 +[1m[37mnumber of occupied seats[0m 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 [1m[37mempty[0m (L) and there are [1m[37mno[0m occupied seats adjacent to it, +the seat becomes [1m[37moccupied[0m. - If a seat is [1m[37moccupied[0m (#) and [1m[37mfour +or more[0m seats adjacent to it are also occupied, the seat becomes [1m[37mempty[0m. - +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 [1m[37m37[0m +occupied seats. + +Simulate your seating area by applying the seating rules repeatedly until no seats change state. +[1m[37mHow many seats end up occupied?[0m + + diff --git a/src/11/part2 b/src/11/part2 new file mode 100644 index 0000000..0aa31f5 --- /dev/null +++ b/src/11/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 [1m[37mthe first seat they can see[0m in each of those eight directions! + +Now, instead of considering just the eight immediately adjacent seats, consider the [1m[37mfirst +seat[0m in each of those eight directions. For example, the empty seat below would see +[1m[37meight[0m occupied seats: + +.......#. ...#..... .#....... ......... ..#L....# ....#.... ......... #........ ...#..... + +The leftmost empty seat below would only see [1m[37mone[0m empty seat, but cannot see any of the +occupied ones: + +............. .L.L.#.#.#.#. ............. + +The empty seat below would see [1m[37mno[0m occupied seats: + +.##.##. #.#.#.# ##...## ...L... ##...## #.#.#.# .##.##. + +Also, people seem to be more tolerant than you expected: it now takes [1m[37mfive or more[0m +visible occupied seats for an occupied seat to become empty (rather than [1m[37mfour or more[0m +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 [1m[37m26[0m occupied seats. + +Given the new visibility method and the rule change for occupied seats becoming empty, once +equilibrium is reached, [1m[37mhow many seats end up occupied?[0m + + |
