Skip to content

Commit 746ac72

Browse files
committed
Solution 2015-08 (Matchsticks)
1 parent 6ef4f94 commit 746ac72

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package de.ronny_h.aoc.year2015.day08
2+
3+
import de.ronny_h.aoc.AdventOfCode
4+
5+
fun main() = Matchsticks().run(1350, 2085)
6+
7+
class Matchsticks : AdventOfCode<Int>(2015, 8) {
8+
override fun part1(input: List<String>): Int =
9+
input.sumOf { numberOfCharactersOfCode(it) - numberOfCharactersInMemory(it) }
10+
11+
override fun part2(input: List<String>): Int =
12+
input.sumOf { numberOfCharactersEncoded(it) - numberOfCharactersOfCode(it) }
13+
14+
fun numberOfCharactersOfCode(string: String) = string.length
15+
fun numberOfCharactersInMemory(string: String) = string.unescape().length
16+
fun numberOfCharactersEncoded(string: String) = string.length +
17+
string.count { it == '"' } +
18+
string.count { it == '\\' } +
19+
2
20+
}
21+
22+
private val startQuotationRegex = "^\"".toRegex()
23+
private val endQuotationRegex = "\"$".toRegex()
24+
private val asciiCharRegex = "\\\\x[0-9A-Fa-f][0-9A-Fa-f]".toRegex()
25+
26+
fun String.unescape(): String = replace(startQuotationRegex, "")
27+
.replace(endQuotationRegex, "")
28+
.replace(asciiCharRegex, """_""")
29+
.replace("""\"""", """"""")
30+
.replace("""\\""", """\""")
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package de.ronny_h.aoc.year2015.day08
2+
3+
import io.kotest.core.spec.style.StringSpec
4+
import io.kotest.data.forAll
5+
import io.kotest.data.row
6+
import io.kotest.matchers.shouldBe
7+
8+
class MatchsticksTest : StringSpec({
9+
10+
"the number of characters of code" {
11+
val matchsticks = Matchsticks()
12+
forAll(
13+
row("""""""", 2),
14+
row(""""abc"""", 5),
15+
row(""""aaa\"aaa"""", 10),
16+
row(""""\x27"""", 6),
17+
row(""""\\"""", 4),
18+
row(""""\\\\"""", 6),
19+
row(""""\\\\""""", 7),
20+
) { string, expected ->
21+
matchsticks.numberOfCharactersOfCode(string) shouldBe expected
22+
}
23+
}
24+
25+
"the number of characters of the in-memory string" {
26+
val matchsticks = Matchsticks()
27+
forAll(
28+
row("""""""", 0),
29+
row(""""abc"""", 3),
30+
row(""""aaa\"aaa"""", 7),
31+
row(""""\x27"""", 1),
32+
row(""""\\x27"""", 2),
33+
row(""""\\"""", 1),
34+
row(""""\\\\"""", 2),
35+
row(""""\\\\""""", 3),
36+
row(""""\\\""""", 2),
37+
row(""""\\""""", 2),
38+
) { string, expected ->
39+
matchsticks.numberOfCharactersInMemory(string) shouldBe expected
40+
}
41+
}
42+
43+
"unescape ascii characters from hexadecimal escapes" {
44+
""""\x27"""".unescape() shouldBe """_"""
45+
""""\xb7"""".unescape() shouldBe """_"""
46+
}
47+
48+
"part 1: the total number of code chars minus number of in-memory chars" {
49+
val input = listOf(
50+
"""""""",
51+
""""abc"""",
52+
""""aaa\"aaa"""",
53+
""""\x27"""",
54+
)
55+
Matchsticks().part1(input) shouldBe 12
56+
}
57+
58+
"the number of characters of encoded string" {
59+
val matchsticks = Matchsticks()
60+
forAll(
61+
row("""""""", 6),
62+
row(""""abc"""", 9),
63+
row(""""aaa\"aaa"""", 16),
64+
row(""""\x27"""", 11),
65+
) { string, expected ->
66+
matchsticks.numberOfCharactersEncoded(string) shouldBe expected
67+
}
68+
}
69+
70+
"part 2: the total number of newly encoded chars minus number of code chars" {
71+
val input = listOf(
72+
"""""""",
73+
""""abc"""",
74+
""""aaa\"aaa"""",
75+
""""\x27"""",
76+
)
77+
Matchsticks().part2(input) shouldBe 19
78+
}
79+
})

0 commit comments

Comments
 (0)