Day 3
This commit is contained in:
81
src/main/kotlin/com/basdado/adventofcode/Day3.kt
Normal file
81
src/main/kotlin/com/basdado/adventofcode/Day3.kt
Normal file
@@ -0,0 +1,81 @@
|
||||
package com.basdado.adventofcode
|
||||
|
||||
import java.util.stream.Collectors
|
||||
import java.util.stream.Stream
|
||||
import java.util.stream.StreamSupport
|
||||
|
||||
fun main() {
|
||||
|
||||
val day = Day3()
|
||||
day.puzzle1()
|
||||
day.puzzle2()
|
||||
}
|
||||
|
||||
class Day3 {
|
||||
|
||||
fun puzzle1() {
|
||||
val grid = FabricGrid()
|
||||
|
||||
lines("/day/3/input.txt")
|
||||
.forEach { grid.mark(it) }
|
||||
|
||||
println(
|
||||
grid.stream()
|
||||
.filter { it.size > 1}
|
||||
.collect(Collectors.counting())
|
||||
)
|
||||
}
|
||||
|
||||
fun puzzle2() {
|
||||
|
||||
val grid = FabricGrid()
|
||||
|
||||
lines("/day/3/input.txt")
|
||||
.forEach { grid.mark(it) }
|
||||
|
||||
val potentials = grid.stream().flatMap { it.stream() }.distinct().collect(Collectors.toSet())
|
||||
|
||||
grid.stream()
|
||||
.filter{ it.size > 1 }
|
||||
.flatMap { it.stream() }
|
||||
.distinct()
|
||||
.forEach { potentials.remove(it) }
|
||||
|
||||
println(potentials)
|
||||
}
|
||||
}
|
||||
|
||||
class FabricGrid: Iterable<List<Int>> {
|
||||
|
||||
private val grid = Array<MutableList<Int>>(1000 * 1000) { mutableListOf() }
|
||||
|
||||
fun getIdsAt(x: Int, y: Int): List<Int> = grid[x + y * 1000]
|
||||
fun addId(x: Int, y: Int, id: Int) = grid[x + y * 1000].add(id)
|
||||
|
||||
override fun iterator(): Iterator<List<Int>> = grid.iterator()
|
||||
fun stream(): Stream<List<Int>> = StreamSupport.stream(spliterator(), false)
|
||||
|
||||
fun mark(fabricPieceDefinition: String) {
|
||||
|
||||
if (fabricPieceDefinition.isEmpty()) {
|
||||
return
|
||||
}
|
||||
|
||||
val id = fabricPieceDefinition.substring(1, fabricPieceDefinition.indexOf('@')).trim().toInt()
|
||||
val x = fabricPieceDefinition.substring(fabricPieceDefinition.indexOf('@') + 1, fabricPieceDefinition.indexOf(',')).trim().toInt()
|
||||
val y = fabricPieceDefinition.substring(fabricPieceDefinition.indexOf(',') + 1, fabricPieceDefinition.indexOf(':')).trim().toInt()
|
||||
val width = fabricPieceDefinition.substring(fabricPieceDefinition.indexOf(':') + 1, fabricPieceDefinition.indexOf('x')).trim().toInt()
|
||||
val height = fabricPieceDefinition.substring(fabricPieceDefinition.indexOf('x') + 1).trim().toInt()
|
||||
|
||||
mark(id, x, y, width, height)
|
||||
}
|
||||
|
||||
fun mark(id: Int, x: Int, y: Int, width: Int, height: Int) {
|
||||
|
||||
for (j in y until (y + height)) {
|
||||
for (i in x until (x + width)) {
|
||||
addId(i, j, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
src/main/kotlin/com/basdado/adventofcode/Utils.kt
Normal file
11
src/main/kotlin/com/basdado/adventofcode/Utils.kt
Normal file
@@ -0,0 +1,11 @@
|
||||
package com.basdado.adventofcode
|
||||
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Paths
|
||||
import java.util.stream.Stream
|
||||
|
||||
fun lines(resourceFile: String): Stream<String> {
|
||||
return Files.lines(Paths.get(Utils::class.java.getResource(resourceFile).toURI()))
|
||||
}
|
||||
|
||||
class Utils;
|
||||
1295
src/main/resources/day/3/input.txt
Normal file
1295
src/main/resources/day/3/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user