--- Day 21: Dirac Dice ---
or..
If you roll the dice enough they become sand...
Part 1
Python
So simple you knew you were about to get your butt absolutely handed to you by part 2.
Go
Still catching up
Part 2
Python
Lanternfish? Polymers? Yes, this is a problem for caching once more. I just had a very hard time getting the concept of what to cache to make my code "performant." I found a hint in a help thread on reddit that basically pointed towards tracking the state of games, since those were not unique. To do this, I re-rewrote my game state dicts as strings so they could be dict keys:
># converts dict to game state string
def gamestate(game) -> str:
# player1|position p1|score p1| player2 | position p2 | score p2
return f"""1|{game["1"]["pos"]}|{game["1"]["score"]}|2|{game["2"]["pos"]}|{game["2"]["score"]}"""
# converts the game state string back into the original dict for easier parsing
def unformat(gamestate) -> dict:
g = gamestate.split("|")
d = { "1" : {"pos" : int(g[1]),
"score" : int(g[2]),
},
"2" : { "pos" : int(g[4]),
"score" : int(g[5]),
}
}
return d
Once I had this in place, I had a solution that completed in 40 seconds which is fast enough for me.
Go
We'll see.
<< - Table of Contents - >>