advent2023/achtzehn.py

42 lines
1.0 KiB
Python
Executable File

#!/usr/bin/env python3
from useful import *
dirind = {'U': (-1, 0),
'D': (1, 0),
'L': (0, -1),
'R': (0, 1)}
def parse(l):
rection, distance, _ = l.strip().split()
return np.asarray(dirind[rection]), int(distance)
def parse2(l):
_, _, pcolor = l.strip().split()
distance = int(pcolor[2:7], 16)
rection = 'RDLU'[int(pcolor[7])]
return np.asarray(dirind[rection]), int(distance)
def make_graph(guide):
edges = []
head = np.asarray((0, 0))
# create path
for rection, distance in guide:
tail = head
head = head + rection * distance
edges += [(tail, head)]
return edges
def shoelace(edges):
return abs(sum(e[0][0] * e[1][1] - e[1][0] * e[0][1] for e in edges) // 2)
linez = lines(open(0))
guide = [parse(l) for l in linez]
edges = make_graph(guide)
print(shoelace(edges) + sum(d for (_, d) in guide) // 2 + 1)
guide2 = [parse2(l) for l in linez]
edges2 = make_graph(guide2)
print(shoelace(edges2) + sum(d for (_, d) in guide2) // 2 + 1)