-
Notifications
You must be signed in to change notification settings - Fork 1
/
Part1.scala
70 lines (53 loc) · 1.84 KB
/
Part1.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.security.MessageDigest
import scala.collection.mutable.Queue
import scala.collection.mutable.ListBuffer
case class State(turn: Int, location: Int, key: String)
object Part1 {
val hasher = MessageDigest.getInstance("MD5")
def isOpenDoor(c: Char): Boolean = {
c == 'b' || c == 'c' || c == 'd' || c == 'e' || c == 'f'
}
def adjacents(s: State, locks: String): List[State] = {
val adjacents = new ListBuffer[State]()
val newTurn = s.turn + 1
val canGoUp = (s.location > 3) && isOpenDoor(locks(0))
if (canGoUp) {
adjacents += State(newTurn, s.location - 4, s.key + "U")
}
val canGoDown = (s.location < 12) && isOpenDoor(locks(1))
if (canGoDown) {
adjacents += State(newTurn, s.location + 4, s.key + "D")
}
val canGoLeft = ((s.location % 4) != 0) && isOpenDoor(locks(2))
if (canGoLeft) {
adjacents += State(newTurn, s.location - 1, s.key + "L")
}
val canGoRight = (s.location != 3 && s.location != 7 && s.location != 11) && isOpenDoor(locks(3))
if (canGoRight) {
adjacents += State(newTurn, s.location + 1, s.key + "R")
}
adjacents.toList
}
def hashFor(s: String) : String = {
hasher.digest(s.getBytes).map("%02x".format(_)).mkString
}
def solve(q: Queue[State], base: String): State = {
while (true) {
val currState = q.dequeue()
if (currState.location == 15) return currState
val locks = hashFor(base + currState.key)
adjacents(currState, locks).foreach(x => q.enqueue(x))
}
// satisfy the type checker lol…
State(-1, -1, "not really a state")
}
def main(args: Array[String]): Unit = {
val base = "bwnlcvfs"
val queue = new Queue[State]()
val initState = State(0, 0, "")
queue.enqueue(initState)
val solution = solve(queue, base)
println("Part 1")
println(solution.key)
}
}