Skip to content

Instantly share code, notes, and snippets.

@eugene70
Created June 4, 2021 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eugene70/46abca0e1380b7ddee3b10fa0352f351 to your computer and use it in GitHub Desktop.
Save eugene70/46abca0e1380b7ddee3b10fa0352f351 to your computer and use it in GitHub Desktop.
package aoc2020
import scala.annotation.tailrec
object Day23:
type InputType = String
@main def runDay23: Unit =
val testData = time("testReady", () => ready(testInput))
time("testPart1", () => part1(testData)) // should be 67384529
time("testPart2", () => part2(testData))
val data = time("ready", () => ready(input))
time("part1", () => part1(data)) // 38925764
time("part2", () => part2(data))
private def ready(input: String): InputType =
input
private def part1(data: InputType): String =
val result = move(100, data)
val results = result.split("1")
results.last + results.head
@tailrec
def move(cnt: Int, cups: String): String =
if (cnt == 0) return cups
val current = cups.head
val tail = cups.tail.splitAt(3)
val pickUp = tail._1
val remain = tail._2
val dest = remain.sorted.findLast(_ < current).getOrElse(remain.sorted.last)
val parts = remain.split(dest)
move(cnt - 1, parts.head + dest + pickUp + (if (parts.length > 1) parts.last else "") + current)
private def part2(data: InputType): Long =
0
private val testInput = "389125467"
private val input = "562893147"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment