From 5bae29ada4ca6ea20d09033a662e6cebccfd0a0c Mon Sep 17 00:00:00 2001 From: Tyler Beckman Date: Fri, 6 Dec 2024 00:58:17 -0700 Subject: [PATCH] Day 6 --- .envrc | 1 + .gitignore | 3 +- .idea/.gitignore | 8 ++ .idea/aoc2024.iml | 9 ++ .idea/inspectionProfiles/Project_Default.xml | 7 ++ .idea/misc.xml | 6 + .idea/modules.xml | 11 ++ day6/part1.kts | 70 +++++++++++ day6/part2.kts | 115 +++++++++++++++++++ 9 files changed, 229 insertions(+), 1 deletion(-) create mode 100644 .envrc create mode 100644 .idea/.gitignore create mode 100644 .idea/aoc2024.iml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100755 day6/part1.kts create mode 100755 day6/part2.kts diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore index 2fa6fd5..21cf35b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -**/input.txt \ No newline at end of file +**/input.txt +.direnv \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/aoc2024.iml b/.idea/aoc2024.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/aoc2024.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..9c69411 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..6f29fee --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..5e7a8ca --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/day6/part1.kts b/day6/part1.kts new file mode 100755 index 0000000..f1c9fbe --- /dev/null +++ b/day6/part1.kts @@ -0,0 +1,70 @@ +#!/usr/bin/env kotlin + +import java.io.File + +enum class Direction { + UP, + DOWN, + RIGHT, + LEFT; + + fun rotate90(): Direction { + return when (this) { + UP -> RIGHT + DOWN -> LEFT + RIGHT -> DOWN + LEFT -> UP + } + } +} + +var grid = mutableListOf>() + +var guy = Pair(0, 0); +var direction = Direction.UP + +var i = 0 +File("./input.txt").forEachLine { + var iter = it.chars().iterator() + var j = 0 + grid.add(MutableList(it.length) { + var next = iter.next().toChar() + + if (next == '^') { + guy = Pair(i, j) + next = '.' + } + + j++ + next + }) + + i++ +} + +var steps = 0 +while (true) { + val next = when (direction) { + Direction.UP -> Pair(guy.first - 1, guy.second) + Direction.DOWN -> Pair(guy.first + 1, guy.second) + Direction.RIGHT -> Pair(guy.first, guy.second + 1) + Direction.LEFT -> Pair(guy.first, guy.second - 1) + } + + + try { + if (grid[next.first][next.second] == '#') { + direction = direction.rotate90() + } else if (grid[next.first][next.second] == 'X') { + guy = next + } else { + grid[next.first][next.second] = 'X' + steps++ + guy = next + } + } catch (_: IndexOutOfBoundsException) { + break + } +} + +println(steps) \ No newline at end of file diff --git a/day6/part2.kts b/day6/part2.kts new file mode 100755 index 0000000..6603afd --- /dev/null +++ b/day6/part2.kts @@ -0,0 +1,115 @@ +#!/usr/bin/env kotlin + +import java.io.File + +object Constants { + const val CONSECUTIVE_REPEATS_THRESHOLD = 200 +} + +fun MutableList>.clone(): MutableList> { + val grid = mutableListOf>() + var i = 0; + this.forEach { + grid.add(MutableList(it.size) { j -> + this[i][j] + }) + i++ + } + + return grid +} + +enum class Direction { + UP, + DOWN, + RIGHT, + LEFT; + + fun rotate90(): Direction { + return when (this) { + UP -> RIGHT + DOWN -> LEFT + RIGHT -> DOWN + LEFT -> UP + } + } +} + +var originalGrid = mutableListOf>() + +var originalGuy = Pair(0, 0); +var originalDirection = Direction.UP + +var i = 0 +File("./input.txt").forEachLine { + var iter = it.chars().iterator() + var j = 0 + originalGrid.add(MutableList(it.length) { + var next = iter.next().toChar() + + if (next == '^') { + originalGuy = Pair(i, j) + next = '.' + } + + j++ + next + }) + + i++ +} + +var sum = 0; +originalGrid.forEachIndexed rowLoop@{ i, row -> + row.forEachIndexed colLoop@{ j, _ -> + if (originalGrid[i][j] != '.') return@colLoop + val grid = originalGrid.clone() + grid[i][j] = '#' + + var guy = originalGuy + var direction = originalDirection + var consecutiveRepeats = 0 + + while (true) { + val next = when (direction) { + Direction.UP -> Pair(guy.first - 1, guy.second) + Direction.DOWN -> Pair(guy.first + 1, guy.second) + Direction.RIGHT -> Pair(guy.first, guy.second + 1) + Direction.LEFT -> Pair(guy.first, guy.second - 1) + } + + try { + if (grid[next.first][next.second] == '#') { + direction = direction.rotate90() + } else if (grid[next.first][next.second] == 'X') { + consecutiveRepeats++ + guy = next + } else { + grid[next.first][next.second] = 'X' + consecutiveRepeats = 0 + guy = next + } + } catch (_: IndexOutOfBoundsException) { + break + } + if (consecutiveRepeats >= Constants.CONSECUTIVE_REPEATS_THRESHOLD) { + sum++ + break + } + } + + print( + // Clear line + "\r " + + // Print status + "\rChecked row ${ + i.toString().padStart(3, ' ') + } column ${j.toString().padStart(3, ' ')} (${ + (((i * row.size + j + 1) * 100) / (originalGrid.size * originalGrid[0].size)).toString() + .padStart(3, ' ') + }%)" + ) + } +} + +println("\n" + sum) \ No newline at end of file