From 7be570c4a6e86fb7060f0bc06910ca57003dfe90 Mon Sep 17 00:00:00 2001 From: Orfeas <38209077+0xfea5@users.noreply.github.com> Date: Sat, 8 Jun 2024 13:50:47 +0300 Subject: Update file names and init.sh --- day08/solution.nim | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 day08/solution.nim (limited to 'day08/solution.nim') diff --git a/day08/solution.nim b/day08/solution.nim new file mode 100644 index 0000000..f2c4f23 --- /dev/null +++ b/day08/solution.nim @@ -0,0 +1,85 @@ +import std/strutils +import std/strformat +import std/sequtils +import std/algorithm +import std/sugar + +type + Tree = tuple + height: int + found: bool + +proc scanTree(tree: Tree, answer: var int, maxHeight: var int): Tree = + result = tree + if tree.height > maxHeight: + maxHeight = tree.height + if not tree.found: + answer += 1 + result = (tree.height, true) + +proc calculate(row: var seq[Tree], reved = false): int = + var + answer = 0 + maxHeight = -1 + newRow: seq[Tree] + + if reved: + newRow = row.reversed() + else: + newRow = row + + for i, r in newRow: + newRow[i] = scanTree(r, answer, maxHeight) + row = newRow + + return answer + +func part1(content: seq[string]): int = + var grid = map( + content, + (row) => zip(map(row, (c) => ord(c) - ord('0')), newSeq[bool](row.len())) + ) + var answer = 0 + + for row in grid.mitems(): + answer += calculate(row) + answer += calculate(row, true) + + for i in 0 ..< grid.len(): + var col = map(grid, (row) => row[i]) + answer += calculate(col) + answer += calculate(col, true) + + return answer + +func checkVisibility(line: seq[int], height: int): int = + result = 0 + for h in line: + result += 1 + if h >= height: + return result + + return result + +proc part2(content: seq[string]): int = + let grid = map(content, (row) => map(row, (c) => ord(c) - ord('0'))) + result = 0 + + for i, row in grid: + for j, h in row: + var col = map(grid, (row) => row[j]) + var count = + checkVisibility(row[j+1 .. ^1], h) * + checkVisibility(row[0 .. j-1].reversed(), h) * + checkVisibility(col[i+1 .. ^1], h) * + checkVisibility(col[0 .. i-1].reversed(), h) + + # echo fmt"{i}, {j} = {count}" + result = max(result, count) + + return result + +let content = readFile("./input.txt").strip().splitLines() + +echo part1(content) +echo part2(content) -- cgit v1.2.3