Adding day 18 p1 and 19 p1 + creating folder 2018

This commit is contained in:
Xavier Morel
2019-01-17 16:40:36 +01:00
parent 5fb50b11af
commit 5723d21ecb
32 changed files with 121 additions and 0 deletions

34
2018/day6/part2.scala Normal file
View File

@@ -0,0 +1,34 @@
import scala.io.StdIn.readLine
case class Pos(x: Int, y: Int) {
def dist(other: Pos): Int = (other.x - x).abs + (other.y - y).abs
}
case class Coords(id: Int, p: Pos)
case class CoordsWithDist(c: Coords, dist: Int)
val MAX_X: Int = 400
val MAX_Y: Int = 400
val MAX_DIST_SUM: Int = 10000
val coords: List[Coords] = Iterator
.continually(readLine)
.takeWhile(_ != null)
.map { s =>
val p = s.split(", ")
Pos(p(0).toInt, p(1).toInt)
}
.toList
.zipWithIndex
.map(x => Coords(x._2, x._1))
val distSum = for {
x <- 0 to MAX_X
y <- 0 to MAX_Y
p = Pos(x, y)
c = coords.map(_.p.dist(p)).sum if c < MAX_DIST_SUM
} yield (p, c)
println(distSum.size)