Advent of Code 2023 day 15
12/15/2023, 5:00 AMMarcin Wisniowski
12/15/2023, 5:21 AMbj0
12/15/2023, 5:24 AMbj0
12/15/2023, 5:25 AMMarcin Wisniowski
12/15/2023, 5:28 AMMarcin Wisniowski
12/15/2023, 5:29 AMmap/sum
can be simplified into sumOf
Marcin Wisniowski
12/15/2023, 5:30 AMMichael de Kaste
12/15/2023, 5:32 AMMichael de Kaste
12/15/2023, 5:34 AMbj0
12/15/2023, 5:34 AMMarcin Wisniowski
12/15/2023, 5:37 AM'as Int' needs to be at the end of your lineYeah that's fine, the point is that the IDE suggests a quick fix that breaks code that was compiling before
Marcin Wisniowski
12/15/2023, 5:38 AMMarcin Wisniowski
12/15/2023, 5:38 AMMichael de Kaste
12/15/2023, 5:43 AMIf the operation character is a dash (-), go to the relevant box and remove the lens with the given label if it is present in the box. Then, move any remaining lenses as far forward in the box as they can go without changing their order, filling any space made by removing the indicated lens. (If no lens in that box has the given label, nothing happens.)
and
If there is not already a lens in the box with the same label, add the lens to the box immediately behind any lenses already in the box. Don't move any of the other lenses when you do this. If there aren't any lenses in the box, the new lens goes all the way to the front of the box.
luckily it did 😁Jakub Gwóźdź
12/15/2023, 5:43 AMbj0
12/15/2023, 5:47 AMbj0
12/15/2023, 5:52 AMNorbert Kiesel
12/15/2023, 6:04 AMArray(256) { mutableListOf<Lens>() }
or MutableList(256) { mutableListOf<Lens>() }
?Jakub Gwóźdź
12/15/2023, 6:08 AMNorbert Kiesel
12/15/2023, 6:11 AMList
. I used Array
as well, but see most others here do not.andriyo
12/15/2023, 6:20 AMNorbert Kiesel
12/15/2023, 6:24 AMephemient
12/15/2023, 7:00 AMMichael de Kaste
12/15/2023, 7:01 AMephemient
12/15/2023, 7:02 AM0.toInt()
instead of as Int
though, it's safer if anything changesphldavies
12/15/2023, 7:44 AMphldavies
12/15/2023, 7:47 AMPaul Woitaschek
12/15/2023, 7:59 AMephemient
12/15/2023, 8:03 AMLinkedHashMap
doesn't existephemient
12/15/2023, 8:03 AMPaul Woitaschek
12/15/2023, 8:04 AMephemient
12/15/2023, 8:08 AMknthmn
12/15/2023, 8:42 AMMax Thiele
12/15/2023, 9:11 AMWerner Altewischer
12/15/2023, 9:11 AMval difficult = day in setOf(5, 10, 12)
Still figuring out the mathematical formula for that…Werner Altewischer
12/15/2023, 9:14 AMephemient
12/15/2023, 9:15 AMphldavies
12/15/2023, 9:17 AM5 10 12 [11]?
5 2 [-1]?
-3 [-3?]
I'm not sure we can go backwards...Werner Altewischer
12/15/2023, 9:18 AMphldavies
12/15/2023, 9:18 AMWerner Altewischer
12/15/2023, 9:21 AMphldavies
12/15/2023, 9:22 AMMarcin Wisniowski
12/15/2023, 9:24 AMephemient
12/15/2023, 9:29 AMWerner Altewischer
12/15/2023, 9:36 AMval map = data.fold(
defaultMutableMapOf<Long, MutableMap<String, Long>>(
putValueImplicitly = true,
defaultValue = { linkedMapOf() })
) { map, item ->
val (label, rest) = item.split('-', '=')
val boxNumber = hash(label)
if (item.contains('=')) {
val focalLength = rest.toLong()
map[boxNumber][label] = focalLength
} else {
map[boxNumber].remove(label)
}
map
}
This allows you to use a more natural syntax and get rid of all the null checks, getOrPut etc.ephemient
12/15/2023, 9:50 AMList(256) { mutableMapOf() }
and indexing a list is non-nulltodd.ginsberg
12/15/2023, 4:09 PMephemient
12/15/2023, 4:11 PMcompute
is a java.util.Map
interface default method; Kotlin's version is getOrPut
. on JVM there isn't a whole ton of difference aside from that Kotlin doesn't enforce null-safety on the former; on non-JVM you can only use the lattertodd.ginsberg
12/15/2023, 4:12 PMCharles Flynn
12/15/2023, 5:27 PMboxes[label.hash()][label] = instruction.substringAfter("=").toInt()
Charles Flynn
12/15/2023, 5:27 PMtodd.ginsberg
12/15/2023, 5:34 PMtodd.ginsberg
12/15/2023, 5:46 PMNeil Banman
12/15/2023, 7:10 PMThankfully,Also pertaining to some implementations of Day 14. As a hobbyist, nothing I do is ever in production. But again, in the bizarre scenario where you needed to solve AoC puzzles in a production environment, would it be kosher to rely on theprovides amutableMapOf()
which retains insertion order, making part 2 pretty straight forwardLinkedHashMap
Map
or Set
interface and associated factory methods, given that the interface does not guarantee retention of insertion order? Realistically, Kotlin is never going to change its factory methods to an unordered HashMap as it would break a ton of stuff.Jaap Beetstra
12/15/2023, 8:34 PMandriyo
12/15/2023, 8:45 PMAndrei Kovalevsky
12/20/2023, 11:57 PM