Back to Home

Hex Game of Life

Published on

Project

Hexagonals

This was my fun Christmas eve project! Christmas eve was sure a lot less action packed as it normally is, and I decided to entertain myself with a fun programming challenge!

I logged into adventofcode and went straight to Day 24. I'm not 100% sure, but I believe these advent challenges are supposed to get a little harder each day. So picking day 24, after having not done many challenges in a while was going to be difficult.

The problem: Your raft makes it to the tropical island; it turns out that the small crab was an excellent navigator. You make your way to the resort.

As you enter the lobby, you discover a small problem: the floor is being renovated. You can't even reach the check-in desk until they've finished installing the new tile floor.

The tiles are all hexagonal; they need to be arranged in a hex grid with a very specific color pattern. Not in the mood to wait, you offer to help figure out the pattern.

The tiles are all white on one side and black on the other. They start with the white side facing up. The lobby is large enough to fit whatever pattern might need to appear there.

A member of the renovation crew gives you a list of the tiles that need to be flipped over (your puzzle input). Each line in the list identifies a single tile that needs to be flipped by giving a series of steps starting from a reference tile in the very center of the room. (Every line starts from the same reference tile.)

Because the tiles are hexagonal, every tile has six neighbors: east, southeast, southwest, west, northwest, and northeast. These directions are given in your list, respectively, as e, se, sw, w, nw, and ne. A tile is identified by a series of these directions with no delimiters; for example, esenee identifies the tile you land on if you start at the reference tile and then move one tile east, one tile southeast, one tile northeast, and one tile east.

Each time a tile is identified, it flips from white to black or from black to white. Tiles might be flipped more than once. For example, a line like esew flips a tile immediately adjacent to the reference tile, and a line like nwwswee flips the reference tile itself.

Here is a larger example:

sesenwnenenewseeswwswswwnenewsewsw neeenesenwnwwswnenewnwwsewnenwseswesw seswneswswsenwwnwse nwnwneseeswswnenewneswwnewseswneseene swweswneswnenwsewnwneneseenw eesenwseswswnenwswnwnwsewwnwsene sewnenenenesenwsewnenwwwse wenwwweseeeweswwwnwwe wsweesenenewnwwnwsenewsenwwsesesenwne neeswseenwwswnwswswnw nenwswwsewswnenenewsenwsenwnesesenew enewnwewneswsewnwswenweswnenwsenwsw sweneswneswneneenwnewenewwneswswnese swwesenesewenwneswnwwneseswwne enesenwswwswneneswsenwnewswseenwsese wnwnesenesenenwwnenwsewesewsesesew nenewswnwewswnenesenwnesewesw eneswnwswnwsenenwnwnwwseeswneewsenese neswnwewnwnwseenwseesewsenwsweewe wseweeenwnesenwwwswnew

In the above example, 10 tiles are flipped once (to black), and 5 more are flipped twice (to black, then back to white). After all of these instructions have been followed, a total of 10 tiles are black.

Go through the renovation crew's list and determine which tiles they need to flip. After all of the instructions have been followed, how many tiles are left with the black side up? The solution So it had taken me a while to figure out, that this problem essentially is a Game of Life problem, however instead of the normal squares, the problem was shaped hexagonally. Only until figuring this out, that I was prepared to tackle the problem.

I drew a few sketches modelling the problem. Which I recommend to everyone to do, this really helps break down tricky problems, into something that is a bit more real than "nenenenesenwsewnenwww...."

Here is the drawing:

Game of life solution runthrough

I also painted over the model, to trace through different edge cases, to help me through.

I'd say this is also part of a TDD lifecycle, Whiteboard, Test, Code, Refactor. Before, I had any real chance of understatanding the problem, I started to draw it out and began to have ideas.

The problem, was incredibly fun, and a really great way to stretch my python skills. You can check out my solution here