aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortchojnacki <tomaszchojnacki2001@gmail.com>2021-12-18 22:25:33 +0100
committertchojnacki <tomaszchojnacki2001@gmail.com>2021-12-18 22:25:33 +0100
commitac40561e6ad662cfbf7c2540bbdcf8572bae552a (patch)
treebc49286cf7f7ad0429e5484ef0ba883dae931c4e
parent7a2de912f5048f01df790e7edf2d308f68b314ac (diff)
downloadgleam_aoc2020-ac40561e6ad662cfbf7c2540bbdcf8572bae552a.tar.gz
gleam_aoc2020-ac40561e6ad662cfbf7c2540bbdcf8572bae552a.zip
Finish day 13
-rw-r--r--README.md6
-rw-r--r--src/Day13.kt102
2 files changed, 105 insertions, 3 deletions
diff --git a/README.md b/README.md
index 7893928..991e42c 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,7 @@
# Advent of Code 2021 in Kotlin
![Kotlin](https://img.shields.io/badge/Kotlin-grey?logo=Kotlin)
-![](https://img.shields.io/badge/⭐%20stars-28-yellow)
-![](https://img.shields.io/badge/📅%20days-14-blue)
+![](https://img.shields.io/badge/⭐%20stars-30-yellow)
+![](https://img.shields.io/badge/📅%20days-15-blue)
Welcome to the Advent of Code[^aoc] Kotlin project created by [tchojnacki][github] using the [Advent of Code Kotlin Template][template] delivered by JetBrains.
@@ -20,7 +20,7 @@ Welcome to the Advent of Code[^aoc] Kotlin project created by [tchojnacki][githu
| Day 10: Syntax Scoring | 🌟 | 🌟 |
| Day 11: Dumbo Octopus | 🌟 | 🌟 |
| Day 12: Passage Pathing | 🌟 | 🌟 |
-| Day 13: Transparent Origami | | |
+| Day 13: Transparent Origami | 🌟 | 🌟 |
| Day 14: Extended Polymerization | 🌟 | 🌟 |
| Day 15: Chiton | 🌟 | 🌟 |
| Day 16: Packet Decoder | | |
diff --git a/src/Day13.kt b/src/Day13.kt
new file mode 100644
index 0000000..f37c527
--- /dev/null
+++ b/src/Day13.kt
@@ -0,0 +1,102 @@
+import java.lang.IllegalStateException
+
+sealed class FoldCommand {
+ abstract fun dispatch(dots: Set<Pos2D>): Set<Pos2D>
+
+ class AlongX(private val x: Int) : FoldCommand() {
+ override fun dispatch(dots: Set<Pos2D>): Set<Pos2D> = dots
+ .filter { it.x != x }
+ .map {
+ if (it.x < x) {
+ it
+ } else {
+ Pos2D(2 * x - it.x, it.y)
+ }
+ }
+ .toSet()
+ }
+
+ class AlongY(val y: Int) : FoldCommand() {
+ override fun dispatch(dots: Set<Pos2D>): Set<Pos2D> = dots
+ .filter { it.y != y }
+ .map {
+ if (it.y < y) {
+ it
+ } else {
+ Pos2D(it.x, 2 * y - it.y)
+ }
+ }
+ .toSet()
+ }
+}
+
+fun parseOrigami(input: List<String>): Pair<Set<Pos2D>, Sequence<FoldCommand>> {
+ val dots = mutableSetOf<Pos2D>()
+ val commands = mutableListOf<FoldCommand>()
+
+ for (line in input) {
+ if (line.matches("\\d+,\\d+".toRegex())) {
+ val (x, y) = line.split(",").map(String::toInt)
+ dots.add(Pos2D(x, y))
+ }
+
+ if (line.matches("fold along [xy]=\\d+".toRegex())) {
+ val equation = line.substring(11)
+ val (axis, valueString) = equation.split("=")
+ val value = valueString.toInt()
+
+ commands.add(
+ when (axis) {
+ "x" -> FoldCommand.AlongX(value)
+ "y" -> FoldCommand.AlongY(value)
+ else -> throw IllegalStateException("Illegal axis given!")
+ }
+ )
+ }
+ }
+
+ return dots to commands.asSequence()
+}
+
+fun main() {
+ fun part1(input: List<String>): Int {
+ val (dots, commands) = parseOrigami(input)
+
+ val res = commands.first().dispatch(dots).size
+
+ return res
+ }
+
+ fun part2(input: List<String>): String {
+ val origami = parseOrigami(input)
+ var dots = origami.first
+ val commands = origami.second
+
+ commands.forEach {
+ dots = it.dispatch(dots)
+ }
+
+ val bounds = dots.reduce { max, pos ->
+ when ((pos.x > max.x) to (pos.y > max.y)) {
+ true to true -> pos
+ true to false -> Pos2D(pos.x, max.y)
+ false to true -> Pos2D(max.x, pos.y)
+ else -> max
+ }
+ }
+
+ val lines = Array(bounds.y + 1) { Array(bounds.x + 1) { ' ' } }
+
+ dots.forEach { lines[it.y][it.x] = '#' }
+
+ return lines.joinToString("\n") { it.joinToString("") }
+ }
+
+
+ val testInput = readInputAsLines("Day13_test")
+ check(part1(testInput) == 17)
+
+ val input = readInputAsLines("Day13")
+ println(part1(input))
+ println(part2(input))
+}