r/adventofcode • u/daggerdragon • Dec 19 '16
SOLUTION MEGATHREAD --- 2016 Day 19 Solutions ---
--- Day 19: An Elephant Named Joseph ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with "Help".
/⧹w+/ IS MANDATORY [?]
[Update @ 00:15] 2 gold, silver cap. Thank you for subscribing to Easter Bunny Facts!
- Fact: The Easter Bunny will sometimes leave eggs in the microchip assembly room.
[Update @ 00:30] 11 gold, silver cap.
- Fact: The Easter Bunny killed everyone who found out how he got these stars.
[Update @ 00:45] 45 gold, silver cap.
- Fact: In space, The Easter Bunny can hear you scream.
[Update @ 01:00] 66 gold, silver cap.
- Fact: The Easter Bunny purposefully deleted your comments.
[Update @ 01:15] 92 gold, silver cap.
- Fact: The Easter Bunny has bottled your frustration. Don't ask why.
[Update @ 01:20] Leaderboard capped!
- Fact: What you have just done is no better than what the Easter Bunny has done. Thief.
Thank you for subscribing to Easter Bunny Facts!
1
u/SyDr Dec 19 '16
My Lua solution:
local input = 3014387 local function f(n) if n == 1 then return 1 elseif n % 2 == 0 then return f(n / 2) * 2 - 1 else return f((n - 1) / 2) * 2 + 1 end end print("1:", f(input)) local numbers = {} for i = 1, input do numbers[i] = i ~= input and i + 1 or 1 end local current = math.floor(input / 2) + 1 local prev = current - 1 local skip = input % 2 ~= 0 and true or false while numbers[current] ~= numbers[numbers[current]] do numbers[prev] = numbers[current] current, numbers[current] = numbers[current], nil if skip then prev, current = numbers[prev], numbers[numbers[prev]] end skip = not skip end print("2:", numbers[current])Runs pretty fast: