-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08_1.nim
123 lines (96 loc) · 3.2 KB
/
day08_1.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import os
if paramCount() != 1:
echo "Usage: ./dayXX <input file>"
quit(1)
let filename = paramStr(1)
if not fileExists(filename):
echo "File not found: ", filename
quit(1)
## RUN: TEST
# RUN: FULL
import std/sequtils
type
Matrix[T: untyped, W, H:int] = seq[seq[T]]
proc newMatrix[T](W, H: int): Matrix[T, W, H] =
result.newSeq(H)
for i in 0 ..< H:
result[i].newSeq(W)
proc rows[T: untyped, W, H:int](m: Matrix[T, W, H]): int = result = len(m)
proc columns[T: untyped, W, H:int](m: Matrix[T, W, H]): int = result = len(m[0])
proc `[]`[T: untyped, W, H:int](m: Matrix[T, W, H], x, y: int): T =
m[y][x]
proc `[]=`[T: untyped, W, H:int](m: var Matrix[T, W, H], x, y: int, value: T) =
m[y][x] = value
proc parseInt(c: char): uint8 =
assert(c in '0' .. '9')
uint8(c.ord - 48)
var width = 0
var height = 0
var all_lines: seq[string]
for line in lines(filename):
if len(all_lines) == 0:
width = line.len
else:
assert(line.len == width)
all_lines.add(line)
height += 1
var forest = newMatrix[uint8](width, height)
for hi, line in all_lines:
for wi, character in line:
forest[wi, hi] = character.parseInt
var visible = newMatrix[bool](width, height)
type
Direction = enum
up, down, left, right
# https://stackoverflow.com/a/35697846/2531987
template toClosure(it): auto =
iterator j: type(it) =
for i in it:
yield i
j
proc visibilityCheck[W, H](forest: Matrix[uint8, W, H], visible: var Matrix[bool, W, H], direction: Direction) =
case direction:
of up, down:
let counter = if direction == up:
toClosure(countdown(forest.rows-1, 0))
else:
toClosure(countup(0, forest.rows-1))
let first = if direction == up: forest.rows-1 else: 0
var highest: seq[uint8] = newSeq[uint8](forest.columns)
for hi in counter:
let row = forest[hi]
for wi, tree in row:
if hi == first or tree > highest[wi]:
visible[wi, hi] = true
highest[wi] = tree
of left, right:
let counter = if direction == left:
toClosure(countdown(forest.columns-1, 0))
else:
toClosure(countup(0, forest.columns-1))
let first = if direction == left: forest.columns-1 else: 0
var highest: seq[uint8] = newSeq[uint8](forest.rows)
for wi in counter:
for hi, row in forest:
let tree = row[wi]
if wi == first or tree > highest[hi]:
visible[wi, hi] = true
highest[hi] = tree
for direction in [up, down, left, right]:
visibilityCheck(forest, visible, direction)
# const PRINT = true
const PRINT = false
proc purple(s: string): string = "\e[1;34m" & s & "\e[0m"
proc darkgray(s: string): string = "\e[0;30m" & s & "\e[0m"
var total = 0
for hi, row in forest:
for wi, tree in row:
let ts = $tree
if not visible[wi, hi]:
if PRINT: write(stdout, ts.purple)
else:
# total += int(tree)
total += 1
if PRINT: write(stdout, ts.darkgray)
if PRINT: write(stdout, "\n")
echo total