-
Notifications
You must be signed in to change notification settings - Fork 0
/
day13_2.nim
158 lines (135 loc) · 3.53 KB
/
day13_2.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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: FULL
## RUN: TEST
import sequtils
import strutils
import algorithm
type
PacketKind = enum
pkInt, pkSeq
Packet = ref object
case kind: PacketKind # the ``kind`` field is the discriminator
of pkInt:
intVal: int
of pkSeq:
seqVal: seq[Packet]
func len(p: Packet): int =
case p.kind
of pkInt:
return 1
of pkSeq:
return p.seqVal.len
func `[]`(p: Packet, i: int): Packet =
case p.kind
of pkInt:
return p
of pkSeq:
return p.seqVal[i]
proc echo(p: Packet, level: int = 0) =
case p.kind
of pkInt:
write(stdout, p.intVal)
of pkSeq:
write(stdout, "[")
let N = p.seqVal.len
for i, p in p.seqVal:
echo(p, level+1)
if i < N-1:
write(stdout, ",")
write(stdout, "]")
if level == 0:
write(stdout, "\n")
func newPacket(intVal: int): Packet =
result = Packet(kind: pkInt, intVal: intVal)
func newPacket(seqVal: seq[Packet]): Packet =
result = Packet(kind: pkSeq, seqVal: seqVal)
func newPacket(seqVal: seq[int]): Packet =
result = Packet(kind: pkSeq, seqVal: seqVal.map(newPacket))
func toPacket(s: string): Packet =
if s[0] == '[' and s[^1] == ']':
# Parse top-level bracket
var
bracket_level = 0
last_parsed = 0
parts: seq[string]
for i in countup(1, len(s)-2):
if s[i] == '[':
bracket_level += 1
elif s[i] == ']':
bracket_level -= 1
elif s[i] == ',' and bracket_level == 0:
parts.add(s[last_parsed+1..i-1])
last_parsed = i
parts.add(s[last_parsed+1..^2]) # Add last part
return newPacket(parts.filterIt(it.len > 0).mapIt(it.toPacket))
else:
# Parse int
return newPacket(s.parseInt)
var packets: seq[Packet] = @[]
let file: File = open(filename, fmRead)
try:
while not endOfFile(file):
var line = readLine(file)
if line.len == 0:
continue
packets.add(line.toPacket)
finally:
file.close()
func sign(a, b: int): int =
if a < b:
return -1
elif a > b:
return 1
else:
return 0
func `<?`(a, b: Packet): int =
case a.kind
of pkInt:
case b.kind
of pkInt:
return sign(b.intVal, a.intVal)
of pkSeq:
return newPacket(@[a]) <? b
of pkSeq:
case b.kind
of pkInt:
return a <? newPacket(@[b])
of pkSeq:
for i in 0..<max(a.len, b.len):
if i > a.len-1:
return 1
if i > b.len-1:
return -1
let s = a[i] <? b[i]
if s != 0:
return s
return 0
func `<`(p1, p2: Packet): bool =
let r = p1 <? p2
assert(r != 0)
return r == 1
let
divider_1 = "[[2]]".toPacket
divider_2 = "[[6]]".toPacket
packets.add(divider_1)
packets.add(divider_2)
packets.sort()
var
divider_2_index = -1
divider_1_index = -1
for i, p in packets:
echo p
if p == divider_1:
assert(divider_1_index == -1)
divider_1_index = i+1
elif p == divider_2:
assert(divider_2_index == -1)
divider_2_index = i+1
echo divider_1_index * divider_2_index