Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class LavaLagoon(instructions: List<String>) {
- private val trenches: List<Pair<LongRange, LongRange>>
- init {
- val dugOut = mutableListOf<Pair<LongRange, LongRange>>()
- var runningPos = 0L to 0L
- instructions.forEach {
- val instruction = it.split(" ")
- val length = instruction[2].substring(2, 7).toLong(radix = 16)
- val nextPos = when (instruction[2][7]) {
- '0' -> runningPos.first to runningPos.second + length
- '1' -> runningPos.first + length to runningPos.second
- '2' -> runningPos.first to runningPos.second - length
- else -> runningPos.first - length to runningPos.second
- }
- val yRange = minOf(runningPos.first, nextPos.first)..maxOf(runningPos.first, nextPos.first)
- val xRange = minOf(runningPos.second, nextPos.second)..maxOf(runningPos.second, nextPos.second)
- dugOut += yRange to xRange
- runningPos = nextPos
- }
- trenches = dugOut
- System.out.println("mapping done")
- }
- fun countHoles(): Long {
- val yRange = trenches.map { it.first.first }.min()!! .. trenches.map { it.first.last }.max()!!
- val xRange = trenches.map { it.second.first }.min()!! .. trenches.map { it.second.last }.max()!!
- var lastLine = 0L
- return yRange.map { y ->
- var isHole = false
- var count = 0L
- var lastDirection = 0
- if (trenches.any {
- it.first.first == y || it.first.first == y - 1 || it.first.first == y + 1 ||
- it.first.last == y || it.first.last == y - 1 || it.first.last == y + 1
- }) {
- val filteredTrenches = trenches.filter { it.first.contains(y) || it.first.contains(y-1) || it.first.contains(y+1) }
- println("$y/${yRange.last}")
- xRange.forEach { x ->
- val trenchAbove = filteredTrenches.any { it.first.contains(y - 1) && it.second.contains(x) }
- val isTrench = filteredTrenches.any { it.first.contains(y) && it.second.contains(x) }
- val trenchBelow = filteredTrenches.any { it.first.contains(y + 1) && it.second.contains(x) }
- if (isTrench || isHole) count++
- if (isTrench && trenchAbove && trenchBelow) {
- isHole = !isHole
- } else if (isTrench && (trenchAbove || trenchBelow)) {
- val direction = if (trenchAbove) 1 else 2
- when (lastDirection) {
- 0 -> {
- isHole = !isHole
- lastDirection = direction
- }
- direction -> {
- isHole = !isHole
- lastDirection = 0
- }
- else -> lastDirection = 0
- }
- }
- }
- lastLine = count
- count
- } else {
- lastLine
- }
- }.sum()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement