Update day 18 solution
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Parse function is not needed since it can be replaced by eval().
This commit is contained in:
@@ -2,25 +2,6 @@
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
def parse(line):
|
|
||||||
if ',' not in line:
|
|
||||||
return int(line)
|
|
||||||
line = line[1:-1]
|
|
||||||
level = 0
|
|
||||||
split_idx = 0
|
|
||||||
for i, c in enumerate(line):
|
|
||||||
if '[' == c:
|
|
||||||
level += 1
|
|
||||||
elif ']' == c:
|
|
||||||
level -= 1
|
|
||||||
elif 0 == level and ',' == c:
|
|
||||||
split_idx = i
|
|
||||||
break
|
|
||||||
left = parse(line[:split_idx])
|
|
||||||
right = parse(line[split_idx + 1:])
|
|
||||||
return (left, right)
|
|
||||||
|
|
||||||
|
|
||||||
def explode_to_left(n, l):
|
def explode_to_left(n, l):
|
||||||
(left, right) = n
|
(left, right) = n
|
||||||
res = True
|
res = True
|
||||||
@@ -123,10 +104,11 @@ def part_1(input):
|
|||||||
n = None
|
n = None
|
||||||
for line in input:
|
for line in input:
|
||||||
line = line.rstrip()
|
line = line.rstrip()
|
||||||
|
nn = eval(line.replace('[', '(').replace(']', ')'))
|
||||||
if not n:
|
if not n:
|
||||||
n = parse(line)
|
n = nn
|
||||||
continue
|
continue
|
||||||
n = (n, parse(line))
|
n = (n, nn)
|
||||||
res = True
|
res = True
|
||||||
while res:
|
while res:
|
||||||
while res:
|
while res:
|
||||||
@@ -140,8 +122,7 @@ def part_2(input):
|
|||||||
result = 0
|
result = 0
|
||||||
nbrs = set()
|
nbrs = set()
|
||||||
for line in input:
|
for line in input:
|
||||||
line = line.rstrip()
|
nbrs.add(eval(line.rstrip().replace('[', '(').replace(']', ')')))
|
||||||
nbrs.add(parse(line))
|
|
||||||
for n1 in nbrs:
|
for n1 in nbrs:
|
||||||
for n2 in nbrs:
|
for n2 in nbrs:
|
||||||
if n1 == n2:
|
if n1 == n2:
|
||||||
|
|||||||
Reference in New Issue
Block a user