This commit is contained in:
Paul-Henri Froidmont 2024-12-09 01:13:24 +01:00
parent ab68e90706
commit 87c3869717
Signed by: phfroidmont
GPG key ID: BE948AFD7E7873BE
2 changed files with 110 additions and 0 deletions

50
input/day08 Normal file
View file

@ -0,0 +1,50 @@
.............4....O..........w....R...............
.................................f................
..............4...j......NW0......................
....................R..W..........................
...............R..................................
..................................................
v.......................f.......0W................
.....9L............l...N.........w................
....L....9.......ON........8......................
.1.........49L........f..0..N.....................
..........................V...l...................
..........4.......................................
.....................j...................3.....U..
....O.....U.......................................
........J......................l..................
.O....s.Q.......j.....l.....w..........F...q......
..................................................
.U.......................j..8.....................
................U...............................3.
2.............................J............3......
..............................F...................
.....s...R...........J..................F.........
.s......................x..........F.....q........
.......2.....Q........3........x..................
...........v......................u...............
..............v...........n......8............q...
.......f..................8........i..............
.5..................1n..............P.....i.......
............7............Q..................X.....
......5...p....................V..................
.................J..........nx............q.......
.......p............W...........................0.
......2.............p.5.....1....P................
......I.................7.X....i...P..............
............s.....r...w................V..........
...............or...6.................V...........
............................PS.7..................
..........o...........................S...........
...........5..............o..1.......n............
...........I.........r.......7.......6............
.................o.r...........X..................
................................x.........u.......
.........p..Q....2................................
.........v.................S.....................u
I...........................S.....6...............
..................................................
.......I..........................................
..................................................
.......................................6..........
.................................X................

60
src/day08.scala Normal file
View 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)