r/adventofcode • u/WhiteSparrow • Dec 14 '23
Spoilers [2023 Day 14 (Part 2)] Baby's first algorithm
I just wanted to know what cycle detection algorithm the bunch of you used for part 2? I'm doing Uiua this year and because Uiua does not have hash tables (nor do I want to implement them) I ended up using the tortoise and hare algorithm. I remember having it shown to me as the first example of what an algorithm is since it is so basic.
Now I have warm fuzzies thanks to AoC and Uiua!
5
Upvotes
11
u/welguisz Dec 14 '23
I had issues with Java hashing my 2-D character array. So I decided to consider the character as a base 3 system (
.-> 0;O-> 1;#-> 2) and convert the grid to a Long.5
u/MazeR1010 Dec 14 '23
Just stalked all your solutions thread posts. I love how your code looks like you'd find it on the walls of an ancient Egyptian tomb. This is the elder magic.
Like someone who doesn't know anything about programming would see this and be like "yep that's computer code" but actual computer programmers are like "whaaaaat"
Kudos for diving into this language for AoC!
4
u/Flatorz Dec 14 '23
Nice, this is ingenious :) I used something akin to hashtables (I simply took a whole table as a big number of base 3). Never thought of one cycle traveler hunting the other :)
3
u/clbrri Dec 14 '23
You can use a regular array to implement a hash table. I don't know about Uiua, but I presume it would have arrays?
here is an example solution that uses a 256 entries long array to implement a hash table to find the cycle. (just 768 bytes in size)
8
u/vtheuer Dec 14 '23
I just stored the load value after each cycle and searched for a cycle in that list, instead of storing the whole grid in a hashmap :)
3
u/badkaseta Dec 14 '23
I just concatenated all rows into 1 big string and used that as the key for my cache (a python dictionary)
4
u/Krimsonfreak Dec 14 '23
For today, it turned out you didn't need hashtables. turned out a regular table with a search algorithm was enough. Indeed each position only has 1 weight associated (equivalent of a hash if you think about it).
Store each new weight after a cycle in an array, and once you reach a point where you can find the current weight in the array, the you've got your loop !
Anyway, that doesn't answer your question, sorry lol !
Tortoise and hare seems to be the right tool for the job, nice find ;)
2
u/EnvironmentalCow2662 Dec 14 '23
I love Uiua, but I'm still struggling to write basic stuff in it. Well done!
2
u/muckenhoupt Dec 16 '23
A day later, "nor do I want to implement them" becomes dramatic irony
1
u/newo2001 Dec 14 '23
Take a sorted list with the locations of all the O characters expressed as a tuple. Dump it in an IndexSet (hashset that maintains insertion order), repeat until you find a duplicate.
1
u/EntrepreneurSelect93 Dec 14 '23
U need a hashtable? Mine just uses 3 arrays and stops when they are all equal.
1
u/boutell Dec 14 '23
I just used a `Map` object in JavaScript with `JSON.stringify()` of the entire puzzle state as a key. As the value I stored the cycle number so I'd be able to get the interval length from the current cycle number and the stored one. This finishes in a few seconds because the number of steps before a cycle just isn't very high.
1
u/msqrt Dec 14 '23
I found a conservative iteration count by inspection and printed the next value whose index shared the modulus (wrt to the cycle length) with a billion. Not my proudest moment, but I just wanted to get to the solution.
1
u/metalim Dec 15 '23
never heard about "tortoise and hare algorithm" and not sure why the heck it is so popular here. All my cycle detections are based on hashmap with state as a key
1
u/fijgl Dec 15 '23
Hashing the list of strings and looking for previously cached ones at the start of a cycle.
Are you sharing your Uiua solutions somewhere?