diff --git a/src/aoc2022/Day01.kt b/src/aoc2022/Day01.kt index 7195847..b303ea7 100644 --- a/src/aoc2022/Day01.kt +++ b/src/aoc2022/Day01.kt @@ -20,4 +20,4 @@ fun main() { val input = readInput("Day01") part1(input).println() part2(input).println() -} +} \ No newline at end of file diff --git a/src/aoc2022/Day02.kt b/src/aoc2022/Day02.kt new file mode 100644 index 0000000..ab1a9fc --- /dev/null +++ b/src/aoc2022/Day02.kt @@ -0,0 +1,30 @@ +fun main() { + // A = 0 X = 0 Win: X0 - C2, Z2 - B1, Y1 - A0 + // B = 1 Y = 1 Win: (r+2) % 3) == c; Lose: ((r+1) % 3) == c + // C = 2 Z = 2 Points = (r + 1) + ((3-c) - (r+1) % 3) * 3 + fun part1(input: List): Int { + return input.sumOf { + val c = it[0] - 'A' + val r = it[2] - 'X' + (r + 1) + ((r - c + 4) % 3) * 3 + } + } + + fun part2(input: List): Int { + return input.sumOf { + val c = it[0] - 'A' + val l = it[2] - 'X' + val r = (l - c * 2 + 8) % 3 + (r + 1) + ((r - c + 4) % 3) * 3 + } + } + + // test if implementation meets criteria from the description, like: + val testInput = readInput("aoc2022/Day02_test") + check(part1(testInput) == 15) + check(part2(testInput) == 12) + + val input = readInput("aoc2022/Day02") + part1(input).println() + part2(input).println() +} diff --git a/src/aoc2022/Day03.kt b/src/aoc2022/Day03.kt new file mode 100644 index 0000000..2d07ede --- /dev/null +++ b/src/aoc2022/Day03.kt @@ -0,0 +1,32 @@ +fun main() { + + fun toPrio(input: Char) = if (input in 'A'..'Z') input - 'A' + 27 else input - 'a' + 1 + + fun part1(input: List): Int = + input.sumOf { + val seen = IntArray(2 * 26 + 1) + it.subSequence(0, it.length / 2).forEach { seen[toPrio(it)] = 1 } + it.subSequence(it.length / 2, it.length).forEach { seen[toPrio(it)] = seen[toPrio(it)] or 2 } + seen.withIndex().find { it.value == 3 }!!.index + } + + fun part2(input: List): Int = + input.chunked(3) + .sumOf { + val seen = IntArray(2 * 26 + 1) + it[0].forEach { seen[toPrio(it)] = 1 } + it[1].forEach { seen[toPrio(it)] = seen[toPrio(it)] or 2 } + it[2].forEach { seen[toPrio(it)] = seen[toPrio(it)] or 4 } + seen.withIndex().find { it.value == 7 }!!.index + } + + // test if implementation meets criteria from the description, like: + val testInput = readInput("aoc2022/Day03_test") + part1(testInput).println() + check(part1(testInput) == 157) + check(part2(testInput) == 70) + + val input = readInput("aoc2022/Day03") + part1(input).println() + part2(input).println() +}