Skip to content

Commit 0117fa6

Browse files
committed
refactor(day23): clean up
1 parent 747fcbc commit 0117fa6

File tree

2 files changed

+39
-42
lines changed

2 files changed

+39
-42
lines changed

day23.sc

+11-42
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,10 @@
11
case class Connection(room: Char, hallway: Char, distance: Int, blockedBy: String)
22

3-
val connectionList = List(
4-
Connection('A', '0', 2, "1"),
5-
Connection('A', '1', 1, ""),
6-
Connection('A', '2', 1, ""),
7-
Connection('A', '3', 3, "2"),
8-
Connection('A', '4', 5, "23"),
9-
Connection('A', '5', 7, "234"),
10-
Connection('A', '6', 8, "2345"),
11-
Connection('B', '0', 4, "21"),
12-
Connection('B', '1', 3, "2"),
13-
Connection('B', '2', 1, ""),
14-
Connection('B', '3', 1, ""),
15-
Connection('B', '4', 3, "3"),
16-
Connection('B', '5', 5, "34"),
17-
Connection('B', '6', 6, "345"),
18-
Connection('C', '0', 6, "321"),
19-
Connection('C', '1', 5, "32"),
20-
Connection('C', '2', 3, "3"),
21-
Connection('C', '3', 1, ""),
22-
Connection('C', '4', 1, ""),
23-
Connection('C', '5', 3, "4"),
24-
Connection('C', '6', 4, "45"),
25-
Connection('D', '0', 8, "4321"),
26-
Connection('D', '1', 7, "432"),
27-
Connection('D', '2', 5, "43"),
28-
Connection('D', '3', 3, "4"),
29-
Connection('D', '4', 1, ""),
30-
Connection('D', '5', 1, ""),
31-
Connection('D', '6', 2, "5"),
32-
)
3+
val connectionList = common.loadPackets(List("day23.txt")).map(_.split(" ").toList match {
4+
case room :: hall :: distance :: obstructedBy =>
5+
Connection(room.charAt(0), hall.charAt(0), distance.toInt, obstructedBy.mkString)
6+
})
7+
338
// (room, hallway) -> Connection
349
val connections: Map[(Char, Char), Connection] =
3510
connectionList.groupBy(c => (c.room, c.hallway)).view.mapValues(_.head).toMap
@@ -57,20 +32,19 @@ case class State(rooms: Map[Char, String], hallways: Map[Char, Option[Char]] = h
5732
State(rooms.updated(room, contents.tail), hallways.updated(hallway, Some(contents.head)))
5833
}
5934

60-
def moveFromHallwayToRoom(hallway: Char, room: Char): State = {
61-
assert(hallways(hallway).isDefined)
35+
def moveFromHallwayToRoom(hallway: Char, room: Char): State =
6236
State(rooms.updated(room, room + rooms(room)), hallways.updated(hallway, None))
63-
}
6437
}
6538

6639
class Burrow(depth: Int) extends common.Grid[State] {
40+
val goal = State(rooms.map(room => room -> room.toString * 2).toMap)
41+
6742
override def heuristicDistance(from: State, to: State): Int =
6843
from.rooms.flatMap {
6944
case (room, contents) => contents.filter(_ != room).map(mobility)
7045
}.sum * 4 +
7146
from.hallways.map {
72-
case (hallway, Some(amphipod)) =>
73-
connections((amphipod, hallway)).distance * mobility(amphipod)
47+
case (hallway, Some(amphipod)) => connections((amphipod, hallway)).distance * mobility(amphipod)
7448
case _ => 0
7549
}.sum
7650

@@ -90,27 +64,22 @@ class Burrow(depth: Int) extends common.Grid[State] {
9064
}
9165

9266
override def moveCost(from: State, to: State): Int = {
93-
val hallwayOption = halls.find(hallway => from.hallways(hallway) != to.hallways(hallway))
94-
assert(hallwayOption.isDefined)
95-
val hallway = hallwayOption.get
67+
val hallway = halls.find(hallway => from.hallways(hallway) != to.hallways(hallway)).get
9668
if (from.hallways(hallway).isEmpty) {
9769
// moved from room to hallway
9870
val room = rooms.find(room => from.rooms(room) != to.rooms(room)).get
9971
val extraSteps = depth - to.rooms(room).length
10072
val aphipod = to.hallways(hallway).get
10173
(connections((room, hallway)).distance + extraSteps) * mobility(aphipod)
10274
} else {
103-
val source = from.hallways(hallway)
104-
assert(source.isDefined)
10575
// moved from hallway to room
106-
val aphipod = source.get
76+
val aphipod = from.hallways(hallway).get
10777
val extraSteps = depth - from.rooms(aphipod).length
10878
(connections((aphipod, hallway)).distance + extraSteps) * mobility(aphipod)
10979
}
11080
}
11181
}
11282

113-
val burrowPart1 = new Burrow(1)
11483
val inputPart1 = State(Map('A' -> "CB", 'B' -> "BC", 'C' -> "DA", 'D' -> "DA"))
11584
/*
11685
#############

src/main/resources/day23.txt

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
A 0 2 1
2+
A 1 1
3+
A 2 1
4+
A 3 3 2
5+
A 4 5 23
6+
A 5 7 234
7+
A 6 8 2345
8+
B 0 4 21
9+
B 1 3 2
10+
B 2 1
11+
B 3 1
12+
B 4 3 3
13+
B 5 5 34
14+
B 6 6 345
15+
C 0 6 321
16+
C 1 5 32
17+
C 2 3 3
18+
C 3 1
19+
C 4 1
20+
C 5 3 4
21+
C 6 4 45
22+
D 0 8 4321
23+
D 1 7 432
24+
D 2 5 43
25+
D 3 3 4
26+
D 4 1
27+
D 5 1
28+
D 6 2 5

0 commit comments

Comments
 (0)