This commit is contained in:
Aly 2018-11-25 15:18:27 -08:00
parent b0222f3f42
commit c357030b21
No known key found for this signature in database
GPG key ID: 555B7346639DDAC3
3 changed files with 53 additions and 10 deletions

View file

@ -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 = {

View 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
}
}

View file

@ -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))
}
}