First two days
This commit is contained in:
commit
a72952fd1c
28
puzzle1.kts
Normal file
28
puzzle1.kts
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import java.io.File
|
||||||
|
import java.util.Scanner
|
||||||
|
import kotlin.math.abs
|
||||||
|
|
||||||
|
val scanner = Scanner(File("puzzle1input.txt"))
|
||||||
|
val firstList = mutableListOf<Int>()
|
||||||
|
val secondList = mutableListOf<Int>()
|
||||||
|
var secondMap = mutableMapOf<Int, Int>()
|
||||||
|
while (scanner.hasNextLine()) {
|
||||||
|
val line = scanner.nextLine()
|
||||||
|
val numbers = line.split(" ")
|
||||||
|
val firstNumber = numbers[0].toInt()
|
||||||
|
val secondNumber = numbers[1].toInt()
|
||||||
|
firstList.add(firstNumber)
|
||||||
|
secondList.add(secondNumber)
|
||||||
|
secondMap[secondNumber] = (secondMap[secondNumber] ?: 0) + 1
|
||||||
|
}
|
||||||
|
firstList.sort()
|
||||||
|
secondList.sort()
|
||||||
|
var totaldistance = 0
|
||||||
|
var similarityscore = 0
|
||||||
|
for (i in firstList.indices) {
|
||||||
|
val distance = abs(firstList[i] - secondList[i])
|
||||||
|
totaldistance += distance
|
||||||
|
similarityscore += (secondMap[firstList[i]] ?: 0) * firstList[i]
|
||||||
|
}
|
||||||
|
println(totaldistance)
|
||||||
|
println(similarityscore)
|
1000
puzzle1input.txt
Normal file
1000
puzzle1input.txt
Normal file
File diff suppressed because it is too large
Load Diff
48
puzzle2.kts
Normal file
48
puzzle2.kts
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
import java.io.File
|
||||||
|
import java.util.Scanner
|
||||||
|
import javax.print.attribute.standard.NumberUpSupported
|
||||||
|
import kotlin.math.abs
|
||||||
|
|
||||||
|
val scanner = Scanner(File("puzzle2input.txt"))
|
||||||
|
var safelines = 0
|
||||||
|
var dampenedsafelines = 0
|
||||||
|
while (scanner.hasNextLine()) {
|
||||||
|
val line = scanner.nextLine()
|
||||||
|
val numbers = line.split(" ").map { it.toInt() }
|
||||||
|
if (isSafe(numbers)) {
|
||||||
|
safelines++
|
||||||
|
} else if (dampenedSafe(numbers)) {
|
||||||
|
dampenedsafelines++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println(safelines)
|
||||||
|
println(dampenedsafelines + safelines)
|
||||||
|
|
||||||
|
fun isSafe(numbers: List<Int> ): Boolean {
|
||||||
|
var directionality = 0
|
||||||
|
for (i in 1 until numbers.size) {
|
||||||
|
val direction = if (numbers[i] > numbers[i - 1]) 1 else -1
|
||||||
|
if (directionality == 0) {
|
||||||
|
directionality = direction
|
||||||
|
} else if (directionality != direction) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
val difference = abs(numbers[i] - numbers[i - 1])
|
||||||
|
if (difference < 1 || difference > 3) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
fun dampenedSafe(numbers: List<Int>): Boolean {
|
||||||
|
// Brute force approach
|
||||||
|
for (i in numbers.indices) {
|
||||||
|
val copy = numbers.toMutableList()
|
||||||
|
copy.removeAt(i)
|
||||||
|
if (isSafe(copy)) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
1000
puzzle2input.txt
Normal file
1000
puzzle2input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user