-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.nim
65 lines (55 loc) · 1.18 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
61
62
63
64
65
import std/[
algorithm,
bitops,
deques,
json,
math,
re,
sequtils,
sets,
strformat,
strutils,
tables,
sugar,
]
type
Row = string
proc nextRow(self: Row): Row =
let N = self.len
for i in 0 ..< N:
let left = (if i - 1 >= 0 and self[i - 1] == '^': '^' else: '.')
let center = self[i]
let right = (if i + 1 < N and self[i + 1] == '^': '^' else: '.')
let p = &"{left}{center}{right}"
result &= (if p in ["^^.", ".^^", "^..", "..^"]: '^' else: '.')
when defined(test):
block:
doAssert nextRow("..^^.") == ".^^^^"
doAssert nextRow(".^^^^") == "^^..^"
block:
let lines = """
.^^.^.^^^^
^^^...^..^
^.^^.^.^^.
..^^...^^^
.^^^^.^^.^
^^..^.^^..
^^^^..^^^.
^..^^^^.^^
.^^^..^.^^
^^.^^^..^^
""".strip.split("\n")
for i in 1 ..< lines.len:
doAssert nextRow(lines[i - 1]) == lines[i]
proc part1(input: string, n: int): int =
var s = input
result = s.countIt(it == '.')
for i in 1 ..< n:
s = s.nextRow
result += s.countIt(it == '.')
when defined(test):
doAssert part1(".^^.^.^^^^", 10) == 38
when isMainModule and not defined(test):
let input = readAll(stdin).strip
echo part1(input, 40)
echo part1(input, 400000)