Advent of Code 2021 day 7
12/07/2021, 5:00 AMDavid Whittaker
12/07/2021, 5:13 AMSimon Nyström
12/07/2021, 5:15 AMSimon Nyström
12/07/2021, 5:15 AMMarcin Wisniowski
12/07/2021, 5:17 AMJakub Gwóźdź
12/07/2021, 5:32 AM(0..x).sum() == (x+1)*x/2
:)Marcin Wisniowski
12/07/2021, 5:33 AMsum()
version is more readable. 😄 But yours is definitely faster.Jakub Gwóźdź
12/07/2021, 5:48 AMMichael Böiers
12/07/2021, 6:09 AMJakub Gwóźdź
12/07/2021, 6:13 AMDan Fingal-Surma
12/07/2021, 6:33 AMPaul Woitaschek
12/07/2021, 7:28 AMPaul Woitaschek
12/07/2021, 7:50 AMephemient
12/07/2021, 7:51 AMephemient
12/07/2021, 7:52 AMephemient
12/07/2021, 7:54 AMMichael de Kaste
12/07/2021, 8:51 AMval parsed = input.split(",").map(String::toInt).sorted()
private val rangeToCheck = parsed.first()..parsed.last()
override fun part1() = solve { it }
override fun part2() = solve { it * (it + 1) / 2 }
private fun solve(mapping: (Int) -> Int) = rangeToCheck.minOf { candidate ->
parsed.map { abs(it - candidate) }.sumOf(mapping)
}
Nate Ridderman
12/07/2021, 2:25 PMminOf
and calculating the distance for each element. It got me thinking: since the distance is a convex function, is there a concise way to stop the traversal when you hit the minimum? My solution is not very concise (takeWhile and a local variable)Paul Woitaschek
12/07/2021, 2:26 PMPaul Woitaschek
12/07/2021, 2:28 PMRoukanken
12/07/2021, 2:33 PMNate Ridderman
12/07/2021, 2:36 PMgnu
12/07/2021, 3:34 PMephemient
12/07/2021, 3:59 PMephemient
12/07/2021, 4:00 PMgnu
12/07/2021, 4:00 PMgnu
12/07/2021, 4:00 PMgnu
12/07/2021, 4:05 PMRoukanken
12/07/2021, 4:06 PMephemient
12/07/2021, 4:13 PMephemient
12/07/2021, 4:14 PMgnu
12/07/2021, 4:16 PMgnu
12/07/2021, 4:19 PMkqr
12/07/2021, 4:38 PMRoukanken
12/07/2021, 4:40 PMRoukanken
12/07/2021, 4:40 PMephemient
12/07/2021, 4:42 PM