-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDay03.kt
37 lines (30 loc) · 1.04 KB
/
Day03.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
/*
* Copyright (c) 2022 by Todd Ginsberg
*/
/**
* Advent of Code 2022, Day 3 - Rucksack Reorganization
* Problem Description: http://adventofcode.com/2022/day/3
* Blog Post/Commentary: https://todd.ginsberg.com/post/advent-of-code/2022/day3/
*/
package com.ginsberg.advent2022
class Day03(private val input: List<String>) {
fun solvePart1(): Int =
input.sumOf { it.sharedItem().priority() }
fun solvePart2(): Int =
input.chunked(3).sumOf { it.sharedItem().priority() }
private fun Char.priority(): Int =
when (this) {
in 'a'..'z' -> (this - 'a') + 1
in 'A'..'Z' -> (this - 'A') + 27
else -> throw IllegalArgumentException("Letter not in range: $this")
}
private fun String.sharedItem(): Char =
listOf(
substring(0..length / 2),
substring(length / 2)
).sharedItem()
private fun List<String>.sharedItem(): Char =
map { it.toSet() }
.reduce { left, right -> left intersect right}
.first()
}