diff options
-rw-r--r-- | aoc/2021/09/part-one.zig | 33 | ||||
-rw-r--r-- | aoc/2021/09/part-two.py | 20 | ||||
-rwxr-xr-x | others/other/nuoc.py | 2 |
3 files changed, 54 insertions, 1 deletions
diff --git a/aoc/2021/09/part-one.zig b/aoc/2021/09/part-one.zig new file mode 100644 index 0000000..8e3ae15 --- /dev/null +++ b/aoc/2021/09/part-one.zig @@ -0,0 +1,33 @@ +const print = std.debug.print; +const std = @import("std"); + +const input = @embedFile("input"); + +inline fn get(x: i8, y: i8) u8 { + return switch (x) { + 0...99 => switch (y) { + 0...99 => input[@intCast(usize, x) + @intCast(usize, y) * 101], + else => ':', + }, + else => ':', + }; +} + +pub fn main() void { + var sum: usize = 0; + defer print("{}\n", .{ sum }); + + var row: i8 = 0; + while (row < 100) : (row += 1) { + var col: i8 = 0; + while (col < 100) : (col += 1) { + const height = get(col, row); + sum += @boolToInt(@reduce(.And, @Vector(4, u8){ + get(col + 1, row), + get(col, row + 1), + get(col - 1, row), + get(col, row - 1), + } > @splat(4, height))) * (height - '/'); + } + } +} diff --git a/aoc/2021/09/part-two.py b/aoc/2021/09/part-two.py new file mode 100644 index 0000000..99524c4 --- /dev/null +++ b/aoc/2021/09/part-two.py @@ -0,0 +1,20 @@ +from collections import deque +from functools import reduce +from operator import mul +from sys import stdin + +heightmap = [[int(c) for c in s] for s in stdin.read().strip().split()] +get = lambda y, x: 10 if min(x, y) < 0 or max(x, y) > 99 else heightmap[y][x] +adjacent = lambda y, x: ((y, x + 1), (y + 1, x), (y, x - 1), (y - 1, x)) +basins = {(i, j): 0 for i, row in enumerate(heightmap) + for j, height in enumerate(row) + if all(height < get(y, x) for y, x in adjacent(i, j))} +queue, visited = deque((key, key) for key in basins), set() +while queue: + key, pos = queue.popleft() + if pos in visited: continue + basins[key] += 1 + visited.add(pos) + for adj in adjacent(*pos): + if get(*adj) < 9 and adj not in visited: queue.append((key, adj)) +print(reduce(mul, sorted(basins.values(), reverse=True)[:3])) diff --git a/others/other/nuoc.py b/others/other/nuoc.py index 9fcff54..4d53861 100755 --- a/others/other/nuoc.py +++ b/others/other/nuoc.py @@ -17,7 +17,7 @@ visited = ([[True] * n] result = 0 while queue: - h, i, j = heappop(queue) + h, i, j = heappop(queue) for x, y in (i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1): if 0 <= x < m and 0 <= y < n and not visited[x][y]: result += max(0, h - height[x][y]) |