Skip to content

Commit 24d84bc

Browse files
committed
2022/Day 04
1 parent 9aed672 commit 24d84bc

File tree

4 files changed

+1031
-3
lines changed

4 files changed

+1031
-3
lines changed

src/main/kotlin/me/grison/aoc/Extensions.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,14 @@ fun Iterable<Pair<Long, Long>>.pointsDisplay(empty: String = " "): String {
345345
return display.joinToString("\n") { it.joinToString("") }
346346
}
347347

348+
fun Collection<Int>.toRange() = IntRange(this.first(), this.last())
349+
fun Collection<Long>.toRange() = LongRange(this.first(), this.last())
350+
fun Pair<Int, Int>.toRange() = IntRange(this.first, this.second)
351+
fun Pair<Long, Long>.toRange() = LongRange(this.first, this.second)
352+
353+
fun IntRange.contains(range: IntRange) = this.first <= range.first && this.last >= range.last
354+
fun IntRange.overlap(range: IntRange) = range.first in this || range.last in this || this.first in range || this.last in range
355+
348356
fun Collection<Int>.range() = maxOrNull()!! - minOrNull()!!
349357
fun Collection<Long>.range() = maxOrNull()!! - minOrNull()!!
350358
fun Collection<BigInteger>.range() = maxOrNull()!!.minus(minOrNull()!!)

src/main/kotlin/me/grison/aoc/Strings.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package me.grison.aoc
22

3+
import java.util.*
4+
35
/** Returns the occurrences of `c`. */
46
fun String.occurrences(c: Char) = count { it == c }
57

@@ -73,6 +75,7 @@ fun String.afterLast(s: String) = split(s).last()
7375
fun String.allInts(includeNegative: Boolean = true): List<Int> =
7476
(if (includeNegative) "(-?\\d+)" else "(\\d+)").regex().findAll(this).map { it.value.toInt() }.toList()
7577

78+
fun String.allPositiveInts(): List<Int> = this.allInts(false)
7679
/** Returns all long found in this string. */
7780
fun String.allLongs(includeNegative: Boolean = true): List<Long> =
7881
(if (includeNegative) "(-?\\d+)" else "(\\d+)").regex().findAll(this).map { it.value.toLong() }.toList()
@@ -101,6 +104,9 @@ fun String.except(c: Char) = replace(c + "", "")
101104
/** Returns the string before `str`. */
102105
fun String.before(str: String) = split(str)[0]
103106

107+
fun String.intRange() = this.normalSplit("-").let { IntRange(it[0].toInt(), it[1].toInt()) }
108+
fun String.longRange() = this.normalSplit("-").let { LongRange(it[0].toLong(), it[1].toLong()) }
109+
104110
/** Returns the string after `str`. */
105111
fun String.after(str: String) = split(str)[1]
106112

@@ -114,8 +120,8 @@ fun String.words() = normalSplit(" ")
114120
fun String.set() = stringList().toSet()
115121

116122
fun String.`is`(s: String) = this == s
117-
fun String.upper() = toUpperCase()
118-
fun String.lower() = toLowerCase()
123+
fun String.upper() = uppercase(Locale.getDefault())
124+
fun String.lower() = lowercase(Locale.getDefault())
119125

120126
fun String.isLower() = all { it.isLowerCase() }
121127
fun String.isUpper() = all { it.isUpperCase() }
@@ -142,5 +148,5 @@ fun String.split(at: Int) = listOf(this.take(at), this.drop(at))
142148
fun String.inTwo() = this.split(this.length / 2)
143149

144150
fun foo() {
145-
"".capitalize()
151+
"".replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
146152
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package me.grison.aoc.y2022
2+
3+
import me.grison.aoc.*
4+
5+
class Day04 : Day(4, 2022) {
6+
override fun title() = "Camp Cleanup"
7+
8+
override fun partOne() = solve { a, b -> a.contains(b) || b.contains(a) }
9+
10+
override fun partTwo() = solve { a, b -> a.overlap(b) }
11+
12+
private fun solve(predicate: (pair1: IntRange, pair2: IntRange) -> Boolean) =
13+
inputList.count { predicate(it.before(",").intRange(), it.after(",").intRange()) }
14+
}

0 commit comments

Comments
 (0)