-
Notifications
You must be signed in to change notification settings - Fork 3
/
Day03.scala
41 lines (34 loc) · 1.26 KB
/
Day03.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
package AdventOfCode2017
object Day03:
val adjacent = for y <- -1 to 1; x <- -1 to 1 if !(x == 0 && y == 0) yield (x, y)
case class Point(x: Int, y: Int):
def delta(dx: Int, dy: Int): Point = Point(x + dx, y + dy)
def neighbours: Seq[Point] = adjacent.map(delta)
def position(input: Int): Point =
val n = Iterator.iterate(1)(_ + 2).dropWhile(n => n * n < input).next()
val base = input - (n - 2) * (n - 2) - 1
val size = n - 1
val half = size / 2
val quadrant = base / size
val offset = base % size
quadrant match
case 0 => Point(half, offset + 1 - half)
case 1 => Point(half - 1 - offset, half)
case 2 => Point(-half, half - 1 - offset)
case 3 => Point(offset + 1 - half, -half)
end position
def part1(input: Int): Int =
val point = position(input)
point.x.abs + point.y.abs
def part2(input: Int): Int =
def helper(n: Int, squares: Map[Point, Int]): Int =
val point = position(n)
val result = point.neighbours.flatMap(squares.get).sum
if result > input then result
else helper(n + 1, squares.updated(point, result))
helper(2, Map(Point(0, 0) -> 1))
end part2
def main(args: Array[String]): Unit =
val data = 312051
println(part1(data))
println(part2(data))