From 44bb13882fd353507374e0ac1a4f1959e5ffd99f Mon Sep 17 00:00:00 2001 From: Dean Cowley Date: Thu, 15 May 2025 09:35:12 +0100 Subject: [PATCH 1/3] Day 04: Security Through Obscurity (part 1) --- 2016/src/Day04.kt | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 2016/src/Day04.kt diff --git a/2016/src/Day04.kt b/2016/src/Day04.kt new file mode 100644 index 0000000..29f0200 --- /dev/null +++ b/2016/src/Day04.kt @@ -0,0 +1,30 @@ +fun main() { + fun part1(input: List): Int { + val regex = "([a-z-]+)+-(\\d+)\\[(\\w+)]".toRegex() + + return input.sumOf { + val (name, id, checksum) = regex.find(it)!!.destructured + + val counts = name + .replace("-", "") + .associateWith { char -> Regex("$char").findAll(name).count() } + + val actualChecksum = counts.keys.sorted() + .sortedByDescending(counts::get) + .take(5) + .joinToString("") + + if (actualChecksum == checksum) { + id.toInt() + } else { + 0 + } + } + } + + check(part1(listOf("aaaaa-bbb-z-y-x-123[abxyz]")) == 123) + check(part1(listOf("a-b-c-d-e-f-g-h-987[abcde]")) == 987) + check(part1(listOf("not-a-real-room-404[oarel]")) == 404) + check(part1(listOf("totally-real-room-200[decoy]")) == 0) + println(part1(readInput("Day04"))) +} \ No newline at end of file From b2200cbd560b59b85e0b2a5fffdafb39f9610b02 Mon Sep 17 00:00:00 2001 From: Dean Cowley Date: Fri, 16 May 2025 08:01:47 +0100 Subject: [PATCH 2/3] Day 04: Security Through Obscurity (part 2) --- 2016/src/Day04.kt | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/2016/src/Day04.kt b/2016/src/Day04.kt index 29f0200..08fa455 100644 --- a/2016/src/Day04.kt +++ b/2016/src/Day04.kt @@ -22,9 +22,26 @@ fun main() { } } + fun part2(input: List) = input.map { + val regex = "([a-z-]+)+-(\\d+)\\[(\\w+)]".toRegex() + + val (name, id, _) = regex.find(it)!!.destructured + + val decrypted = name + .split("-") + .joinToString(" ") { word -> + word.map { c -> Char(((c.code - 'a'.code) + id.toInt()) % 26 + 'a'.code) } + .joinToString("") + } + + "$decrypted: $id" + }.find { it.contains("northpole object storage") } + check(part1(listOf("aaaaa-bbb-z-y-x-123[abxyz]")) == 123) check(part1(listOf("a-b-c-d-e-f-g-h-987[abcde]")) == 987) check(part1(listOf("not-a-real-room-404[oarel]")) == 404) check(part1(listOf("totally-real-room-200[decoy]")) == 0) println(part1(readInput("Day04"))) + + println(part2(readInput("Day04"))) } \ No newline at end of file From 2feb5295c19d13c2e826e22850e9a96117b5a929 Mon Sep 17 00:00:00 2001 From: Dean Cowley Date: Fri, 16 May 2025 08:04:00 +0100 Subject: [PATCH 3/3] Day 04: Security Through Obscurity (refactor) --- 2016/src/Day04.kt | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/2016/src/Day04.kt b/2016/src/Day04.kt index 08fa455..67f41eb 100644 --- a/2016/src/Day04.kt +++ b/2016/src/Day04.kt @@ -1,30 +1,26 @@ fun main() { - fun part1(input: List): Int { - val regex = "([a-z-]+)+-(\\d+)\\[(\\w+)]".toRegex() + val regex = "([a-z-]+)+-(\\d+)\\[(\\w+)]".toRegex() - return input.sumOf { - val (name, id, checksum) = regex.find(it)!!.destructured + fun part1(input: List) = input.sumOf { + val (name, id, checksum) = regex.find(it)!!.destructured - val counts = name - .replace("-", "") - .associateWith { char -> Regex("$char").findAll(name).count() } + val counts = name + .replace("-", "") + .associateWith { char -> Regex("$char").findAll(name).count() } - val actualChecksum = counts.keys.sorted() - .sortedByDescending(counts::get) - .take(5) - .joinToString("") + val actualChecksum = counts.keys.sorted() + .sortedByDescending(counts::get) + .take(5) + .joinToString("") - if (actualChecksum == checksum) { - id.toInt() - } else { - 0 - } + if (actualChecksum == checksum) { + id.toInt() + } else { + 0 } } fun part2(input: List) = input.map { - val regex = "([a-z-]+)+-(\\d+)\\[(\\w+)]".toRegex() - val (name, id, _) = regex.find(it)!!.destructured val decrypted = name @@ -44,4 +40,4 @@ fun main() { println(part1(readInput("Day04"))) println(part2(readInput("Day04"))) -} \ No newline at end of file +}