diff --git a/app/src/main/kotlin/day06/Day06.kt b/app/src/main/kotlin/day06/Day06.kt new file mode 100644 index 0000000..8ca56e5 --- /dev/null +++ b/app/src/main/kotlin/day06/Day06.kt @@ -0,0 +1,107 @@ +package day06 + +import kotlin.io.println +import kotlin.collections.listOf + +class Day06 { + fun parse(input: String): MutableList> { + 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>): 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..) { + 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() + } + } + } + } +} diff --git a/app/src/test/kotlin/day06/Day06Test.kt b/app/src/test/kotlin/day06/Day06Test.kt new file mode 100644 index 0000000..ddd3de5 --- /dev/null +++ b/app/src/test/kotlin/day06/Day06Test.kt @@ -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) + } +} +