DAY 8 BABY LETS GO
This commit is contained in:
parent
37bc98fafb
commit
e02dfaab32
2 changed files with 34 additions and 0 deletions
33
aoc/src/main/scala/aoc/y2019/Day08.scala
Normal file
33
aoc/src/main/scala/aoc/y2019/Day08.scala
Normal file
|
@ -0,0 +1,33 @@
|
|||
package aoc.y2019
|
||||
|
||||
import aoc.Day
|
||||
|
||||
object Day08 extends Day {
|
||||
|
||||
val imgWidth = 25
|
||||
val imgHeight = 6
|
||||
|
||||
override def part1(input: String): String = {
|
||||
val image = input.map(_ - '0').toVector.grouped(imgWidth).toVector.grouped(imgHeight).toVector
|
||||
val layerWithLeastZeros = image.minBy(_.map(_.count(_ == 0)).sum)
|
||||
(layerWithLeastZeros.map(_.count(_ == 1)).sum * layerWithLeastZeros.map(_.count(_ == 2)).sum).toString
|
||||
}
|
||||
|
||||
override def part2(input: String): String = {
|
||||
val image = input.map(_ - '0').toVector.grouped(imgWidth).toVector.grouped(imgHeight).toVector
|
||||
val message = image.foldLeft(Vector.fill(imgHeight, imgWidth)(2)) { (image, vector) =>
|
||||
image.zip(vector).map {
|
||||
case (imageRow, vectorRow) =>
|
||||
imageRow.zip(vectorRow).map {
|
||||
case (2, v) => v
|
||||
case (o, _) => o
|
||||
}
|
||||
}
|
||||
}
|
||||
"\n" ++ message.map(_.map {
|
||||
case 0 => " "
|
||||
case 1 => "#"
|
||||
}.mkString).mkString("\n")
|
||||
}
|
||||
|
||||
}
|
|
@ -10,6 +10,7 @@ package object y2019 extends Year {
|
|||
"5" -> Day05,
|
||||
"6" -> Day06,
|
||||
"7" -> Day07,
|
||||
"8" -> Day08,
|
||||
)
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue