Skip to content

Commit 88c706e

Browse files
committed
Solution 2015-05 (Doesn't He Have Intern-Elves For This?)
1 parent a550438 commit 88c706e

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package de.ronny_h.aoc.year2015.day05
2+
3+
import de.ronny_h.aoc.AdventOfCode
4+
5+
fun main() = InternElves().run(255, 55)
6+
7+
class InternElves : AdventOfCode<Int>(2015, 5) {
8+
override fun part1(input: List<String>): Int = input
9+
.filter(::hasThreeVowels)
10+
.filter(::hasALetterTwiceInARow)
11+
.count(::doesNotContainBadStrings)
12+
13+
private val vowels = setOf('a', 'e', 'i', 'o', 'u')
14+
private val badStrings = setOf("ab", "cd", "pq", "xy")
15+
16+
fun hasThreeVowels(string: String) = string.filter { it in vowels }.length >= 3
17+
18+
fun hasALetterTwiceInARow(string: String): Boolean {
19+
for (i in string.indices) {
20+
if (i != string.lastIndex && string[i] == string[i + 1]) return true
21+
}
22+
return false
23+
}
24+
25+
fun doesNotContainBadStrings(string: String) = badStrings.all { !string.contains(it) }
26+
27+
override fun part2(input: List<String>): Int = input
28+
.filter(::containsAPairOfLettersAtLeastTwiceWithoutOverlapping)
29+
.count(::containsALetterRepeatingWithExactlyOneLetterInBetween)
30+
31+
fun containsAPairOfLettersAtLeastTwiceWithoutOverlapping(string: String): Boolean {
32+
for (i in string.indices) {
33+
if (i < string.lastIndex - 1) {
34+
val pair = "${string[i]}${string[i + 1]}"
35+
if (string.findAnyOf(listOf(pair), i + 2) != null) {
36+
return true
37+
}
38+
}
39+
}
40+
return false
41+
}
42+
43+
fun containsALetterRepeatingWithExactlyOneLetterInBetween(string: String): Boolean {
44+
for (i in string.indices) {
45+
if (i < string.lastIndex - 1) {
46+
if (string[i] == string[i + 2]) return true
47+
}
48+
}
49+
return false
50+
}
51+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package de.ronny_h.aoc.year2015.day05
2+
3+
import io.kotest.core.spec.style.StringSpec
4+
import io.kotest.matchers.shouldBe
5+
6+
class InternElvesTest : StringSpec({
7+
8+
val internElves = InternElves()
9+
10+
"part 1: A nice string contains at least three vowels" {
11+
internElves.hasThreeVowels("aei") shouldBe true
12+
internElves.hasThreeVowels("ugknbfddgicrmopn") shouldBe true
13+
}
14+
15+
"part 1: A nice string contains at least one letter that appears twice in a row" {
16+
internElves.hasALetterTwiceInARow("ugknbfddgicrmopn") shouldBe true
17+
}
18+
19+
"part 1: A nice string does not contain the strings ab, cd, pq, or xy" {
20+
internElves.doesNotContainBadStrings("ugknbfddgicrmopn") shouldBe true
21+
}
22+
23+
"part 1: The number of nice strings" {
24+
internElves.part1(listOf("ugknbfddgicrmopn")) shouldBe 1
25+
internElves.part1(listOf("aaa")) shouldBe 1
26+
internElves.part1(listOf("jchzalrnumimnmhp", "haegwjzuvuyypxyu", "dvszwmarrgswjxmb")) shouldBe 0
27+
}
28+
29+
"part 2: A nice string contains a pair of any two letters that appears at least twice in the string without overlapping" {
30+
internElves.containsAPairOfLettersAtLeastTwiceWithoutOverlapping("xyxy") shouldBe true
31+
internElves.containsAPairOfLettersAtLeastTwiceWithoutOverlapping("aabcdefgaa") shouldBe true
32+
internElves.containsAPairOfLettersAtLeastTwiceWithoutOverlapping("aaa") shouldBe false
33+
}
34+
35+
"part 2: A nice string contains at least one letter which repeats with exactly one letter between them" {
36+
internElves.containsALetterRepeatingWithExactlyOneLetterInBetween("xyx") shouldBe true
37+
internElves.containsALetterRepeatingWithExactlyOneLetterInBetween("abcdefeghi") shouldBe true
38+
internElves.containsALetterRepeatingWithExactlyOneLetterInBetween("abcdefeghi") shouldBe true
39+
internElves.containsALetterRepeatingWithExactlyOneLetterInBetween("aaa") shouldBe true
40+
internElves.containsALetterRepeatingWithExactlyOneLetterInBetween("abc") shouldBe false
41+
}
42+
43+
"part 2: The number of nice strings" {
44+
internElves.part2(listOf("qjhvhtzxzqqjkmpb")) shouldBe 1
45+
internElves.part2(listOf("xxyxx")) shouldBe 1
46+
internElves.part2(listOf("uurcxstgmygtbstg", "uurcxstgmygtbstg")) shouldBe 0
47+
}
48+
})

0 commit comments

Comments
 (0)