File tree Expand file tree Collapse file tree 3 files changed +37
-1
lines changed
Expand file tree Collapse file tree 3 files changed +37
-1
lines changed Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ plugins {
77group = " de.nxll.aoc2022"
88version = " 1.0"
99
10- val currentDay = 5
10+ val currentDay = 6
1111
1212repositories {
1313 mavenCentral()
Original file line number Diff line number Diff line change 11fun 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments