Source: Hex Ed1
Part 1: Work on a hex grid:
\ n /
nw +–+ ne
/
-+ +-
\ /
sw +–+ se
/ s \
That makes code for the actual question nice and clean:
origin = (0, 0, 0)
for line in lib.input():
point = origin
for direction in line.split(','):
point = move(point, direction)
distance_to_origin = distance(origin, point)
print(f'ended at {point} ({distance_to_origin} from origin)')
Part 2: What is the furthest point from the origin visited?
Similar to day 8, we just need to track the maximum value seen thus far as we go:
for line in lib.input():
point = origin
furthest_distance = 0
furthest_point = None
for direction in line.split(','):
point = move(point, direction)
distance_to_origin = distance(origin, point)
if distance_to_origin > furthest_distance:
furthest_distance = distance_to_origin
furthest_point = point
print(f'ended at {point} ({distance_to_origin} from origin); furthest was {furthest_point} ({furthest_distance} from origin)')
That’s a pretty interesting problem as well.
$ python3 run-all.py day-11
day-11 python3 its-full-of-hexagons.py input.txt 0.0798640251159668 ended at (650, -313, -337) (650 from origin); furthest was (1465, -1070, -395) (1465 from origin)
Not sure who’s being punnier here. 😄 ↩︎