Skip to content

Commit 747fcbc

Browse files
committed
feat(day23): part 2
1 parent e764bc7 commit 747fcbc

File tree

1 file changed

+52
-44
lines changed

1 file changed

+52
-44
lines changed

day23.sc

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

33
val connectionList = List(
4-
Connection('A', '0', 3, "1"),
5-
Connection('A', '1', 2, ""),
6-
Connection('A', '2', 2, ""),
7-
Connection('A', '3', 4, "2"),
8-
Connection('A', '4', 6, "23"),
9-
Connection('A', '5', 8, "234"),
10-
Connection('A', '6', 9, "2345"),
11-
Connection('B', '0', 5, "21"),
12-
Connection('B', '1', 4, "2"),
13-
Connection('B', '2', 2, ""),
14-
Connection('B', '3', 2, ""),
15-
Connection('B', '4', 4, "3"),
16-
Connection('B', '5', 6, "34"),
17-
Connection('B', '6', 7, "345"),
18-
Connection('C', '0', 7, "321"),
19-
Connection('C', '1', 6, "32"),
20-
Connection('C', '2', 4, "3"),
21-
Connection('C', '3', 2, ""),
22-
Connection('C', '4', 2, ""),
23-
Connection('C', '5', 4, "4"),
24-
Connection('C', '6', 5, "45"),
25-
Connection('D', '0', 9, "4321"),
26-
Connection('D', '1', 8, "432"),
27-
Connection('D', '2', 6, "43"),
28-
Connection('D', '3', 4, "4"),
29-
Connection('D', '4', 2, ""),
30-
Connection('D', '5', 2, ""),
31-
Connection('D', '6', 3, "5"),
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"),
3232
)
3333
// (room, hallway) -> Connection
3434
val connections: Map[(Char, Char), Connection] =
@@ -52,9 +52,9 @@ case class State(rooms: Map[Char, String], hallways: Map[Char, Option[Char]] = h
5252
.filter(connection => !connection.blockedBy.exists(blocker => hallways(blocker).isDefined))
5353
.map(_.hallway)
5454

55-
def moveFromRoomToHallway(room: Char, hallway: Char): State = rooms(room).toList match {
56-
case aphipod :: leftover =>
57-
State(rooms.updated(room, leftover.mkString), hallways.updated(hallway, Some(aphipod)))
55+
def moveFromRoomToHallway(room: Char, hallway: Char): State = {
56+
val contents = rooms(room)
57+
State(rooms.updated(room, contents.tail), hallways.updated(hallway, Some(contents.head)))
5858
}
5959

6060
def moveFromHallwayToRoom(hallway: Char, room: Char): State = {
@@ -63,7 +63,7 @@ case class State(rooms: Map[Char, String], hallways: Map[Char, Option[Char]] = h
6363
}
6464
}
6565

66-
class Burrow extends common.Grid[State] {
66+
class Burrow(depth: Int) extends common.Grid[State] {
6767
override def heuristicDistance(from: State, to: State): Int =
6868
from.rooms.flatMap {
6969
case (room, contents) => contents.filter(_ != room).map(mobility)
@@ -96,33 +96,41 @@ class Burrow extends common.Grid[State] {
9696
if (from.hallways(hallway).isEmpty) {
9797
// moved from room to hallway
9898
val room = rooms.find(room => from.rooms(room) != to.rooms(room)).get
99-
val extraStep = if (to.rooms(room).isEmpty) 1 else 0
99+
val extraSteps = depth - to.rooms(room).length
100100
val aphipod = to.hallways(hallway).get
101-
(connections((room, hallway)).distance + extraStep) * mobility(aphipod)
101+
(connections((room, hallway)).distance + extraSteps) * mobility(aphipod)
102102
} else {
103103
val source = from.hallways(hallway)
104104
assert(source.isDefined)
105105
// moved from hallway to room
106106
val aphipod = source.get
107-
val extraStep = if (from.rooms(aphipod).isEmpty) 1 else 0
108-
(connections((aphipod, hallway)).distance + extraStep) * mobility(aphipod)
107+
val extraSteps = depth - from.rooms(aphipod).length
108+
(connections((aphipod, hallway)).distance + extraSteps) * mobility(aphipod)
109109
}
110110
}
111111
}
112112

113-
val burrow = new Burrow()
114-
115-
val example = State(Map('A' -> "BA", 'B' -> "CD", 'C' -> "BC", 'D' -> "DA"))
116-
val input = State(Map('A' -> "CB", 'B' -> "BC", 'C' -> "DA", 'D' -> "DA"))
113+
val burrowPart1 = new Burrow(1)
114+
val inputPart1 = State(Map('A' -> "CB", 'B' -> "BC", 'C' -> "DA", 'D' -> "DA"))
117115
/*
118116
#############
119117
#...........#
120118
###C#B#D#D###
121119
#B#C#A#A#
122120
#########
123121
*/
124-
val goal = State(rooms.map(room => room -> room.toString * 2).toMap)
125-
val part1 = common.aStarSearch(input, goal, burrow)
126-
127-
122+
val goalPart1 = State(rooms.map(room => room -> room.toString * 2).toMap)
123+
val part1 = common.aStarSearch(inputPart1, goalPart1, new Burrow(2))
128124

125+
val inputPart2 = State(Map('A' -> "CDDB", 'B' -> "BCBC", 'C' -> "DBAA", 'D' -> "DACA"))
126+
/*
127+
#############
128+
#...........#
129+
###C#B#D#D###
130+
#D#C#B#A#
131+
#D#B#A#C#
132+
#B#C#A#A#
133+
#########
134+
*/
135+
val goalPart2 = State(rooms.map(room => room -> room.toString * 4).toMap)
136+
val part2 = common.aStarSearch(inputPart2, goalPart2, new Burrow(4))

0 commit comments

Comments
 (0)