-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11.sc
24 lines (17 loc) · 967 Bytes
/
day11.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
import common.loadPackets
val input = loadPackets(List("day11.txt"))
case class Galaxy(row: Int, col: Int)
val galaxies: List[Galaxy] = input.zipWithIndex.flatMap {
case (line, row) => line.zipWithIndex.flatMap({
case ('#', col) => Some(Galaxy(row, col))
case _ => None
})
}
val emptyCols = input.head.indices.filter(col => !galaxies.exists(_.col == col)).toSet
val emptyRows = input.indices.filter(row => !galaxies.exists(_.row == row)).toSet
def count(set: Set[Int], a: Int, b: Int) = set.intersect((a.min(b) to a.max(b)).toSet).size
def distance(from: Galaxy, to: Galaxy, expansion: Long): Long =
(from.col - to.col).abs + count(emptyCols, from.col, to.col) * (expansion - 1) +
(from.row - to.row).abs + count(emptyRows, from.row, to.row) * (expansion - 1)
val part1 = galaxies.combinations(2).map { case List(a, b) => distance(a, b, 2L) }.sum
val part2 = galaxies.combinations(2).map { case List(a, b) => distance(a, b, 1_000_000L) }.sum