First two days

This commit is contained in:
Logiar 2024-12-02 14:06:26 +01:00
commit a72952fd1c
Signed by: Logiar
SSH Key Fingerprint: SHA256:tq77C31em1ZG4oELIHC3k62wq5UzPSXmhqH8g62whIY
4 changed files with 2076 additions and 0 deletions

28
puzzle1.kts Normal file
View 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

File diff suppressed because it is too large Load Diff

48
puzzle2.kts Normal file
View 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

File diff suppressed because it is too large Load Diff