Skip to content

Commit 6d40ea4

Browse files
committed
Day 5
1 parent 1c4e065 commit 6d40ea4

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ plugins {
77
group = "de.nxll.aoc2022"
88
version = "1.0"
99

10-
val currentDay = 5
10+
val currentDay = 6
1111

1212
repositories {
1313
mavenCentral()

src/common/kotlin/Text.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
fun CharSequence.splitInTwo(delimiter: String): Pair<String, String> {
22
return this.split(delimiter, limit = 2).let { it.first() to it.last() }
33
}
4+
5+
fun CharSequence.containsDuplicates(): Boolean {
6+
return this.any { this.count(it::equals) > 1 }
7+
}

src/day_6/kotlin/Day6.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// AOC Day 6
2+
3+
fun findMarker(dataStream: String, markerSize: Int): Int? {
4+
for (headPosition in markerSize until dataStream.length) {
5+
/** [String.substring] start is inclusive, end exclusive */
6+
val tailPositionIndex = headPosition - markerSize
7+
if (!dataStream.substring(tailPositionIndex, headPosition).containsDuplicates()) {
8+
return headPosition
9+
}
10+
}
11+
12+
return null
13+
}
14+
15+
fun part1(dataStream: String) {
16+
val firstPacketPosition = findMarker(dataStream, 4)!!
17+
18+
println("Part 1: The first packet starts at position $firstPacketPosition")
19+
}
20+
21+
fun part2(dataStream: String) {
22+
val firstMessagePacketPosition = findMarker(dataStream, 14)!!
23+
24+
println("Part 2: The first message packet starts at position $firstMessagePacketPosition")
25+
}
26+
27+
fun main() {
28+
val dataStream = getAOCInput { it.trim() }
29+
30+
part1(dataStream)
31+
part2(dataStream)
32+
}

0 commit comments

Comments
 (0)