forked from matelaszlo/advent-of-code-scala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay02.scala
48 lines (35 loc) · 1.4 KB
/
Day02.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.lmat.adventofcode.year2015
import com.lmat.adventofcode.SimpleCommonPuzzle
import com.lmat.adventofcode.year2015.Day02Definitions._
import com.lmat.util.Files.readResource
object Day02Definitions {
case class Box(length: Int, width:Int, height:Int)
}
object Day02 extends SimpleCommonPuzzle[Seq[Box], Int, Int] {
override def parse(resource: String): Seq[Box] = readResource(resource).flatMap(parseBox)
def parseBox(line:String): Option[Box] = {
val box = "(.*)x(.*)x(.*)".r
line match {
case box(length, width, height) => Some(Box(length.toInt, width.toInt, height.toInt))
case _ => None
}
}
override def part1(boxes: Seq[Box]): Int =
boxes.map(wrapping).sum
def wrapping(box: Box): Int =
area(box) + sideAreas(box).min
def area(box: Box): Int =
2 * box.length * box.width +
2 * box.width * box.height +
2 * box.height * box.length
def sideAreas(box: Box): Seq[Int] =
(box.length * box.width) :: (box.width * box.height) :: (box.height * box.length) :: Nil
override def part2(boxes: Seq[Box]): Int =
boxes.map(ribbon).sum
def ribbon(box: Box): Int =
volume(box) + perimeters(box).min
def volume(box: Box): Int =
box.length * box.width * box.height
def perimeters(box: Box): Seq[Int] =
(box.length + box.width) * 2 :: (box.width + box.height) * 2 :: (box.height + box.length) * 2 :: Nil
}