When a puzzle starts out as “see, it’s a grid, and something can be in a cell or it can be empty, and cells can change depending on what’s around them” — I think John Conway’s “Game of Life”. There’s one of these, every year.

The rules were… shorter than expected. There was only one rule, namely that any cell that had fewer than four neighbors would die. Part 1 was to just iterate once; part 2 was to iterate until it reached a steady state, and the values in both were the total number of cells cleared.

The picture up top has, overlaid on it, a portion of the final state after part 2 completed. I thought it might form a recognizable shape or something, but it was just blobs. Was worth doing.

Building the initial grid

The thing about Conway’s Game of Life, even this alternate rules one, is that you’re not really interested in empty cells, and the less time you can spend with them, the better. After all, there’s an infinity of empty cells, and only a few set cells.

I guess it’s like the Monty Haul thought experiment. Given three doors, behind each of which is a prize. Two of the prizes are joke prizes, and one is really nice. You choose a door; the host opens one of the doors you didn’t choose to reveal a joke prize, then offers to let you change your guess. Do you? It’s hard to figure out.

But let’s say there are 1000 doors. You pick a door, and the host opens 998 doors with joke prizes behind them leaving just your door, and one other door. Do you change to that door? Sure. It’s always better to change your pick.

Game of Life is like that. It takes place on an infinite grid. If there’s as many empty spaces as filled spaces in the grid, do you model the whole grid? What if it’s just set in the middle of an infinity of empty cells stretching all directions? You ignore the empty ones.

When I encounter this sort of puzzle, that’s just what I do. I save the positions of the filled cells and never consider the empty ones. These filled cell position are stored in the paper_table table in the code above.

Apply the DEATH RULE

I had an interesting discussion with another AoC participant this morning on Mastodon about this puzzle. Mina thought people should avoid using tables/dictionaries/maps or what have you and just use arrays, as the random access nature of arrays made them superior to the more indirect addressing for maps. I pointed out that my language for this AoC, Lua, doesn’t have arrays; not real ones, anyway.

It made me think about my solution. I solved it just before I had to go to work, and was happy with my solution. I kept thinking about the discussion while at work. I can’t use arrays, that was a non-starter for Lua, but you can see in the code above that for each active cell, I look at all the neighbors to count the number of nearby active cells. This means looking at empty cells a lot. Perhaps I could just make a neighbors table and run through one pass to set up neighbor counts, then another pass to remove cells that don’t make the cut.

This was slower, and so I went with my original solution. It still isn’t fast enough to keep my kitty animating cleanly. I could add more yields, but I don’t care that much.

Four puzzles down, eight to go.

2 responses to “Advent of Code Day 4: Printing Department”

  1. bhagpuss Avatar

    Every year, when you do the Advent of Code, I barely even understand the puzzles. I never have the least clue what you’re doing to solve them. But I still read them. And this year, for once, I learned something! From Gemini!

    I have never been able to understand the reasoning behind the Monty Haul experiment. It’s always seemed to make no sense. Surely, when you have two doors left, the chance has to fifty-fifty. When I read your version with a thousand doors it made no more sense so I thought… why not as an AI to explain it in simple language?

    I asked Gemini and once I read the explanation I understood it immediately. Not because Gemini explained the probability in a way I could understand, although it was very god with that part too, but because Gemini pointed out the one vital piece of information that no other version of the experiment I’d ever seen had emphasized, which is that the person opening the doors KNOWS WHAT’S BEHIND THEM!!

    Once you know that, it’s obvious you have to change your pick.

    Seriously, I must have seen this puzzle dozens of times and if any of the set-ups ever mentioned that crucial piece of information, I have never noticed. I bet most of them never did, though. They all assume you know how game shows work so there’s no need. I always assumed the person opening the door has no more idea than you what’s behind it.

    It does beg the question of what happens if you guess the right door on your first guess, though. Do the experimenters ever account for that?

    1. Tipa Avatar

      Sure, in the game show you have a 33% chance of guessing correctly, and a 67% chance you guessed wrong. But you still had that chance you guess correctly. In the thousand door example, there was a 0.1% chance you guessed correctly — but there was still a chance.

Leave a Reply

Discover more from West Karana

Subscribe now to keep reading and get access to the full archive.

Continue reading