Day 6
This commit is contained in:
parent
3c3eb26235
commit
5bae29ada4
9 changed files with 229 additions and 1 deletions
1
.envrc
Normal file
1
.envrc
Normal file
|
@ -0,0 +1 @@
|
|||
use flake
|
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1 +1,2 @@
|
|||
**/input.txt
|
||||
**/input.txt
|
||||
.direnv
|
8
.idea/.gitignore
vendored
Normal file
8
.idea/.gitignore
vendored
Normal file
|
@ -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
|
9
.idea/aoc2024.iml
Normal file
9
.idea/aoc2024.iml
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
7
.idea/inspectionProfiles/Project_Default.xml
Normal file
7
.idea/inspectionProfiles/Project_Default.xml
Normal file
|
@ -0,0 +1,7 @@
|
|||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="TsLint" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
</profile>
|
||||
</component>
|
6
.idea/misc.xml
Normal file
6
.idea/misc.xml
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
11
.idea/modules.xml
Normal file
11
.idea/modules.xml
Normal file
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/aoc2024.iml" filepath="$PROJECT_DIR$/.idea/aoc2024.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/.direnv/flake-inputs/ab78nf3ad8ly49zfyd3igw3f2n5q7fz4-source/pkgs/development/development.iml" filepath="$PROJECT_DIR$/.direnv/flake-inputs/ab78nf3ad8ly49zfyd3igw3f2n5q7fz4-source/pkgs/development/development.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/.direnv/flake-inputs/ab78nf3ad8ly49zfyd3igw3f2n5q7fz4-source/nixos/nixos.iml" filepath="$PROJECT_DIR$/.direnv/flake-inputs/ab78nf3ad8ly49zfyd3igw3f2n5q7fz4-source/nixos/nixos.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/.direnv/flake-inputs/ab78nf3ad8ly49zfyd3igw3f2n5q7fz4-source/pkgs/pkgs-lib/pkgs-lib.iml" filepath="$PROJECT_DIR$/.direnv/flake-inputs/ab78nf3ad8ly49zfyd3igw3f2n5q7fz4-source/pkgs/pkgs-lib/pkgs-lib.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
70
day6/part1.kts
Executable file
70
day6/part1.kts
Executable file
|
@ -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<MutableList<Char>>()
|
||||
|
||||
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)
|
115
day6/part2.kts
Executable file
115
day6/part2.kts
Executable file
|
@ -0,0 +1,115 @@
|
|||
#!/usr/bin/env kotlin
|
||||
|
||||
import java.io.File
|
||||
|
||||
object Constants {
|
||||
const val CONSECUTIVE_REPEATS_THRESHOLD = 200
|
||||
}
|
||||
|
||||
fun MutableList<MutableList<Char>>.clone(): MutableList<MutableList<Char>> {
|
||||
val grid = mutableListOf<MutableList<Char>>()
|
||||
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<MutableList<Char>>()
|
||||
|
||||
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)
|
Loading…
Reference in a new issue