Day 10
This commit is contained in:
parent
b0222f3f42
commit
c357030b21
3 changed files with 53 additions and 10 deletions
|
@ -12,16 +12,6 @@ object Day06 extends Day {
|
|||
go(as, Set.empty)
|
||||
}
|
||||
|
||||
def rotateLeft[A](list: List[A], i: Int): List[A] = {
|
||||
val size = list.size
|
||||
list.drop(i % size) ++ list.take(i % size)
|
||||
}
|
||||
|
||||
def rotateRight[A](list: List[A], i: Int): List[A] = {
|
||||
val size = list.size
|
||||
list.drop(size - (i % size)) ++ list.take(size - (i % size))
|
||||
}
|
||||
|
||||
case class State(blocks: List[Int]) {
|
||||
|
||||
def step: State = {
|
||||
|
|
42
aoc/src/main/scala/aoc/y2017/Day10.scala
Normal file
42
aoc/src/main/scala/aoc/y2017/Day10.scala
Normal file
|
@ -0,0 +1,42 @@
|
|||
package aoc.y2017
|
||||
|
||||
import aoc.Day
|
||||
|
||||
object Day10 extends Day {
|
||||
|
||||
def knot(ring: List[Int], by: Int, at: Int): List[Int] = {
|
||||
val shifted = rotateLeft(ring, at)
|
||||
val (sHead, sTail) = shifted.splitAt(by)
|
||||
val spliced = sHead.reverse ++ sTail
|
||||
rotateRight(spliced, at)
|
||||
}
|
||||
|
||||
def runShifts(shifts: List[Int]): List[Int] = {
|
||||
val ring = (0 to 255).toList
|
||||
val (last, _) = shifts.zipWithIndex.foldLeft((ring, 0)) {
|
||||
case ((cr, acc), (shift, jump)) =>
|
||||
val k = knot(cr, shift, acc)
|
||||
val i = acc + shift + jump
|
||||
(k, i)
|
||||
}
|
||||
last
|
||||
}
|
||||
|
||||
override def part1(input: String): String = {
|
||||
val shifts = input.split(',').map(_.toInt).toList
|
||||
val last = runShifts(shifts)
|
||||
(last.head * last.tail.head).toString
|
||||
}
|
||||
|
||||
override def part2(input: String): String = {
|
||||
val magic = List(17, 31, 73, 47, 23)
|
||||
val codes = input.map(_.toInt) ++ magic
|
||||
val shifts = List.fill(64)(codes).flatten
|
||||
val last = runShifts(shifts)
|
||||
val hashGroups = last.grouped(16)
|
||||
val xored = hashGroups.map(_.reduce(_ ^ _))
|
||||
val hexed = xored.map(_.toHexString.reverse.padTo(2, '0').reverse.toString)
|
||||
hexed.mkString
|
||||
}
|
||||
|
||||
}
|
|
@ -14,7 +14,18 @@ package object y2017 extends Year {
|
|||
"7" -> Day07,
|
||||
"8" -> Day08,
|
||||
"9" -> Day09,
|
||||
"10" -> Day10,
|
||||
"25" -> Day25
|
||||
)
|
||||
|
||||
def rotateLeft[A](list: List[A], i: Int): List[A] = {
|
||||
val size = list.size
|
||||
list.drop(i % size) ++ list.take(i % size)
|
||||
}
|
||||
|
||||
def rotateRight[A](list: List[A], i: Int): List[A] = {
|
||||
val size = list.size
|
||||
list.drop(size - (i % size)) ++ list.take(size - (i % size))
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue