-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.nim
60 lines (45 loc) · 1.04 KB
/
solution.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
import std/[
algorithm,
bitops,
deques,
json,
math,
re,
sequtils,
sets,
strformat,
strutils,
tables,
sugar,
]
import ../day10/solution
import ../../lib/grid
proc toBin(h: string): string =
h.toSeq.mapIt(fromHex[int]($it).toBin(4)).join
when defined(test):
let input = "flqrgnkx"
block:
doAssert hash(&"{input}-0").toBin[0 ..< 8] == "11010100"
proc genGrid(key: string): seq[string] =
for r in 0 ..< 128:
result.add hash(&"{key}-{r}").toBin
proc part1(input: string): int =
for b in genGrid(input):
result += b.toSeq.countIt(it == '1')
when defined(test):
block:
doAssert part1(input) == 8108
proc part2(input: string): int =
var grid = genGrid(input).mapIt(it.toSeq)
for r in 0 ..< grid.len:
for c in 0 ..< grid[r].len:
if grid[r][c] == '0': continue
result += 1
floodfill(grid, r, c, '1', '0')
when defined(test):
block:
doAssert part2(input) == 1242
when isMainModule and not defined(test):
let input = readAll(stdin).strip
echo part1(input)
echo part2(input)