From da60757f0ec508a8e59b2960198d31d32356e4e5 Mon Sep 17 00:00:00 2001 From: Henk Kalkwater Date: Fri, 11 Dec 2020 08:29:43 +0100 Subject: [PATCH] Add day 11 --- in/11.txt | 92 +++++++++++++++++++++++++ source/app.d | 2 + source/day10.d | 1 - source/day11.d | 183 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 277 insertions(+), 1 deletion(-) create mode 100644 in/11.txt create mode 100644 source/day11.d diff --git a/in/11.txt b/in/11.txt new file mode 100644 index 0000000..a6f636f --- /dev/null +++ b/in/11.txt @@ -0,0 +1,92 @@ +LLLLL.LLL.LLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLLLLLLL.LLLLLLL.LLLLLLL.LLLLL.LLLLLLLLLLLLLLLLLLL.LLLLL +LLLLLLLLL.LLLLLLLLLLLLLLL.LLLLL.LLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLL.LLLLL.LLLLLL.L.LLLL.LLL.L.LLLLL +LLLLLLLLL.LLLLLL.L.LLLLLL.LLLLLLLLL.LLLL...LLLLLL.LLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLLLLL.LLLLL.LLLLL +LLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLLLLLL.L.LLLLLL.LLLLLLLLLLLLLLLL +LLLLLLLLLLLLLLL..LLLLLL.LLLLLLLLLLLLLLLLL..LLLLLL.LLLLLLL.LLLLLLL.LLLLL.LLLLLLLL.LLLL.LLLLLLLLLLL +LLLLLLLLLLLLLLLL.LLLLLLLL.LLLLLLLLL.LLLLL.LLLLLLL.LL.LLLL.LLLLLLL.LLLLL.LLLLLLLLLLLLL.LLLLLLLLLLL +LLLLLLLLLLLLLLLL.LLLLLLLL.LLLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLL..LLLLLLLLLLLLL.LLLL.LLLLLLLLLLL +LLLLLLLLL.LLLLLLL.L.LLLLL.LLLLLLLLL.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LLLLLLLLL.LLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLL.LLLLL +....L...L.....L..L...LL.L....L......L.....L..LL...L.......L....L.....L.L..L...L..........LL.LL... +LLLLL.LLL.LLLLLL.LLL.LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLL..LLLLL.LLLLLLLL.LLLLLLLLLLLLLLLL +LLLLLLLLL.LLLLLL.LLLLLLLL.LLLLLLLLLLLLLLL.LLLLLLL.LLLL.LL.LLLLLLL..LLLLLLLLLLLLL.LLLLL.LLLL.LLLLL +LLLLLLLLL.L.LLLLLLLLLLLLL.LLLLLLLLL.L..LLLLLLLLLL.LLLLLLLLLLLLL.L.LLLLLLLLLLLLLL.LLLL.LLLLL.LLLLL +LLLL.LL.L.LLLLLL.LLLLLLLL.LLLL.LLLL..LLLL.LLLLLLL..LLLLLLLLLLLLLL.LLLLLLLLLLLLLL.LLLL.LLLLLLLLLLL +LLLLLL.LLLLLLLLLLLLLL.LLL.LLLLLLLLLLLLLLL.LLLLLLLLLLLLLLL.LLLLLLL.LLLLL.LLLLLLLLLLLLLLLLLLLLLLLLL +L.LLLLLLLLLLLLLL.LLLLL.LL.LLLLLLLLLLLLLLLLLLLLLLL.LLLLLLL.LLLL.LL.LLLLLLLLLLLLLLLLLLL.LLLLL.LLLLL +LLLLLLLL..LLLLLL.LLLLLLLLLLLLLLLL.L.LLLLL.LLLLLLLLLLLLLLL.LLLLLLL..LLLLLLLLLLLLL.LLLL.LLLLL.LLLLL +L...LLL.L..LL..L.LLL.....L......L...LLL..L....LLL..L...L...........L..L..L...LL.LL.L...LL.LLLLLL. +.LLLLLLLLLLLLLLL.LLLLLLLL.LLLLLLLLL.LLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLL.LLLL.LLLLL.LLLLL +LLLLLLLLL.LLLLLL.LLLLLLLL.LLLLLLLLLLLLL....LLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLL.LLL.LLLL.LLL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LLL +LLLL.LLLLLLLLLLL.LLLLLLLL.LL.LLLLL..LLLLL.LLLLLLL.L.LLLLL.LLLLLLL.LLLLL.LL.LLLLL.LLLLLLLLLL..LLLL +LLLLLLLLL.LLLLLL.LLLLLLLL.LLLLLL.LL.LLLLL.LLLLLLLLLLLLLLL.LLLLLLL...LLL.LLLLLLLL.LLLL.LLLLLLLLLLL +LLLLLLLLL.LLLLL.LLLLLLLLL.LLLLLLLLL.LLLLL.L.LLLLL.LLLLLLLLLLLLLLL.LLLLLLLLLLLL.L.LLLL.LLLLL.LLLLL +LLLL.LLLL.LLLLLL.LLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLL.LLLLLLL.L.LLLLLLLLLLLLLLLLLLLL.LLLL..LLLL.LLLLL +LLLLLLLLL.LLLLLL.LLLLLLLL.LLL.LLLLL.LLLLLLLLLLLLL.LLLLLLLLLLLLLLL.LLLLL.LLLLLLLLLLLLLLLLLLL.LL.LL +LLLLLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLL.LLLL.LLLLLLL.LLLLLLL.LLLLL.LLLLLLLL.LLLL.LLLLLL.LLLL +LLLLLLLLLLLLLLLL.LLLLLLL..LLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLL.L..LLLL.L.LLLLL..LLLL.LLLLL.LLLLL diff --git a/source/app.d b/source/app.d index bb2fd72..3e537c5 100644 --- a/source/app.d +++ b/source/app.d @@ -18,6 +18,7 @@ import day7; import day8; import day9; import day10; +import day11; import dayutil; immutable string progName = "aoc-2020"; @@ -37,6 +38,7 @@ Variant function(int, File, bool, string[])[] programs = [ &day8.run, &day9.run, &day10.run, + &day11.run, ]; void printUsage(string message = null) { diff --git a/source/day10.d b/source/day10.d index edd0145..2ed4831 100644 --- a/source/day10.d +++ b/source/day10.d @@ -27,7 +27,6 @@ size_t part1(T)(SortedRange!(T[]) numbers) { } // Device adapter is always rated 3 volts higher gap3++; - writeln(gap1, ", ", gap3); return gap1 * gap3; } diff --git a/source/day11.d b/source/day11.d new file mode 100644 index 0000000..1b58f9b --- /dev/null +++ b/source/day11.d @@ -0,0 +1,183 @@ +import std.array; +import std.algorithm; +import std.range; +import std.string; +import std.stdio; +import std.traits; +import std.variant; + +import dayutil; + +enum Spot { + FLOOR, + EMPTY, + TAKEN +} + +Variant run(int part, File input, bool bigboy, string[] args) { + Spot[][] spots = parseInput(input.byLineCopy); + + return Variant(parts!size_t(part, + () => part1(spots), + () => part2(spots))); +} + +Spot[][] parseInput(Range)(Range range) if (isInputRange!Range && isSomeString!(ElementType!Range)) { + return range.map!(row => row.map!((spot) { + switch(spot) { + case '.': + return Spot.FLOOR; + case 'L': + return Spot.EMPTY; + case '#': + return Spot.TAKEN; + default: + assert(false, "Unknown type"); + } + }).array).array; +} + +size_t part1(Spot[][] input) { + Spot[][] prev = input.dup; + do { + foreach (i, row; input.enumerate) { + prev[i] = row.dup; + } + //prev = input.dup; + for (int y = 0; y < input.length; y++) { + for (int x = 0; x < input[y].length; x++) { + if(input[y][x] == Spot.FLOOR) continue; + + uint adjacent_taken = 0; + for (int dy = max(0, y - 1); dy <= min(prev.length - 1, y + 1); dy++) { + for (int dx = max(0, x - 1); dx <= min(prev[y].length - 1, x + 1); dx++) { + if (dx == x && dy == y) continue; + switch(prev[dy][dx]) { + case Spot.TAKEN: + adjacent_taken++; + break; + default: + break; + } + } + } + import std.format; + if (adjacent_taken == 0) { + input[y][x] = Spot.TAKEN; + } else if (adjacent_taken >= 4) { + input[y][x] = Spot.EMPTY; + } + } + } + + } while(!prev.equal(input)); + + return input.joiner.count(Spot.TAKEN); +} + + +unittest { + import std.format; + string layout = q"EOS +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 +EOS"; + size_t result = parseInput(layout.lineSplitter).part1(); + assert(result == 37, "Result is %d (expected 37)".format(result)); +} + +size_t part2(Spot[][] input) { + Spot[][] prev = input.dup; + do { + foreach (i, row; input.enumerate) { + prev[i] = row.dup; + } + //prev = input.dup; + for (int y = 0; y < prev.length; y++) { + for (int x = 0; x < prev[y].length; x++) { + if(prev[y][x] == Spot.FLOOR) { + //debug write('.'); + continue; + } + import std.format; + + int adjacent_taken = 0; + for (int dy = -1; dy <= 1; dy++) { + for (int dx = -1; dx <= 1; dx++) { + if (dx == 0 && dy == 0) continue; + + Spot spot; + int i = 1; + do { + int nx = x + i * dx; + int ny = y + i * dy; + i++; + if (ny < 0 || ny >= prev.length || nx < 0 || nx >= prev[ny].length) { + //debug write("\x1B[41m"); + spot = Spot.EMPTY; + break; + } + + spot = prev[ny][nx]; + } while(spot == Spot.FLOOR); + + if (spot == Spot.TAKEN) { + adjacent_taken++; + } + } + } + + //debug write(adjacent_taken); + //debug write("\x1B[0m"); + if (adjacent_taken == 0) { + input[y][x] = Spot.TAKEN; + } else if (adjacent_taken >= 5) { + input[y][x] = Spot.EMPTY; + } + } + //debug writeln(); + } + //debug writeln(); + + /+debug { + void printSpots(Spot[][] flop) { + foreach(row; flop) { + foreach(seat; row) { + write(seat == Spot.TAKEN ? '#' : (seat == Spot.EMPTY ? 'L' : '.')); + } + writeln(); + } + } + printSpots(input); + }+/ + } while(!prev.equal(input)); + + return input.joiner.count(Spot.TAKEN); +} + +unittest { + import std.format; + string layout = q"EOS +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 +EOS"; + size_t result = parseInput(layout.lineSplitter).part2(); + assert(result == 26, "Result is %d (expected 26)".format(result)); +} +