Day 7
This commit is contained in:
parent
f2497d4e3a
commit
f248ccde17
2 changed files with 81 additions and 0 deletions
38
day7/part1.kts
Executable file
38
day7/part1.kts
Executable file
|
@ -0,0 +1,38 @@
|
|||
#!/usr/bin/env kotlin
|
||||
|
||||
import java.io.File
|
||||
import java.time.Instant
|
||||
import java.util.*
|
||||
|
||||
val start = Instant.now()
|
||||
|
||||
val tests = File("./input.txt").readLines().map {
|
||||
Pair(it.split(": ")[0], it.split(": ")[1])
|
||||
}.map {
|
||||
Pair(it.first.toLong(), it.second.split(" ").map { n -> n.toLong() })
|
||||
}
|
||||
|
||||
var finalResult = 0L
|
||||
|
||||
tests.forEach { (test, values) ->
|
||||
var results = LinkedList<Long>()
|
||||
results.add(values[0])
|
||||
|
||||
for (i in 1..<values.size) {
|
||||
val iter = results.listIterator();
|
||||
while (iter.hasNext()) {
|
||||
val result = iter.next()
|
||||
|
||||
iter.set(result * values[i])
|
||||
iter.add(result + values[i])
|
||||
}
|
||||
}
|
||||
|
||||
if (test in results) {
|
||||
finalResult += test
|
||||
}
|
||||
}
|
||||
|
||||
val time = (Instant.now().toEpochMilli() - start.toEpochMilli())
|
||||
|
||||
println("Result: ${finalResult}\nTime: ${time}ms")
|
43
day7/part2.kts
Executable file
43
day7/part2.kts
Executable file
|
@ -0,0 +1,43 @@
|
|||
#!/usr/bin/env kotlin
|
||||
|
||||
import java.io.File
|
||||
import java.time.Instant
|
||||
import java.util.*
|
||||
import kotlin.math.*
|
||||
|
||||
infix fun Long.`||`(other: Long)
|
||||
= ((this * 10.0.pow(floor(log10(other.toDouble())) + 1)) + other).toLong()
|
||||
|
||||
val start = Instant.now()
|
||||
|
||||
val tests = File("./input.txt").readLines().map {
|
||||
Pair(it.split(": ")[0], it.split(": ")[1])
|
||||
}.map {
|
||||
Pair(it.first.toLong(), it.second.split(" ").map { n -> n.toLong() })
|
||||
}
|
||||
|
||||
var finalResult = 0L
|
||||
|
||||
tests.forEach { (test, values) ->
|
||||
var results = LinkedList<Long>()
|
||||
results.add(values[0])
|
||||
|
||||
for (i in 1..<values.size) {
|
||||
val iter = results.listIterator();
|
||||
while (iter.hasNext()) {
|
||||
val result = iter.next()
|
||||
|
||||
iter.set(result * values[i])
|
||||
iter.add(result + values[i])
|
||||
iter.add(result `||` values[i])
|
||||
}
|
||||
}
|
||||
|
||||
if (test in results) {
|
||||
finalResult += test
|
||||
}
|
||||
}
|
||||
|
||||
val time = (Instant.now().toEpochMilli() - start.toEpochMilli())
|
||||
|
||||
println("Result: ${finalResult}\nTime: ${time}ms")
|
Loading…
Reference in a new issue