-
Notifications
You must be signed in to change notification settings - Fork 4
/
Day3.kt
38 lines (33 loc) · 953 Bytes
/
Day3.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.github.ephemient.aoc2022
@Day
class Day3(private val lines: List<String>) {
@Day.Part
fun part1(): Int = lines.sumOf {
(it.items(end = it.length / 2) and it.items(start = it.length / 2)).bitSum()
}
@Day.Part
fun part2(): Int = lines.chunked(3).sumOf { (a, b, c) ->
(a.items() and b.items() and c.items()).singleBit()
}
}
private fun String.items(start: Int = 0, end: Int = length): Long {
var set = 0L
for (i in start until end) {
when (val c = get(i)) {
in 'a'..'z' -> set = set or 1L.shl(c - 'a' + 1)
in 'A'..'Z' -> set = set or 1L.shl(c - 'A' + 27)
}
}
return set
}
private fun Long.bitSum(): Int {
var sum = 0
for (i in 0 until 64) {
if (this and 1L.shl(i) != 0L) sum += i
}
return sum
}
private fun Long.singleBit(): Int {
require(this != 0L && this and this - 1 == 0L)
return countTrailingZeroBits()
}