Day 08
This commit is contained in:
parent
ab68e90706
commit
87c3869717
2 changed files with 110 additions and 0 deletions
60
src/day08.scala
Normal file
60
src/day08.scala
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
package aoc
|
||||
package day08
|
||||
|
||||
val dayNumber = "08"
|
||||
|
||||
@main def part1: Unit =
|
||||
println(part1(loadInput(dayNumber)))
|
||||
|
||||
@main def part2: Unit =
|
||||
println(part2(loadInput(dayNumber)))
|
||||
|
||||
def part1(input: String): String =
|
||||
val (gridMap, antennas) = parseInput(input)
|
||||
antennas.toList
|
||||
.flatMap(positions =>
|
||||
positions.combinations(2).flatMap { case List((ax, ay), (bx, by)) =>
|
||||
val (diffX, diffY) = (bx - ax, by - ay)
|
||||
List((ax - diffX, ay - diffY), (bx + diffX, by + diffY))
|
||||
}
|
||||
)
|
||||
.distinct
|
||||
.count(gridMap.get(_).isDefined)
|
||||
.toString
|
||||
|
||||
def part2(input: String): String =
|
||||
val (gridMap, antennas) = parseInput(input)
|
||||
antennas.toList
|
||||
.flatMap(positions =>
|
||||
positions.combinations(2).flatMap { case List((ax, ay), (bx, by)) =>
|
||||
val (diffX, diffY) = (bx - ax, by - ay)
|
||||
LazyList
|
||||
.iterate(ax -> ay)((x, y) => (x - diffX, y - diffY))
|
||||
.takeWhile(gridMap.get(_).isDefined) ++
|
||||
LazyList
|
||||
.iterate(bx -> by)((x, y) => (x + diffX, y + diffY))
|
||||
.takeWhile(gridMap.get(_).isDefined) ++
|
||||
List((ax, ay), (bx, by))
|
||||
}
|
||||
)
|
||||
.distinct
|
||||
.length
|
||||
.toString
|
||||
|
||||
def parseInput(
|
||||
input: String
|
||||
): (Map[(Int, Int), Char], List[List[(Int, Int)]]) =
|
||||
val grid = input.split("\n")
|
||||
val coords =
|
||||
for
|
||||
y <- 0 until grid.length
|
||||
x <- 0 until grid.head.length
|
||||
yield (x, y) -> grid(y)(x)
|
||||
val gridMap = coords.toMap
|
||||
val antennas = coords
|
||||
.filter(_._2 != '.')
|
||||
.groupMap(_._2)(_._1)
|
||||
.values
|
||||
.map(_.toList)
|
||||
.toList
|
||||
(gridMap, antennas)
|
||||
Loading…
Add table
Add a link
Reference in a new issue