-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04.sc
49 lines (38 loc) · 1.18 KB
/
day04.sc
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
49
import common.loadPackets
val input = loadPackets(List("day04.txt")).toArray
val ys = input.indices
val xs = input.head.indices
case class Point(x: Int, y: Int):
def onGrid: Boolean = xs.contains(x) && ys.contains(y)
def plus(d: Point, steps: Int = 1): Point = copy(x = x + d.x * steps, y = y + d.y * steps)
def charAt: Char = input(y).charAt(x)
case object Points:
def directions = for
dx <- -1 to 1
dy <- -1 to 1
if dx != 0 || dy != 0
yield Point(dx, dy)
def getLine(start: Point, direction: Point, length: Int): String =
(0 until length)
.map(index => start.plus(direction, index))
.filter(_.onGrid)
.map(_.charAt)
.mkString
def isXMASLine(start: Point, direction: Point) =
getLine(start, direction, 4) == "XMAS"
val part1 = (for
x <- xs
y <- ys
start = Point(x, y)
d <- Points.directions
if isXMASLine(start, d)
yield start).size
def isXMASCross(start: Point): Boolean =
List("MAS", "SAM").contains(getLine(start.plus(Point(-1, -1)), Point(1, 1), 3)) &&
List("MAS", "SAM").contains(getLine(start.plus(Point(-1, 1)), Point(1, -1), 3))
val part2 = (for
x <- xs
y <- ys
start = Point(x, y)
if isXMASCross(start)
yield start).size