Conversation
Cotel
left a comment
There was a problem hiding this comment.
Quite good. Using Kotlin collection operators will help you a lot 👍
|
|
||
| fun valid(passphrase: String): Boolean { | ||
| val list = LinkedList(passphrase.split(" ")) | ||
| val element = list.poll() |
There was a problem hiding this comment.
I suppose you are using LinkedList to get Dequeue behaviour. But did you know you could the same with an immutable list? (Just a bit uglier though, but in Kategory they have implemented methods for (x : xs) haskell notation like)
val list = listOf(1,2,3)
val (head, tail) = Pair(list.first, list.drop(1))
// head -> 1
// tail -> listOf(2,3)There was a problem hiding this comment.
You are droping in the same way, Yes I did with LinkedList to work like (x :xs)
There was a problem hiding this comment.
Except, drop is immutable. It returns a new list.
There was a problem hiding this comment.
Yes, In kategory, we have several issues with OutOfMemory with drop in ListKW.fold for many elements 👎
| else { | ||
| val element = passphrase.poll() | ||
| validR(passphrase, element) | ||
| } |
There was a problem hiding this comment.
Why are you using tailrec? Did the IDE suggested it to you or you knew you had to use it?
There was a problem hiding this comment.
No! tailrec is for recursion optimization, tailrec coerce to call the method only in the last operation in return https://kotlinlang.org/docs/reference/functions.html and https://medium.com/@JorgeCastilloPr/tail-recursion-and-how-to-use-it-in-kotlin-97353993e17f
There was a problem hiding this comment.
Interesting, I will search if Scala has a tailrec reserved word too 👍
| } | ||
|
|
||
| fun countValid(passphrase: List<String>): Int = passphrase | ||
| .fold(0) { acc, a -> if (valid(a)) acc + 1 else acc } |
There was a problem hiding this comment.
You could simplify this like this
fun countValid(passphrase: List<String>): Int = passphrase.count(::valid)| acc | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
I guess you could simplify this to
return words.all { word -> words.count { it == word } <= 1 } There was a problem hiding this comment.
all checks all the elements in a collection fulfill a predicate.
count counts how many items in a collection fulfill a predicate.
There was a problem hiding this comment.
too much magic behind this methods
| fun `in list with 5 passphrase elements only 2 are valid`() { | ||
| assertEquals(2, countValid(listOf( | ||
| "aa bb cc dd cc", | ||
| "miguel say my name miguel", |
|
|
||
| fun equalsInAnyOrder(a: String, b: String): Boolean = a.fold(true) { acc, it -> | ||
| if (a.length != b.length) false else acc && b.contains(it) | ||
| } |
There was a problem hiding this comment.
This is okay but you can check if you strings are anagrams if you sort them and they are equal. That will simplify your solution a lot 👍
There was a problem hiding this comment.
Yes but I don't what is better for performance? good thought
There was a problem hiding this comment.
I don't know what will be the cost of sort integrated method. But right now you have 4 nested loops. @JoseLlorensRipolles and me only have 2, I think, and we are using this approach.
|
Please NOT MERGE THIS PR I want see the @Cotel simplifications another day! |
Toni's solution for day 4 challenge
Description
Easy day, I have created the recursive and iterative solution.
But the Recursive one, has parts with iterative loops :(
any ideas??Solution
Part 1
Part 2
Tests battery
always the same hahah
Happy Advent of Code!