Advent of Code 2021 day 1
12/01/2021, 5:00 AMKroppeb
12/01/2021, 5:08 AMwindowed
David Whittaker
12/01/2021, 5:32 AMinput[i] + input[i+1] + input[i+2]
Jakub Gwóźdź
12/01/2021, 5:42 AMDavid Whittaker
12/01/2021, 5:43 AMreturn part1(input.windowed(3, 1).map{it.sum()})
Kroppeb
12/01/2021, 5:48 AMdata.windowed(3) { it.sum() }.windowed(2).count { it.isSorted() }
Jakub Gwóźdź
12/01/2021, 6:01 AM.count { (a, b) -> b > a }
instead of isSorted(). in case there are two exact consecutive numbersDavid Whittaker
12/01/2021, 6:14 AMcount
-- very clever!!Edgars
12/01/2021, 6:29 AMinput.zipWithNext().count { (prev, next) -> next > prev }
. I wonder which is more effective - zipping or windowing. (of course I windowed(3)
for the 2nd part).Piotr KrzemiÅski
12/01/2021, 7:10 AMcak
12/01/2021, 7:19 AMwindowed
- wish I had š! Thanks for sharing!Michael de Kaste
12/01/2021, 7:25 AMobject Day1 : Challenge("--- Day 1: Sonar Sweep ---") {
val parsed = input.lines().map(String::toInt)
override fun part1() = parsed.windowed(2).count { (a, b) -> b > a }
override fun part2() = parsed.windowed(3).windowed(2).count { (a, b) -> b.sum() > a.sum() }
}
I had been using zipwithnext and always wished there was a "zipWithNAmount" function. I always knew windowed exist but it somehow slipped my mind.Kroppeb
12/01/2021, 8:49 AMdata.zip(data.drop(1)).count { (a, b) -> a < b } // part 1
data.zip(data.drop(3)).count { (a, b) -> a < b } // part 2
there isn't an actual need to add the values togetherMichael Bƶiers
12/01/2021, 10:58 AMfun main() {
with(generateSequence(::readLine).map { it.toInt() }.toList()) {
println(countInc(1))
println(countInc(3))
}
}
private fun List<Int>.countInc(d: Int) =
(d until size).count { this[it] > this[it - d] }
Marcin Wisniowski
12/01/2021, 11:04 AMinput.lines().map { it.toInt() }.windowed(2).count { it[1] > it[0] }
Part 2
input.lines().map { it.toInt() }.windowed(4).count { it[3] > it[0] }
Michael Bƶiers
12/01/2021, 11:09 AMfirst()
and last()
š
fun main() {
with(generateSequence(::readLine).map { it.toInt() }.toList()) {
println(countInc(2))
println(countInc(4))
}
}
private fun List<Int>.countInc(d: Int) =
windowed(d).count { it.last() > it.first() }
Marcin Wisniowski
12/01/2021, 11:09 AMPaul Woitaschek
12/01/2021, 1:16 PMMichael Bƶiers
12/01/2021, 1:19 PMPaul Woitaschek
12/01/2021, 1:21 PMMarcin Wisniowski
12/01/2021, 3:31 PMKents
12/01/2021, 4:33 PMtodd.ginsberg
12/01/2021, 4:34 PMephemient
12/01/2021, 6:11 PMtodd.ginsberg
12/01/2021, 7:00 PMTim Schraepen
12/01/2021, 7:45 PM.windowed(2)
and .zipWithNext()
?todd.ginsberg
12/01/2021, 7:46 PMList<T>
vs. Pair<T,T>
Tim Schraepen
12/01/2021, 7:47 PM.zipWithNext()
then, because weāre only supposed to be comparing 2 values.todd.ginsberg
12/01/2021, 7:47 PMList<T>
with only two elements, you can use .first()
and .last()
, which makes it kinda nice.Tim Schraepen
12/01/2021, 7:47 PMfun solve2(depthMeasurementInput: List<String>): Int {
return depthMeasurementInput
.map { it.toDepthMeasurement() }
.toDepthMeasurementSums()
.zipWithNext()
.count { (prev, cur) -> prev < cur }
}
Tim Schraepen
12/01/2021, 7:48 PMtodd.ginsberg
12/01/2021, 7:49 PMreturn
expression and the brackets and just return the function output directly.
fun solve2(): Int = depthMeasurementInput.map { ... }.etc()
Tim Schraepen
12/01/2021, 7:50 PMTim Schraepen
12/01/2021, 7:53 PMMichael Bƶiers
12/01/2021, 8:01 PMfun main() {
val l = generateSequence(::readLine).map { it.toInt() }.toList()
var part1 = 0
var part2 = 0
for (i in l.indices) {
if (i > 0 && l[i] > l[i-1]) part1++
if (i > 2 && l[i] > l[i-3]) part2++
}
println(part1)
println(part2)
}
Jesse Hill
12/01/2021, 8:44 PMcount
. I thought Iād share mine even though itās a bit longer than most.
fun part2(input: List<String>): Int {
val windows = input
.toInts()
.windowed(3, 1)
var previousWindowSize = windows.first().sum()
return windows.fold(0) { total, window ->
val windowSum = window.sum()
val increase = if (windowSum > previousWindowSize) total + 1 else total
previousWindowSize = windowSum
increase
}
}
gnu
12/01/2021, 9:48 PMclass Day01(private val entries: List<Int>) {
companion object {
const val input = "/adventofcode/year2021/Day01.txt"
}
fun part1(): Int =
entries.countIncreasingEntries()
fun part2(): Int =
entries.windowed(3, 1).map { it.sum() }.countIncreasingEntries()
private fun List<Int>.countIncreasingEntries() =
this.zipWithNext().count { it.second > it.first }
}
fun main() {
val entries = Day01::class.java.getResource(Day01.input)!!.readText().trim().split("\n", "\r\n").map { it.toInt() }
val day01 = Day01(entries)
println("Day01::part1 = ${day01.part1()}")
println("Day01::part2 = ${day01.part2()}")
}
gnu
12/01/2021, 9:53 PMimport org.junit.Assert
import org.junit.Test
import kotlin.system.measureNanoTime
class Day01Test {
private val measurements = listOf(199, 200, 208, 210, 200, 207, 240, 269, 260, 263)
private val test = mapOf(
Day01::part1 to (measurements to 7),
Day01::part2 to (measurements to 5)
)
@Test
fun testMySolution() {
for (function in test.keys) {
val data = test[function]!!
val result: Int
val time = measureNanoTime { result = function.invoke(Day01(data.first)) }
println("Day01::${function.name}: ${data.first} -> $result [${time}ns]")
Assert.assertEquals(data.second, result)
}
}
}
And the Unit-test for my solution ...