-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDay01.scala
28 lines (22 loc) · 848 Bytes
/
Day01.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.lmat.adventofcode.year2017
import com.lmat.adventofcode.SimpleCommonPuzzle
import com.lmat.util.Files.readResource
import com.lmat.util.Sequences.shiftRight
object Day01 extends SimpleCommonPuzzle[String, Int, Int] {
override def parse(resource: String): String =
readResource(resource).head
override def part1(captcha: String): Int =
solveCaptcha(1)(captcha)
override def part2(captcha: String): Int =
solveCaptcha(captcha.length / 2)(captcha)
def solveCaptcha(shiftAmount: Int)(captcha: String): Int = {
val digits = captcha.toIndexedSeq.map(_.asDigit)
val shifted = shiftRight(digits, shiftAmount)
sumEqualDigits(digits, shifted)
}
def sumEqualDigits(digits: Seq[Int], digits2: Seq[Int]): Int =
(digits zip digits2)
.filter { case (a, b) => a == b }
.map(_._1)
.sum
}