Is there any alternative to `String::split` for in...
# advent-of-code
m
Is there any alternative to
String::split
for input parsing in e.g. day 2, day 4 and day 9? In Scala there is pattern patching on string templates and regular expressions. Do we have anything like that in Kotlin?
v
String::split
(or actually
Charsequence::split
) also has a variant taking regex or pattern, isn't that what you are after?
m
Maybe. How would you use it for e.g. day 2 or day 5?
v
I have no idea how it would fit into your code, my crystal ball is broken, so I don't know your code. And even if I would know it, I'd probably not have the time and mood to analyse it where it might fit in. 😉
m
Well, I am not asking how it would fit into my code. I am asking how it would be used to solve Advent Of Code day 2 or day 5.
e
I don't use string split on any of the days mentioned except 4. but I don't understand what you're asking for at all
there are many ways to approach these puzzles, and especially for parsing, there is no single "obvious" way, it depends on which choices you take
if you're asking about pattern matching specifically, just look around. there's plenty of usage of destructuring on string split and regex match results
m
there is also
substringBefore
and `substringAfter`and this blog post
s
But I think I have an idea what the OP means. I too want to parse string templates declaratively. There's this cool example of destructuring the regex in the article linked by @maiatoday:
Copy code
val inputLineRegex = """(\d+),(\d+) -> (\d+),(\d+)""".toRegex()

val (startX, startY, endX, endY) = inputLineRegex
    .matchEntire(s)
    ?.destructured
    ?: throw IllegalArgumentException("Incorrect input line $s")
The problem with this code is,
startX
,
startY
etc. will be
Strings
, and you'll have to manually convert them to
Int
. So the
destructured
property of the
MatchResult
is a step in the right direction, but it's still pretty messy. What I would like to do is to declaratively say: here's a template, here's where the variables in this template are, here are their types, please parse. So hear me out. What if Day 4 part 1 could look like this:
Copy code
fun part1(input: String) = input.lines().count { "${from1:Int},${to1:Int}-${from2:Int},${to2:Int}" ->
    from1 <= from2 && to1 >= to2 || from1 >= from2 && to1 <= to2 }
e
@Sergei Petunin Part of what you are asking for is often called "view patterns" and we're looking to have something like that in the future versions of Kotlin when we add more extensive pattern matching facilities. Finding the syntax for it that is both extensible and easy to understand is quite tricky though. One possible approach is explained here, even though I cannot say the syntax presented there is clear: https://jetbrains.quip.com/6ax2AAr9rt5z/What-might-Kotlin-pattern-matching-look-like-syntactically P.S. Fully custom patterns that can generalize regular expressions (like in your example) would be even cooler, of course, but are way trickier to design in a compiled language.