1
1
case class Connection (room : Char , hallway : Char , distance : Int , blockedBy : String )
2
2
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
+
33
8
// (room, hallway) -> Connection
34
9
val connections : Map [(Char , Char ), Connection ] =
35
10
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
57
32
State (rooms.updated(room, contents.tail), hallways.updated(hallway, Some (contents.head)))
58
33
}
59
34
60
- def moveFromHallwayToRoom (hallway : Char , room : Char ): State = {
61
- assert(hallways(hallway).isDefined)
35
+ def moveFromHallwayToRoom (hallway : Char , room : Char ): State =
62
36
State (rooms.updated(room, room + rooms(room)), hallways.updated(hallway, None ))
63
- }
64
37
}
65
38
66
39
class Burrow (depth : Int ) extends common.Grid [State ] {
40
+ val goal = State (rooms.map(room => room -> room.toString * 2 ).toMap)
41
+
67
42
override def heuristicDistance (from : State , to : State ): Int =
68
43
from.rooms.flatMap {
69
44
case (room, contents) => contents.filter(_ != room).map(mobility)
70
45
}.sum * 4 +
71
46
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)
74
48
case _ => 0
75
49
}.sum
76
50
@@ -90,27 +64,22 @@ class Burrow(depth: Int) extends common.Grid[State] {
90
64
}
91
65
92
66
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
96
68
if (from.hallways(hallway).isEmpty) {
97
69
// moved from room to hallway
98
70
val room = rooms.find(room => from.rooms(room) != to.rooms(room)).get
99
71
val extraSteps = depth - to.rooms(room).length
100
72
val aphipod = to.hallways(hallway).get
101
73
(connections((room, hallway)).distance + extraSteps) * mobility(aphipod)
102
74
} else {
103
- val source = from.hallways(hallway)
104
- assert(source.isDefined)
105
75
// moved from hallway to room
106
- val aphipod = source .get
76
+ val aphipod = from.hallways(hallway) .get
107
77
val extraSteps = depth - from.rooms(aphipod).length
108
78
(connections((aphipod, hallway)).distance + extraSteps) * mobility(aphipod)
109
79
}
110
80
}
111
81
}
112
82
113
- val burrowPart1 = new Burrow (1 )
114
83
val inputPart1 = State (Map ('A' -> " CB" , 'B' -> " BC" , 'C' -> " DA" , 'D' -> " DA" ))
115
84
/*
116
85
#############
0 commit comments