33
loading...
This website collects cookies to deliver better user experience
src
folder as a source set to put the code directly there. We copied input files, like src/day1/input.txt
, to the source folder for convenience. You can find the solutions in this project.1721
979
366
299
675
1456
readLines()
function for reading a list of lines from a given file:File("src/day1/input.txt").readLines()
readLines()
returns a list of Strings, and we convert it to a list of numbers:import java.io.File
fun main() {
val numbers = File("src/day1/input.txt")
.readLines()
.map(String::toInt)
}
main
function, the entry point for your program. When you start typing, IntelliJ IDEA imports the java.io.File
automatically.for (first in numbers) {
for (second in numbers) {
if (first + second == 2020) {
println(first * second)
return
}
}
}
main
, so return
returns from main
when the required numbers are found.for (first in numbers) {
for (second in numbers) {
for (third in numbers) {
if (first + second + third == 2020) {
println(first * second * third)
return
}
}
}
}
val complements = numbers.associateBy { 2020 - it }
associateBy
function to build the map. Its lambda argument returns a key in this map, by which the list element is getting stored. For the sample input it’ll be the following map:numbers: [1721, 979, 366, 299, 675, 1456]
complements map: {299=1721, 1041=979, 1654=366, 1721=299, 1345=675, 564=1456}
1721
from the list is present in the complements
map as a key: 1721=299
, which means it’s the complement for the number 299
, and they sum to 2020
.val pair = numbers.mapNotNull { number ->
val complement = complements[number]
if (complement != null) Pair(number, complement) else null
}.firstOrNull()
mapNotNull
, which transforms each element in a list and filters out all the resulting null
s. It’s shorthand for calling first map
, and then filterNotNull
on the result.firstOrNull
returns the first element in the list or null
if the list is empty. Kotlin standard library often uses the OrNull
suffix to mark functions returning null
on failure rather than throwing an exception (like elementAtOrNull
, singleOrNull
, or maxOrNull
).mapNotNull
and first(OrNull)
with one function call: firstNotNullOf(OrNull)
.println(pair?.let { (a, b) -> a * b })
pair
variable contains a nullable Pair
of two numbers and is null
if the initial list contains no numbers that sum up to 2020. We use safe access ?.
together with the let
function and destructuring in a lambda syntax to display the result in case pair
is not null
.fun List<Int>.findPairOfSum(sum: Int): Pair<Int, Int>? {
// Map: sum - x -> x
val complements = associateBy { sum - it }
return firstNotNullOfOrNull { number ->
val complement = complements[number]
if (complement != null) Pair(number, complement) else null
}
}
firstNotNullOfOrNull
function.findPairOfSum
to build a helper map that stores the complement pair of values for each number which together with this number sums up to 2020:// Map: x -> (y, z) where y + z = 2020 - x
val complementPairs: Map<Int, Pair<Int, Int>?> =
numbers.associateWith { numbers.findPairOfSum(2020 - it) }
numbers: [1721, 979, 366, 299, 675, 1456]
complement pairs: {1721=null, 979=(366, 675), 366=(979, 675), 299=null, 675=(979, 366), 1456=null}
firstNotNullOfOrNull
function:fun List<Int>.findTripleOfSum(): Triple<Int, Int, Int>? =
firstNotNullOfOrNull { x ->
findPairOfSum(2020 - x)?.let { pair ->
Triple(x, pair.first, pair.second)
}
}
println(triple?.let { (x, y, z) -> x * y * z} )