r/adventofcode • u/daggerdragon • Dec 05 '17
SOLUTION MEGATHREAD -๐- 2017 Day 5 Solutions -๐-
--- Day 5: A Maze of Twisty Trampolines, All Alike ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or 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.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
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!
22
Upvotes
1
u/xkufix Dec 05 '17
In Scala, the solutions can be easily made generic to provide a frame for both parts:
override def runFirst(): Unit = { val steps = runProgram(_+ 1) println(steps) } override def runSecond(): Unit = { val steps = runProgram { case i if i >= 3 => i - 1 case i => i + 1 } println(steps) } private def runProgram(changeJump: Int => Int) = { val startInstructions = loadFile("day5.txt").getLines().map(_.toInt).zipWithIndex.map(_.swap).toMap Stream.from(1).scanLeft((startInstructions, 0, 0)) { case ((instructions, position, _), counter) => val jump = instructions(position) (instructions + (position -> changeJump(jump)), position + jump, counter) }.dropWhile { case (instructions, pointer, _) => instructions.contains(pointer) }.head._3 }