-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday03.nim
46 lines (36 loc) · 999 Bytes
/
day03.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
import strscans
let input = "./inputs/03.txt"
const size = 1000
type
Claim = tuple
x, y: int16
w, h: int8
Fabric = array[size, array[size, int8]]
template forGrid(start1, size1, start2, size2: int, body: untyped) =
for i {.inject.} in start1 ..< start1 + size1:
for j {.inject.} in start2 ..< start2 + size2:
body
template ifOverlappingFabric(start1, size1, start2, size2: int, body: untyped) =
forGrid(start1, size1, start2, size2):
if fabric[i][j] > 1:
body
var
fabric: Fabric
claims: seq[Claim]
for line in input.lines:
var id, x, y, w, h: int
if line.scanf("#$i @ $i,$i: $ix$i", id, x, y, w, h):
forGrid(y, h, x, w):
inc fabric[i][j]
claims.add (x.int16, y.int16, w.int8, h.int8)
proc first(): int =
ifOverlappingFabric(0, size, 0, size):
inc result
proc second(): int =
for i, c in claims:
block outer:
ifOverlappingFabric(c.y, c.h, c.x, c.w):
break outer
return i+1
echo first()
echo second()