Advent of Code: Day 11

For Day 11, the challenge is to optimize a path through a hex grid. My Python solution is below:

with open("input.txt", "r") as o:
    directions = o.read().split(",")

DIR = {
    "n": (0, 1, -1),
    "nw": (-1, 1, 0),
    "sw": (-1, 0, 1),
    "ne": (1, 0, -1),
    "s": (0, -1, 1),
    "se": (1, -1, 0)
}

x = y = z = 0
max_dist = 0

def get_dist(x, y, z):
    return (abs(x) + abs(y) + abs(z)) / 2

for d in directions:
    x += DIR[d][0]
    y += DIR[d][1]
    z += DIR[d][2]

    max_dist = max(max_dist, get_dist(x, y, z))

print "Part 1: ", get_dist(x, y, z)
print "Part 2: ", max_dist

The best place to learn about hex grids that I've seen is Red Blob Games. Once you get your head around how the grids work today's challenge isn't too difficult.

Advent of Code runs every day up to Christmas, you should join in!.

Show Comments

Get the latest posts delivered right to your inbox.