@@ -3,14 +3,14 @@ package com.matsemann.adventofcode2021
33import java.util.*
44import kotlin.math.abs
55
6- val hallwayStops = listOf (0 , 1 , 3 , 5 , 7 , 9 , 10 )
7- val doors = listOf (2 , 4 , 6 , 8 )
8- val room1 = listOf (11 , 12 , 13 , 14 )
9- val room2 = listOf (21 , 22 , 23 , 24 )
10- val room3 = listOf (31 , 32 , 33 , 34 )
11- val room4 = listOf (41 , 42 , 43 , 44 )
12- val rooms = listOf (room1, room2, room3, room4)
13- val costs = listOf (1 , 10 , 100 , 1000 )
6+ val hallwayStops = listOf (0 , 1 , 3 , 5 , 7 , 9 , 10 )
7+ val doors = listOf (2 , 4 , 6 , 8 )
8+ val room1 = listOf (11 , 12 , 13 , 14 )
9+ val room2 = listOf (21 , 22 , 23 , 24 )
10+ val room3 = listOf (31 , 32 , 33 , 34 )
11+ val room4 = listOf (41 , 42 , 43 , 44 )
12+ val rooms = listOf (room1, room2, room3, room4)
13+ val costs = listOf (1 , 10 , 100 , 1000 )
1414
1515data class State (val numNumbers : Int , val positions : List <Int >) {
1616
@@ -97,6 +97,25 @@ data class State(val numNumbers: Int, val positions: List<Int>) {
9797 val range = minOf(from + 1 , to).. maxOf(from - 1 , to)
9898 return ! positions.any { pos -> pos in range }
9999 }
100+
101+ fun heuristic (): Int {
102+ return positions.mapIndexed { i, pos ->
103+ val extraDst = i % numNumbers
104+ val goalRoom = i / numNumbers
105+ if (pos > 10 ) { // in a room
106+ val currentRoom = (pos / 10 ) - 1
107+ if (goalRoom == currentRoom) {
108+ 0
109+ } else {
110+ val dst = abs(doors[goalRoom] - doors[currentRoom]) + 1 + extraDst + (pos % 10 )
111+ costs[goalRoom] * dst
112+ }
113+ } else { // outside
114+ val dst = abs(doors[goalRoom] - pos) + 1 + extraDst
115+ costs[goalRoom] * dst
116+ }
117+ }.sum()
118+ }
100119}
101120
102121fun day23_1 (lines : List <String >): Any {
@@ -129,8 +148,8 @@ fun day23_1(lines: List<String>): Any {
129148
130149
131150 val visited = mutableSetOf<State >()
132- val queue = PriorityQueue <Pair <Int , State >>(Comparator .comparing { it.first })
133- queue.offer(0 to start)
151+ val queue = PriorityQueue <Triple <Int , State , Int >>(Comparator .comparing { it.first + it.third })
152+ queue.offer(Triple ( 0 , start, start.heuristic()) )
134153
135154 var count = 0
136155
@@ -149,7 +168,7 @@ fun day23_1(lines: List<String>): Any {
149168
150169 u.second.getPossibleMoves().filterNot { it.second in visited }.forEach { (cost, state) ->
151170 val totalCost = u.first + cost
152- queue.offer(totalCost to state)
171+ queue.offer(Triple ( totalCost, state, state.heuristic()) )
153172 }
154173
155174 }
@@ -159,6 +178,7 @@ fun day23_1(lines: List<String>): Any {
159178
160179
161180fun main () {
181+ for (i in 0 .. 100 )
162182 run (" 1" , fileName = " day23_dummy.txt" , func = ::day23_1)
163183}
164184
@@ -174,6 +194,12 @@ Copied to clipboard!
174194Searched: 467459
175195Done. Took 5960ms to run
176196Result for 1: 46754
197+ Copied to clipboard!
198+
199+ With A*
200+ Searched: 303308
201+ Done. Took 1689ms to run
202+ Result for 1: 46754
177203Copied to clipboard!
178204
179205 */
0 commit comments