233 tokens in Kotlin for part 1 by Valentin Slawicek
Download solution
import
kotlin
.
math
.
pow
data
class
Card
(
val
cardNumber
:
Int
,
val
winningNumbers
:
Set
<
Int
>
,
val
numbersYouHave
:
Set
<
Int
>
)
fun
main
(
)
{
val
cards
=
generateSequence
(
::
readLine
)
.
map
{
line
->
val
cardNumber
=
line
.
substringBefore
(
"
:
"
)
.
substringAfterLast
(
"
"
)
.
toInt
(
)
val
winningNumbers
=
line
.
substringAfter
(
"
:
"
)
.
substringBefore
(
"
|
"
)
.
split
(
"
"
)
.
mapNotNull
{
number
->
number
.
trim
(
)
.
takeIf
{
it
.
isNotEmpty
(
)
}
?.
toInt
(
)
}
val
numbersYouHave
=
line
.
substringAfter
(
"
|
"
)
.
split
(
"
"
)
.
mapNotNull
{
number
->
number
.
trim
(
)
.
takeIf
{
it
.
isNotEmpty
(
)
}
?.
toInt
(
)
}
Card
(
cardNumber
,
winningNumbers
.
toSet
(
)
,
numbersYouHave
.
toSet
(
)
)
}
println
(
cards
.
sumOf
{
card
->
val
sameNumbers
=
card
.
winningNumbers
.
intersect
(
card
.
numbersYouHave
)
.
count
(
)
if
(
sameNumbers
==
0
)
0
else
(
2
.
toDouble
(
)
.
pow
(
sameNumbers
-
1
)
)
.
toInt
(
)
}
)
}