@@ -3,17 +3,20 @@ package jurisk.adventofcode.y2024
3
3
import cats .implicits ._
4
4
import jurisk .adventofcode .y2024 .Advent06 .Block .Empty
5
5
import jurisk .adventofcode .y2024 .Advent06 .Block .Wall
6
- import jurisk .collections .mutable .BitSetKey
7
- import jurisk .collections .mutable .BitSetKeySyntax ._
8
6
import jurisk .collections .mutable .MutableBitSet
9
7
import jurisk .geometry .Coords2D
10
8
import jurisk .geometry .Direction2D
11
9
import jurisk .geometry .Direction2D .CardinalDirection2D
12
10
import jurisk .geometry .Field2D
11
+ import jurisk .geometry .Field2D .coordsToInt
12
+ import jurisk .geometry .Field2D .intToCoords
13
13
import jurisk .geometry .Rotation
14
14
import jurisk .utils .FileInput ._
15
+ import jurisk .utils .FromInt
15
16
import jurisk .utils .Parsing .StringOps
16
17
import jurisk .utils .Simulation
18
+ import jurisk .utils .ToInt
19
+ import jurisk .utils .conversions .syntax ._
17
20
18
21
object Advent06 {
19
22
sealed trait Block extends Product with Serializable
@@ -58,11 +61,7 @@ object Advent06 {
58
61
private def guardsPath (data : Input ): MutableBitSet [Coords2D ] = {
59
62
val (location, field) = data
60
63
61
- implicit val key : BitSetKey [Coords2D ] = new BitSetKey [Coords2D ] {
62
- def toInt (value : Coords2D ): Int = value.x + value.y * field.width
63
- def fromInt (value : Int ): Coords2D =
64
- Coords2D (value % field.width, value / field.width)
65
- }
64
+ implicit val c2i : ToInt [Coords2D ] = coordsToInt(field)
66
65
67
66
val visited = MutableBitSet [Coords2D ](location)
68
67
@@ -88,39 +87,16 @@ object Advent06 {
88
87
location : Coords2D ,
89
88
field : Field2D [Block ],
90
89
): Boolean = {
91
- implicit val coordsBitSetKey : BitSetKey [Coords2D ] =
92
- new BitSetKey [Coords2D ] {
93
- def toInt (value : Coords2D ): Int = value.x + value.y * field.width
94
- def fromInt (value : Int ): Coords2D =
95
- Coords2D (value % field.width, value / field.width)
96
- }
97
-
98
- implicit val directionBitSetKey : BitSetKey [CardinalDirection2D ] =
99
- new BitSetKey [CardinalDirection2D ] {
100
- def toInt (value : CardinalDirection2D ): Int = value match {
101
- case Direction2D .N => 0
102
- case Direction2D .E => 1
103
- case Direction2D .S => 2
104
- case Direction2D .W => 3
105
- }
106
- def fromInt (value : Int ): CardinalDirection2D = value match {
107
- case 0 => Direction2D .N
108
- case 1 => Direction2D .E
109
- case 2 => Direction2D .S
110
- case 3 => Direction2D .W
111
- case _ => s " Invalid value: $value" .fail
112
- }
90
+ implicit val c2i : ToInt [Coords2D ] = coordsToInt(field)
91
+ implicit val g2i : ToInt [Guard ] = (guard : Guard ) => {
92
+ val directionInt = guard.direction match {
93
+ case Direction2D .N => 0
94
+ case Direction2D .E => 1
95
+ case Direction2D .S => 2
96
+ case Direction2D .W => 3
113
97
}
114
98
115
- implicit val guardBitSetKey : BitSetKey [Guard ] = new BitSetKey [Guard ] {
116
- def toInt (guard : Guard ): Int =
117
- guard.location.toInt * 4 + guard.direction.toInt
118
-
119
- def fromInt (value : Int ): Guard = {
120
- val location = (value / 4 ).fromInt[Coords2D ]
121
- val direction = (value % 4 ).fromInt[CardinalDirection2D ]
122
- Guard (location, direction)
123
- }
99
+ guard.location.toInt * 4 + directionInt
124
100
}
125
101
126
102
Simulation
@@ -133,6 +109,8 @@ object Advent06 {
133
109
def part2 (data : Input ): Int = {
134
110
val (location, field) = data
135
111
112
+ implicit val i2c : FromInt [Coords2D ] = intToCoords(field)
113
+
136
114
guardsPath(data)
137
115
.count(c =>
138
116
c != location && wouldLoop(location, field.updatedAtUnsafe(c, Wall ))
0 commit comments