<Advent of Code 2021 day 10> :thread:
# advent-of-code
a
d
I'm feeling like we've done something like this before in prior years.
s
day 1 2015 deals with counting parentheses at least
m
💯 1
👍 1
n
My approach was to repeatedly remove direct pairs from the line until it no longer changes. After that, removing all opening chars gives the char for part 1, and processing the remaining chars in reverse order gives the solution for part 2. Not the most efficient way, but still runs in < 100ms. Details in https://github.com/nkiesel/AdventOfCode2021/blob/Norbert/src/test/kotlin/Day10.kt
m
Not very pretty. Got the job done, but took me very long since I took a completely wrong and stupid approach first. Should prepare coffee for the next challenge. 🙂 https://github.com/MikeEnRegalia/AdventOfCode2021/blob/main/kotlin/src/main/kotlin/Day10.kt
d
@Marcin Wisniowski - ooh TIL
mapNotNull
- nice!
K 1
@Michael Böiers - I also used
when
for the scoring table, but I like Marcin's approach of using a map so it can be indexed. I'll have to remember that for next opportunity.
Sealed interface worked nicely here IMO
Got a wrong answer on part 2 first due to integer overflow
🙌 3
t
I used sealed classes as well. And ran into the overflow as well... Anyway, I'm actually really happy with my solution. https://github.com/Sydenth/aoc-21/blob/main/src/year2021/day10/Day10.kt
e
m
@ephemient
private val pairs = "([{<".zip(")]}>").toMap()
🆒
👀 3
m
Here’s my improved solution, had to try to implement it with a stack after having realized my error. 🙂 https://github.com/MikeEnRegalia/AdventOfCode2021/blob/main/kotlin/src/main/kotlin/Day10Proper.kt
👍 1
m
wish I could have used the sealedmap as I said elsewhere in the channel, but this works fine too. Single parse over the input to split the lines into incomplete and illegal versions and then do the proper math https://github.com/mdekaste/AdventOfCode2021/blob/main/src/main/kotlin/year2021/day10/Day10.kt
p
@Michael de Kaste what's a sealed map?
👀 1
m
well it doesn't exist yet, thats the problem 😄
p
Yeah right but what should it do?
m
A sealed class knows all its subclasses because of the promise that you can only add those subclasses within the same file. This means that we could technically treat a sealed class as an enum. If you have a big list of sealed class/interface, you know that the elements of the list are ALWAYS a subtype of that sealed class/interface. You could always do
list.filterIsInstance(YourSealedClass.SubType1::class.java)
but that means that every time you go over the list, you're rechecking types. So your best bet would be to group this list of sealed classes by its type. If I know I have a Map<SealedClassType, SealedClassInstance>, I should just be able to do map[SealedClassType] and get the instance in the correct subtype; this is all just me thinking out loud but it just feels like sealed classes could have a very efficient map implementation.
l
t
I have the day off today and watched F1 practice ended up going to Costco (which always takes about 3x longer than I think it will) and am just getting around to posting my solution. • CodeBlog
👍 2
p