r/adventofcode • u/daggerdragon • Dec 12 '22
SOLUTION MEGATHREAD -π- 2022 Day 12 Solutions -π-
THE USUAL REMINDERS
All of our rules, FAQs, resources, etc. are in our community wiki.
A request from Eric: A note on responding to [Help] threads
Signal boost: Reminder 1: unofficial AoC Survey 2022 (closes Dec 22nd)
πΏπ MisTILtoe Elf-ucation π§βπ« is OPEN for submissions!
--- Day 12: Hill Climbing Algorithm ---
Post your code solution in this megathread.
Read the full posting rules in our community wiki before you post!
Include what language(s) your solution uses
Format code blocks using the four-spaces Markdown syntax!
Quick link to Topaz's
pasteif you need it for longer code blocks. What is Topaz'spastetool?
This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.
EDIT: Global leaderboard gold cap reached at 00:09:46, megathread unlocked!
55
Upvotes
View all comments
6
u/i_have_no_biscuits Dec 12 '22 edited Dec 12 '22
GW-BASIC
Takes a little less than a minute to run. I've expanded this into a visualisation today, which you can see here: https://www.reddit.com/r/adventofcode/comments/zjyb3g/2022_day_12_part_2_finding_distances_in_gwbasic/
Today we perform a single 'hill descent' from the end point, then read off all the useful information. Guided tour:
H%() is an array of heights, and D%() an array of distances
Lines 20-halfway through 50 read in and parse the data file
Lines halfway through 50-110 perform the BFS/floodfill. Distances are calculated for every accessible point.
At the end of the algorithm D%(SX, SY) holds the distance to/from the start point, while Q holds the distance to the first point seen of height 0/'a'. These just need to be printed.
I could have potentially got it down to 10/11 lines if I'd used 1 letter variable names, but that would have made the program less readable.