-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.nim
80 lines (70 loc) · 1.84 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import std/[
algorithm,
re,
sequtils,
sets,
strformat,
strutils,
]
type Op = enum
TURN_ON
TURN_OFF
TOOGLE
type Inst = (Op, int, int, int, int)
proc parseInst(line: string): Inst =
if line =~ re"turn on (\d+),(\d+) through (\d+),(\d+)":
let m = matches[0 ..< 4].mapIt(it.parseInt)
(TURN_ON, m[0], m[1], m[2], m[3])
elif line =~ re"turn off (\d+),(\d+) through (\d+),(\d+)":
let m = matches[0 ..< 4].mapIt(it.parseInt)
(TURN_OFF, m[0], m[1], m[2], m[3])
elif line =~ re"toggle (\d+),(\d+) through (\d+),(\d+)":
let m = matches[0 ..< 4].mapIt(it.parseInt)
(TOOGLE, m[0], m[1], m[2], m[3])
else:
raise newException(ValueError, "parse error")
proc parse(input: string): seq[Inst] =
input.split("\n").mapIt(it.parseInst)
const N = 1000
proc part1(input: string): int =
var grid = newSeqWith(N, newSeq[int](N))
for (op, r1, c1, r2, c2) in input.parse:
case op:
of TURN_ON:
for r in r1 .. r2:
for c in c1 .. c2:
grid[r][c] = 1
of TURN_OFF:
for r in r1 .. r2:
for c in c1 .. c2:
grid[r][c] = 0
of TOOGLE:
for r in r1 .. r2:
for c in c1 .. c2:
grid[r][c] = grid[r][c] xor 1
for r in 0 ..< N:
for c in 0 ..< N:
result += grid[r][c]
proc part2(input: string): int =
var grid = newSeqWith(N, newSeq[int](N))
for (op, r1, c1, r2, c2) in input.parse:
case op:
of TURN_ON:
for r in r1 .. r2:
for c in c1 .. c2:
grid[r][c] += 1
of TURN_OFF:
for r in r1 .. r2:
for c in c1 .. c2:
grid[r][c] = max(0, grid[r][c] - 1)
of TOOGLE:
for r in r1 .. r2:
for c in c1 .. c2:
grid[r][c] += 2
for r in 0 ..< N:
for c in 0 ..< N:
result += grid[r][c]
when isMainModule:
let input = readAll(stdin).strip
echo part1(input)
echo part2(input)