day06
This commit is contained in:
107
app/src/main/kotlin/day06/Day06.kt
Normal file
107
app/src/main/kotlin/day06/Day06.kt
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
package day06
|
||||||
|
|
||||||
|
import kotlin.io.println
|
||||||
|
import kotlin.collections.listOf
|
||||||
|
|
||||||
|
class Day06 {
|
||||||
|
fun parse(input: String): MutableList<MutableList<Tile>> {
|
||||||
|
return input.lines().map {line -> line.map {Tile.fromChar(it)}.toMutableList() }.toMutableList()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun part1(input: String): Int {
|
||||||
|
var map = parse(input)
|
||||||
|
var running = true
|
||||||
|
var pos = Pair(59, 62)
|
||||||
|
var dir: Direction = Direction.Up
|
||||||
|
var count = 1
|
||||||
|
while (running) {
|
||||||
|
val newX = pos.first + dir.delta.first
|
||||||
|
val newY = pos.second + dir.delta.second
|
||||||
|
if (map[pos.first][pos.second] == Tile.Empty) {
|
||||||
|
map[pos.first][pos.second] = Tile.Visited
|
||||||
|
count += 1
|
||||||
|
}
|
||||||
|
if (newX >= map.count() || newX < 0 || newY >= map[0].count() || newY < 0) {
|
||||||
|
return count
|
||||||
|
}
|
||||||
|
if (map[newX][newY] == Tile.Wall) {
|
||||||
|
dir = Direction.rotate(dir)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
pos = Pair(newX, newY)
|
||||||
|
}
|
||||||
|
throw NotImplementedError()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun runGuard(map: List<List<Tile>>): Boolean {
|
||||||
|
var running = true
|
||||||
|
//var pos = Pair(59, 62)
|
||||||
|
var pos = Pair(6, 4)
|
||||||
|
var dir: Direction = Direction.Up
|
||||||
|
while (running) {
|
||||||
|
val newX = pos.first + dir.delta.first
|
||||||
|
val newY = pos.second + dir.delta.second
|
||||||
|
if (newX >= map.count() || newX < 0 || newY >= map[0].count() || newY < 0) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if (map[newX][newY] == Tile.Start && dir == Direction.Up) {
|
||||||
|
println("$newX, $newY")
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if (map[newX][newY] == Tile.Wall) {
|
||||||
|
dir = Direction.rotate(dir)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
pos = Pair(newX, newY)
|
||||||
|
}
|
||||||
|
throw NotImplementedError()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun part2(input: String): Int {
|
||||||
|
val map = parse(input)
|
||||||
|
var count = 0
|
||||||
|
for (i in 0..<map.count()) {
|
||||||
|
for (j in 0..<map[0].count()) {
|
||||||
|
var modMap = map.map { it.toMutableList() }.toMutableList()
|
||||||
|
modMap[i][j] = Tile.Wall
|
||||||
|
if (runGuard(map)) {
|
||||||
|
count += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum class Direction(val delta: Pair<Int, Int>) {
|
||||||
|
Up(Pair(-1, 0)), Down(Pair(1, 0)), Left(Pair(0,-1)), Right(Pair(0,1));
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun rotate(tile: Direction): Direction {
|
||||||
|
return when (tile) {
|
||||||
|
Up -> Right
|
||||||
|
Right -> Down
|
||||||
|
Down -> Left
|
||||||
|
Left -> Up
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum class Tile {
|
||||||
|
Wall, Visited, Empty, Start;
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun fromChar(char: Char): Tile {
|
||||||
|
return when (char) {
|
||||||
|
'.' -> Empty
|
||||||
|
'#' -> Wall
|
||||||
|
'^' -> Start
|
||||||
|
else -> {
|
||||||
|
throw IllegalArgumentException()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
42
app/src/test/kotlin/day06/Day06Test.kt
Normal file
42
app/src/test/kotlin/day06/Day06Test.kt
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package day06
|
||||||
|
|
||||||
|
import kotlin.test.Test
|
||||||
|
import util.InputDownloader
|
||||||
|
|
||||||
|
class Day06Test {
|
||||||
|
val dayNum = 6
|
||||||
|
val day = Day06()
|
||||||
|
|
||||||
|
val input = InputDownloader().getInput(dayNum)
|
||||||
|
val example = InputDownloader().getExample(dayNum)
|
||||||
|
|
||||||
|
//@Test fun part1Example() {
|
||||||
|
// part1(example, 41)
|
||||||
|
//}
|
||||||
|
|
||||||
|
@Test fun part1Solution() {
|
||||||
|
part1(input, 6041)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun part2Example() {
|
||||||
|
part2(example, 123)
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
@Test fun part2Solution() {
|
||||||
|
part2(input, 4884)
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
fun part1(input: String, expected: Int) {
|
||||||
|
val output = day.part1(input)
|
||||||
|
println("output: $output")
|
||||||
|
assert(output == expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun part2(input: String, expected: Int) {
|
||||||
|
val output = day.part2(input)
|
||||||
|
println("output: $output")
|
||||||
|
assert(output == expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Reference in New Issue
Block a user