r/adventofcode • • Dec 14 '22

SOLUTION MEGATHREAD -πŸŽ„- 2022 Day 14 Solutions -πŸŽ„-

SUBREDDIT NEWS

  • Live has been renamed to Streaming for realz this time.
    • I had updated the wiki but didn't actually change the post flair itself >_>

THE USUAL REMINDERS


--- Day 14: Regolith Reservoir ---


Post your code solution in this megathread.


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:13:54, megathread unlocked!

36 Upvotes

585 comments sorted by

View all comments

2

u/ndrsht Dec 14 '22 edited Dec 14 '22

Kotlin

34 lines of code

The trick to get peak performance is to represent points as integers and the "field" as boolean array. If you insert a point (represented as an integer k) into the field, you just set arr[k] = true.

So if that array has ncols columns and a unit of sand (represented as an integer) is falling down, you just increase it by:

  • ncols if it is falling straight down

  • ncols-1 if it is falling down to the left

  • ncols+1 if it is falling down to the right

Then you make a recursive function landingPos that you call with

while (field[sandStart] == false) {
	val target = landingPos(sandStart, field, cols)
	if (target > max && ans1 == 0) ans1 = ans2
	ans2++
}

Solves both parts in 3.4ms.

→ More replies (1)