Skip to content

Commit 3de8c5d

Browse files
committed
Solve 2024 day 8 part 1
1 parent a47d3f2 commit 3de8c5d

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
...............3................d.................
2+
.........................s..7......i.....e........
3+
................C.......................e.........
4+
...............Z.......m....................e.....
5+
....................gC.....q......................
6+
...............Q....s..........................A..
7+
................................s........A........
8+
...........n.....3.C..F......w..m...d.............
9+
..E...............3.....m......d.i................
10+
............f.3.......C....d........A.............
11+
.........Z...........................n..A.........
12+
....Q......p..............g.i.....................
13+
.r......n...Q....p............S.7...........O.....
14+
..........r......K....p.....M..........7....G.....
15+
....................Fs...................G........
16+
..z.........D..........G.g........................
17+
rR.............F................M...............G.
18+
.........I..c.nr...............M................O.
19+
...I..............................................
20+
...................f......I.......................
21+
z.I...............f..K..........0................7
22+
k...................K......u.........O............
23+
.........Q...z.................ga......0.......o..
24+
....E.5..F..................u..b.P......a.1.......
25+
..........k9..................K.........H......1..
26+
.E.........h..........................0......a...H
27+
..........9...h..e........i......M....1...........
28+
.c.............z.......................j.........T
29+
c..D......................Pb.................2....
30+
....................w.y......W......j.........T.2.
31+
......ph...N..................y.......W.t.2.......
32+
............9.................................o..1
33+
.................Vq.......u....Pb.................
34+
.......6R.........................................
35+
........5............w...a.W.............H.j......
36+
......Z.......Y..........V............H..2........
37+
..........D.................v..y.........t...T..o.
38+
.......5...................t......................
39+
........8k...l...............v.........S....T...4.
40+
......6....U......PR........b.B....y..............
41+
..........6.V...U........................L........
42+
.......8.........N....4.Vq.v..t......oJ.....L.....
43+
N...........R.................w.JY................
44+
............N.....................................
45+
.....5Y.....................................j.....
46+
.98........Y.....l.............B...........S...L..
47+
.8...............U...............4................
48+
..................W.........U4....................
49+
...E........l..........B......................L..u
50+
.....D............l....J..q.....................S.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package eu.sim642.adventofcode2024
2+
3+
import eu.sim642.adventofcodelib.Grid
4+
import eu.sim642.adventofcodelib.pos.Pos
5+
import eu.sim642.adventofcodelib.GridImplicits._
6+
7+
object Day8 {
8+
9+
def findAntennas(grid: Grid[Char]): Map[Char, Set[Pos]] = {
10+
(for {
11+
(row, y) <- grid.view.zipWithIndex
12+
(cell, x) <- row.view.zipWithIndex
13+
if cell != '.'
14+
} yield cell -> Pos(x, y)).toSet.groupMap(_._1)(_._2)
15+
}
16+
17+
def antennaAntinodes(antenna1: Pos, antenna2: Pos): Set[Pos] = {
18+
val diff = antenna2 - antenna1
19+
Set(antenna1 - diff, antenna2 + diff)
20+
}
21+
22+
def countAntinodes(grid: Grid[Char]): Int = {
23+
val antennas = findAntennas(grid)
24+
val antinodes = (for {
25+
(_, poss) <- antennas
26+
antenna1 <- poss
27+
antenna2 <- poss
28+
if antenna1 != antenna2
29+
antinode <- antennaAntinodes(antenna1, antenna2)
30+
if grid.containsPos(antinode)
31+
} yield antinode).toSet
32+
antinodes.size
33+
}
34+
35+
def parseGrid(input: String): Grid[Char] = input.linesIterator.map(_.toVector).toVector
36+
37+
lazy val input: String = scala.io.Source.fromInputStream(getClass.getResourceAsStream("day8.txt")).mkString.trim
38+
39+
def main(args: Array[String]): Unit = {
40+
println(countAntinodes(parseGrid(input)))
41+
}
42+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package eu.sim642.adventofcode2024
2+
3+
import Day8._
4+
import org.scalatest.funsuite.AnyFunSuite
5+
6+
class Day8Test extends AnyFunSuite {
7+
8+
val exampleInput =
9+
"""............
10+
|........0...
11+
|.....0......
12+
|.......0....
13+
|....0.......
14+
|......A.....
15+
|............
16+
|............
17+
|........A...
18+
|.........A..
19+
|............
20+
|............""".stripMargin
21+
22+
test("Part 1 examples") {
23+
assert(countAntinodes(parseGrid(exampleInput)) == 14)
24+
}
25+
26+
test("Part 1 input answer") {
27+
assert(countAntinodes(parseGrid(input)) == 357)
28+
}
29+
}

0 commit comments

Comments
 (0)