From 49a76309ff2ffeafc7aa9cc5972ef38f2961059e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20L=C3=B3pez=20Mar=C3=ADn?= Date: Mon, 4 Dec 2017 12:27:56 +0100 Subject: [PATCH] Day 4 --- tonilopezmr/day4/PassphraseValidator.kt | 73 ++++++++++++ .../day4/PassphraseValidatorPartTwo.kt | 108 ++++++++++++++++++ 2 files changed, 181 insertions(+) create mode 100644 tonilopezmr/day4/PassphraseValidator.kt create mode 100644 tonilopezmr/day4/PassphraseValidatorPartTwo.kt diff --git a/tonilopezmr/day4/PassphraseValidator.kt b/tonilopezmr/day4/PassphraseValidator.kt new file mode 100644 index 0000000..3379a0b --- /dev/null +++ b/tonilopezmr/day4/PassphraseValidator.kt @@ -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() + return validR(list, element) + } + + tailrec fun validR(passphrase: LinkedList, element: String): Boolean = + if (passphrase.isEmpty()) true + else if (passphrase.contains(element)) false + else { + val element = passphrase.poll() + validR(passphrase, element) + } + + fun countValid(passphrase: List): Int = passphrase + .fold(0) { acc, a -> if (valid(a)) acc + 1 else acc } + + @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", + "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() + while (sc.hasNext()) input.add(sc.nextLine()) + assertEquals(386, countValid(input)) + } +} \ No newline at end of file diff --git a/tonilopezmr/day4/PassphraseValidatorPartTwo.kt b/tonilopezmr/day4/PassphraseValidatorPartTwo.kt new file mode 100644 index 0000000..59fdfa8 --- /dev/null +++ b/tonilopezmr/day4/PassphraseValidatorPartTwo.kt @@ -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, 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) + } + + fun valid(passphrase: String): Boolean { + val list = LinkedList(passphrase.split(" ")) + val element = list.poll() + return validR(list, element) + } + + tailrec fun validR(passphrase: LinkedList, 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): 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() + while (sc.hasNext()) input.add(sc.nextLine()) + assertEquals(208, countValid(input)) + } +} \ No newline at end of file