-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_3.kts
31 lines (24 loc) · 894 Bytes
/
day_3.kts
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
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Collectors
val input: List<String> = Files.lines(Paths.get("inputs/day_3.txt")).collect(Collectors.toList())
fun calculatePriority(char: Char): Int {
//println("char: $char toInt: ${char.toInt()}")
if (char.toInt() >= 'a'.toInt() && char.toInt() <= 'z'.toInt()) {
return 1 + char.toInt() - 'a'.toInt()
}
return 27 + char.toInt() - 'A'.toInt()
}
fun commonItemFrom(bags: List<String>): Char {
return bags.map { it.toSet() }
.reduce { acc, bag -> bag.intersect(acc) }
.first()
}
val partOne = input.map { commonItemFrom(it.chunked(it.length / 2)) }
.sumBy { calculatePriority(it) }
println(partOne)
val partTwo = input.windowed(size = 3, step = 3)
.map { commonItemFrom(it) }
.sumBy { calculatePriority(it) }
println(partTwo)