-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Toni Day 4 #11
base: master
Are you sure you want to change the base?
Toni Day 4 #11
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package day4 | ||
|
||
import org.junit.Assert.* | ||
import org.junit.Test | ||
import java.io.File | ||
import java.util.* | ||
|
||
class PassphraseValidator { | ||
|
||
fun validI(passphrase: String): Boolean { | ||
val words = passphrase | ||
.split(" ") | ||
return words | ||
.foldIndexed(true) { idx, acc, a -> | ||
if (words.subList(idx + 1, words.size).contains(a)) { | ||
false | ||
} else { | ||
acc | ||
} | ||
} | ||
} | ||
|
||
fun valid(passphrase: String): Boolean { | ||
val list = LinkedList(passphrase.split(" ")) | ||
val element = list.poll() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose you are using 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are droping in the same way, Yes I did with LinkedList to work like (x :xs) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Except, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, In kategory, we have several issues with OutOfMemory with drop in |
||
return validR(list, element) | ||
} | ||
|
||
tailrec fun validR(passphrase: LinkedList<String>, element: String): Boolean = | ||
if (passphrase.isEmpty()) true | ||
else if (passphrase.contains(element)) false | ||
else { | ||
val element = passphrase.poll() | ||
validR(passphrase, element) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No! tailrec is for recursion optimization, tailrec coerce to call the method only in the last operation in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting, I will search if Scala has a |
||
|
||
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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could simplify this like this fun countValid(passphrase: List<String>): Int = passphrase.count(::valid) |
||
|
||
@Test | ||
fun `valid if no words duplicated`() { | ||
assertTrue(valid("aa bb cc dd")) | ||
} | ||
|
||
@Test | ||
fun `invalid if words duplicated`() { | ||
assertFalse(valid("aa bb cc dd aa")) | ||
} | ||
|
||
@Test | ||
fun `is valid - aa and aaa count as different words`() { | ||
assertTrue(valid("aa bb cc dd aaa")) | ||
} | ||
|
||
@Test | ||
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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😏 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💘 |
||
"password", | ||
"aa bb cc dd hh jja", | ||
"aa bb cc dd hh jj aa" | ||
))) | ||
} | ||
|
||
@Test | ||
fun `result for the challenge of day 4 is`() { | ||
val sc = Scanner(File("input")) | ||
val input = mutableListOf<String>() | ||
while (sc.hasNext()) input.add(sc.nextLine()) | ||
assertEquals(386, countValid(input)) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package day4 | ||
|
||
|
||
import org.junit.Assert.* | ||
import org.junit.Test | ||
import java.io.File | ||
import java.util.* | ||
|
||
class PassphraseValidatorPartTwo { | ||
|
||
fun validI(passphrase: String): Boolean { | ||
val words = passphrase | ||
.split(" ") | ||
return words | ||
.foldIndexed(true) { idx, acc, a -> | ||
acc && words.subList(idx + 1, words.size) | ||
.fold(true) { bcc, b -> | ||
bcc && !equalsInAnyOrder(a, b) | ||
} | ||
} | ||
} | ||
|
||
fun containsInAnyOrder(passphrase: List<String>, element: String): Boolean = | ||
passphrase.fold(false) { bcc, b -> | ||
if (equalsInAnyOrder(element, b)) true | ||
else bcc | ||
} | ||
|
||
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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes but I don't what is better for performance? good thought There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know what will be the cost of |
||
|
||
fun valid(passphrase: String): Boolean { | ||
val list = LinkedList(passphrase.split(" ")) | ||
val element = list.poll() | ||
return validR(list, element) | ||
} | ||
|
||
tailrec fun validR(passphrase: LinkedList<String>, element: String): Boolean = | ||
if (passphrase.isEmpty()) true | ||
else if (containsInAnyOrder(passphrase, element)) false | ||
else { | ||
val element = passphrase.poll() | ||
validR(passphrase, element) | ||
} | ||
|
||
fun countValid(passphrase: List<String>): Int = passphrase | ||
.fold(0) { acc, a -> if (valid(a)) acc + 1 else acc } | ||
|
||
@Test | ||
fun `ab contains in any order ba`() { | ||
assertTrue(equalsInAnyOrder("ab", "ba")) | ||
} | ||
|
||
@Test | ||
fun `aa contains in any order aa`() { | ||
assertTrue(equalsInAnyOrder("aa", "aa")) | ||
} | ||
|
||
@Test | ||
fun `ab not contains in any order bc`() { | ||
assertFalse(equalsInAnyOrder("ab", "bc")) | ||
} | ||
|
||
@Test | ||
fun `aa not contains in any order aaa`() { | ||
assertFalse(equalsInAnyOrder("aa", "aaa")) | ||
} | ||
|
||
@Test | ||
fun `aa bb cc dd contains in any order`() { | ||
assertTrue(containsInAnyOrder("aa bb cc dd".split(" "), "aa")) | ||
} | ||
|
||
@Test | ||
fun `valid if no words duplicated`() { | ||
assertTrue(valid("aa bb cc dd")) | ||
} | ||
|
||
@Test | ||
fun `invalid if words duplicated`() { | ||
assertFalse(valid("aa bb cc dd aa")) | ||
} | ||
|
||
@Test | ||
fun `is valid - aa and aaa count as different words`() { | ||
assertTrue(valid("aa bb cc dd aaa")) | ||
} | ||
|
||
@Test | ||
fun `in list with 5 passphrase elements only 2 are valid`() { | ||
assertEquals(2, countValid(listOf( | ||
"ab bc cd de cb", | ||
"miguel say my name lguiem", | ||
"password", | ||
"aa bb cc dd hh jja", | ||
"aa bb cc dd hh jj aa" | ||
))) | ||
} | ||
|
||
@Test | ||
fun `result for the challenge of day 4 is`() { | ||
val sc = Scanner(File("input")) | ||
val input = mutableListOf<String>() | ||
while (sc.hasNext()) input.add(sc.nextLine()) | ||
assertEquals(208, countValid(input)) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess you could simplify this to
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wooooot?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
too much magic behind this methods
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Declarative magic 😉