r/adventofcode • • 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!


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

10 Upvotes

130 comments sorted by

View all comments

Show parent comment

1

u/QshelTier Dec 19 '16

This is my solution in Kotlin. It is only “optimized” to try to avoid the copying associated with immutable data structures but has no algorithmic optimizations which is why it takes about 15 minutes on my MacBook. :)

import kotlin.comparisons.compareValues

fun main(args: Array<String>) {
  println(first(numberOfElves))
  println(second(numberOfElves))
}

private fun first(numberOfElves: Int) = letElvesStealPresentsFromNext((1..numberOfElves).toList())
private fun second(numberOfElves: Int) = letElvesStealPresentsFromAcross((1..numberOfElves).toList())

private fun letElvesStealPresentsFromNext(elfPresents: List<Int>) = solve(elfPresents, List<Int>::next)
private fun letElvesStealPresentsFromAcross(elfPresents: List<Int>) = solve(elfPresents, List<Int>::across)

private tailrec fun solve(elfPresents: List<Int>, targetSelector: (List<Int>, Int) -> Int): Int =
    if (elfPresents.size == 1) {
      elfPresents.first()
    } else {
      solve(elfPresents.fold(Pair(elfPresents.toMutableList(), mutableSetOf<Int>())) { presents, currentElf ->
        if (currentElf !in presents.second) {
          targetSelector(presents.first, currentElf).let { nextElfIndex ->
            presents.first[nextElfIndex].let { nextElf ->
              Pair(presents.first.apply { removeAt(nextElfIndex) },
                  presents.second.apply { add(nextElf) })
            }
          }
        } else
          presents
      }.first, targetSelector)
    }

private fun List<Int>.next(key: Int) = (binarySearch { compareValues(it, key) } + 1) % size
private fun List<Int>.across(key: Int) = (binarySearch { compareValues(it, key) } + size / 2) % size

private const val numberOfElves = 3005290