day 10 puzzle 1
This commit is contained in:
parent
0f8f8c3f19
commit
ee3a02060b
@ -9,7 +9,7 @@ I will try and do some problems (probably not all of them) in Scala
|
||||
| Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|
||||
|:-----------------:|:-----------------:|:-----------------:|:-----------------:|:-----------------:|:-----------------:|:-----------------:|
|
||||
| | | | | 1<br>:star::star: | 2<br>:star::star: | 3<br>:star::star: |
|
||||
| 4<br>:star::star: | 5<br>:star::star: | 6<br>:star::star: | 7<br>:star::star: | 8<br>:star::star: | 9<br>:star::star: | 10 |
|
||||
| 4<br>:star::star: | 5<br>:star::star: | 6<br>:star::star: | 7<br>:star::star: | 8<br>:star::star: | 9<br>:star::star: | 10<br>:star: |
|
||||
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
|
||||
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
|
||||
| 25 | | | | | | |
|
74
src/day10/Puzzle1.scala
Normal file
74
src/day10/Puzzle1.scala
Normal file
@ -0,0 +1,74 @@
|
||||
package day10
|
||||
|
||||
import scala.collection.immutable.HashMap
|
||||
import scala.io.{BufferedSource, Source}
|
||||
|
||||
object Puzzle1 {
|
||||
var grid: Array[Array[Byte]] = Array.empty
|
||||
|
||||
// bits: NWSE
|
||||
val TILES: Map[Char, Byte] = HashMap(
|
||||
'|' -> 10,
|
||||
'-' -> 5,
|
||||
'L' -> 9,
|
||||
'J' -> 12,
|
||||
'7' -> 6,
|
||||
'F' -> 3,
|
||||
'.' -> 0,
|
||||
'S' -> 15
|
||||
)
|
||||
|
||||
var height: Int = 0
|
||||
var width: Int = 0
|
||||
var startX: Int = 0
|
||||
var startY: Int = 0
|
||||
|
||||
def loadInput(path: String): Unit = {
|
||||
val source: BufferedSource = Source.fromFile(path)
|
||||
val lines: Array[String] = source.getLines().toArray
|
||||
|
||||
height = lines.length
|
||||
width = if (height == 0) 0 else lines(0).length
|
||||
grid = Array.ofDim(height, width)
|
||||
|
||||
for ((line: String, y: Int) <- lines.zipWithIndex) {
|
||||
for ((c: Char, x: Int) <- line.zipWithIndex) {
|
||||
grid(y)(x) = TILES(c)
|
||||
if (c == 'S') {
|
||||
startX = x
|
||||
startY = y
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
source.close()
|
||||
}
|
||||
|
||||
def calculateMaxDistance(): Int = {
|
||||
val walker1: Walker = new Walker(startX, startY)
|
||||
walker1.walk(grid)
|
||||
val walker2: Walker = new Walker(startX, startY, walker1.getX(), walker1.getY())
|
||||
|
||||
while (walker1.getPos() != walker2.getPos()) {
|
||||
walker2.walk(grid)
|
||||
if (walker1.getPos() == walker2.getPos()) {
|
||||
return walker2.getDistance()
|
||||
}
|
||||
walker1.walk(grid)
|
||||
}
|
||||
return walker1.getDistance()
|
||||
}
|
||||
|
||||
def solve(path: String): Int = {
|
||||
loadInput(path)
|
||||
|
||||
val solution: Int = calculateMaxDistance()
|
||||
|
||||
return solution
|
||||
}
|
||||
|
||||
def main(args: Array[String]): Unit = {
|
||||
val solution: Int = solve("./res/day10/input1.txt")
|
||||
println(solution)
|
||||
}
|
||||
}
|
60
src/day10/Walker.scala
Normal file
60
src/day10/Walker.scala
Normal file
@ -0,0 +1,60 @@
|
||||
package day10
|
||||
|
||||
class Walker {
|
||||
private var x: Int = 0
|
||||
private var y: Int = 0
|
||||
private var lastX: Int = -1
|
||||
private var lastY: Int = -1
|
||||
private var distance: Int = 0
|
||||
|
||||
val OFFSETS: Array[(Int, Int)] = Array(
|
||||
(1, 0), (0, 1), (-1, 0), (0, -1)
|
||||
)
|
||||
|
||||
def this(startX: Int, startY: Int) = {
|
||||
this()
|
||||
x = startX
|
||||
y = startY
|
||||
}
|
||||
|
||||
def this(startX: Int, startY: Int, excludeX: Int, excludeY: Int) = {
|
||||
this(startX, startY)
|
||||
lastX = excludeX
|
||||
lastY = excludeY
|
||||
}
|
||||
|
||||
def getX(): Int = x
|
||||
def getY(): Int = y
|
||||
def getPos(): (Int, Int) = (x, y)
|
||||
def getDistance(): Int = distance
|
||||
|
||||
def walk(grid: Array[Array[Byte]]): Unit = {
|
||||
val height: Int = grid.length
|
||||
val width: Int = if (height == 0) 0 else grid(0).length
|
||||
val (x2: Int, y2: Int) = getNextPos(grid, width, height)
|
||||
lastX = x
|
||||
lastY = y
|
||||
x = x2
|
||||
y = y2
|
||||
distance += 1
|
||||
}
|
||||
|
||||
private def getNextPos(grid: Array[Array[Byte]], width: Int, height: Int): (Int, Int) = {
|
||||
val curTile: Byte = grid(y)(x)
|
||||
for (((dx: Int, dy: Int), i: Int) <- OFFSETS.zipWithIndex) {
|
||||
val x2: Int = x + dx
|
||||
val y2: Int = y + dy
|
||||
if (x2 != lastX || y2 != lastY) {
|
||||
if (0 <= x2 && x2 < width && 0 <= y2 && y2 < height) {
|
||||
val bit: Byte = (1 << i).toByte
|
||||
val bit2: Byte = (1 << ((i + 2) % 4)).toByte
|
||||
if ((curTile & bit) != 0 && (grid(y2)(x2) & bit2) != 0) {
|
||||
return (x2, y2)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw new Exception("Dead-end path")
|
||||
}
|
||||
}
|
12
tests/day10/Puzzle1Test.scala
Normal file
12
tests/day10/Puzzle1Test.scala
Normal file
@ -0,0 +1,12 @@
|
||||
package day10
|
||||
|
||||
import org.scalatest.funsuite.AnyFunSuite
|
||||
|
||||
class Puzzle1Test extends AnyFunSuite {
|
||||
test("Puzzle1.solve 1") {
|
||||
assert(Puzzle1.solve("tests_res/day10/input1.txt") == 4)
|
||||
}
|
||||
test("Puzzle1.solve 2") {
|
||||
assert(Puzzle1.solve("tests_res/day10/input2.txt") == 8)
|
||||
}
|
||||
}
|
5
tests_res/day10/input1.txt
Normal file
5
tests_res/day10/input1.txt
Normal file
@ -0,0 +1,5 @@
|
||||
.....
|
||||
.S-7.
|
||||
.|.|.
|
||||
.L-J.
|
||||
.....
|
5
tests_res/day10/input2.txt
Normal file
5
tests_res/day10/input2.txt
Normal file
@ -0,0 +1,5 @@
|
||||
..F7.
|
||||
.FJ|.
|
||||
SJ.L7
|
||||
|F--J
|
||||
LJ...
|
Loading…
Reference in New Issue
Block a user