From df3ee0e171b6402c1fa407ea090fe7d2ff2563eb Mon Sep 17 00:00:00 2001 From: ptrcnull Date: Sun, 12 Dec 2021 10:58:43 +0100 Subject: [PATCH] 11: part1+2 --- 11/input | 10 +++++ 11/solve.zig | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++ 11/testinput | 10 +++++ 3 files changed, 135 insertions(+) create mode 100644 11/input create mode 100644 11/solve.zig create mode 100644 11/testinput diff --git a/11/input b/11/input new file mode 100644 index 0000000..ed7ceb3 --- /dev/null +++ b/11/input @@ -0,0 +1,10 @@ +2138862165 +2726378448 +3235172758 +6281242643 +4256223158 +1112268142 +1162836182 +1543525861 +1882656326 +8844263151 diff --git a/11/solve.zig b/11/solve.zig new file mode 100644 index 0000000..6da1acc --- /dev/null +++ b/11/solve.zig @@ -0,0 +1,115 @@ +const std = @import("std"); +const print = std.debug.print; + +const matrixSize = 10; +const iterations = std.math.maxInt(u32); +// const iterations = 100; +// const iterations = 10; + +const Point = [2]usize; + +const NeighborList = struct { + fn init(alloc: *std.mem.Allocator, x: usize, y: usize) std.ArrayList(Point) { + var list = std.ArrayList(Point).init(alloc); + + for ([_]i8{-1, 0, 1}) |a| { + var _x = @intCast(i8, x & 0xf) + a; + if (_x < 0 or _x >= matrixSize) continue; + + for ([_]i8{-1, 0, 1}) |b| { + var _y = @intCast(i8, y & 0xf) + b; + if (_y < 0 or _y >= matrixSize) continue; + if (a == 0 and b == 0) continue; + + list.append(Point{@intCast(usize, _x), @intCast(usize, _y)}) catch {}; + } + } + + return list; + } +}; + +pub fn main() !void { + var alloc = std.heap.GeneralPurposeAllocator(.{}){}; + defer std.debug.assert(!alloc.deinit()); + var gpa = &alloc.allocator; + + const stdin = std.io.getStdIn().reader(); + + var flashes: u32 = 0; + var fullFlashStep: u32 = 0; + + var matrix: [matrixSize][matrixSize]u8 = undefined; + + // parse data + { + var tmp: [256]u8 = undefined; + var i: u8 = 0; + while (try stdin.readUntilDelimiterOrEof(&tmp, '\n')) |line| { + for (line) |char, j| { + matrix[i][j] = char - 0x30; + } + + i += 1; + } + } + + var i: u32 = 0; + while (i < iterations) : (i += 1) { + // print("iteration {}\n", .{i}); + + var flashed: [matrixSize][matrixSize]bool = undefined; + + for (matrix) |row, x| { + for (row) |_, y| { + matrix[x][y] += 1; + } + } + + while (true) { + var retry = false; + + for (matrix) |row, x| { + for (row) |tile, y| { + if (matrix[x][y] > 9 and !flashed[x][y]) { + flashed[x][y] = true; + retry = true; + + if (i < 100) flashes += 1; + + var neigh = NeighborList.init(gpa, x, y); + defer neigh.deinit(); + + for (neigh.items) |point| { + matrix[point[0]][point[1]] += 1; + } + } + } + } + + if (!retry) break; + } + + var numFlashed: u8 = 0; + for (flashed) |row, x| { + for (row) |hasFlashed, y| { + if (hasFlashed) { + matrix[x][y] = 0; + numFlashed += 1; + } + } + } + + if (numFlashed == comptime matrixSize * matrixSize) { + fullFlashStep = i + 1; + break; + } + + // for (matrix) |row| { + // print("{any}\n", .{row}); + // } + } + + print("{}\n", .{flashes}); + print("{}\n", .{fullFlashStep}); +} diff --git a/11/testinput b/11/testinput new file mode 100644 index 0000000..a3819c9 --- /dev/null +++ b/11/testinput @@ -0,0 +1,10 @@ +5483143223 +2745854711 +5264556173 +6141336146 +6357385478 +4167524645 +2176841721 +6882881134 +4846848554 +5283751526 \ No newline at end of file