From 89696e54a5fc77d8ddd3359ddf4beea74706b764 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Sat, 11 Dec 2021 07:11:29 +0100 Subject: [PATCH] Added day 11 --- source/app.d | 4 +- source/day11/input.txt | 10 ++++ source/day11/part1.d | 110 ++++++++++++++++++++++++++++++++++++ source/day11/test_input.txt | 10 ++++ 4 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 source/day11/input.txt create mode 100644 source/day11/part1.d create mode 100644 source/day11/test_input.txt diff --git a/source/app.d b/source/app.d index 9466856..153ffa9 100644 --- a/source/app.d +++ b/source/app.d @@ -1,7 +1,7 @@ import std.stdio; -import day10.part1; +import day11.part1; void main() { - syntaxChecker(); + dumboOctopus(); } diff --git a/source/day11/input.txt b/source/day11/input.txt new file mode 100644 index 0000000..df77777 --- /dev/null +++ b/source/day11/input.txt @@ -0,0 +1,10 @@ +4112256372 +3143253712 +4516848631 +3783477137 +3746723582 +5861358884 +4843351774 +2316447621 +6643817745 +6366815868 diff --git a/source/day11/part1.d b/source/day11/part1.d new file mode 100644 index 0000000..589f039 --- /dev/null +++ b/source/day11/part1.d @@ -0,0 +1,110 @@ +module day11.part1; + +import std.file; +import std.stdio; +import std.string; +import std.algorithm; +import std.conv; +import std.array; + +struct Point { size_t r; size_t c; } + +Point[] getAdjacentPoints(ubyte[][] map, Point p) { + size_t size = map.length; + Point[] points = []; + if (p.r > 0 && p.c > 0) points ~= Point(p.r - 1, p.c - 1); + if (p.r > 0) points ~= Point(p.r - 1, p.c); + if (p.c > 0) points ~= Point(p.r, p.c - 1); + if (p.r > 0 && p.c + 1 < size) points ~= Point(p.r - 1, p.c + 1); + if (p.c + 1 < size) points ~= Point(p.r, p.c + 1); + if (p.r + 1 < size) points ~= Point(p.r + 1, p.c); + if (p.r + 1 < size && p.c > 0) points ~= Point(p.r + 1, p.c - 1); + if (p.r + 1 < size && p.c + 1 < size) points ~= Point(p.r + 1, p.c + 1); + return points; +} + +uint flash(ref ubyte[][] map, size_t row, size_t col, ref bool[][] flashed) { + Point[] neighbors = getAdjacentPoints(map, Point(row, col)); + uint flashes = 1; + flashed[row][col] = true; + foreach (n; neighbors) { + if (!flashed[n.r][n.c]) { + map[n.r][n.c]++; + if (map[n.r][n.c] > 9) { + flashes += flash(map, n.r, n.c, flashed); + } + } + } + return flashes; +} + +uint doStepPart1(ref ubyte[][] map) { + bool[][] flashed; + flashed.length = map.length; + foreach (r, row; map) { + flashed[r].length = map.length; + foreach (c, e; row) { + map[r][c]++; + } + } + uint flashes = 0; + foreach (r, row; map) { + foreach (c, e; row) { + if (e > 9 && !flashed[r][c]) { + flashes += flash(map, r, c, flashed); + } + } + } + bool allFlashed = true; + foreach (r, row; map) { + foreach (c, e; row) { + if (flashed[r][c]) { + map[r][c] = 0; + } else { + allFlashed = false; + } + } + } + return flashes; +} + +uint doStepPart2(ref ubyte[][] map) { + bool[][] flashed; + flashed.length = map.length; + foreach (r, row; map) { + flashed[r].length = map.length; + foreach (c, e; row) { + map[r][c]++; + } + } + uint flashes = 0; + foreach (r, row; map) { + foreach (c, e; row) { + if (e > 9 && !flashed[r][c]) { + flashes += flash(map, r, c, flashed); + } + } + } + foreach (r, row; map) { + foreach (c, e; row) { + if (flashed[r][c]) { + map[r][c] = 0; + } + } + } + return flashes == (map.length * map.length); +} + +void dumboOctopus() { + ubyte[][] map = readText("source/day11/input.txt").strip.split("\n") + .map!(s => s.strip.map!(c => cast(ubyte)(c.to!ubyte - '0')).array) + .array; + ulong i = 0; + while (true) { + if (doStepPart2(map)) { + writefln("All flashed at: %d", i + 1); + break; + } + i++; + } +} \ No newline at end of file diff --git a/source/day11/test_input.txt b/source/day11/test_input.txt new file mode 100644 index 0000000..03743f6 --- /dev/null +++ b/source/day11/test_input.txt @@ -0,0 +1,10 @@ +5483143223 +2745854711 +5264556173 +6141336146 +6357385478 +4167524645 +2176841721 +6882881134 +4846848554 +5283751526