-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDay13.kt
55 lines (45 loc) · 1.83 KB
/
Day13.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* Copyright (c) 2021 by Todd Ginsberg
*/
/**
* Advent of Code 2021, Day 13 - Transparent Origami
* Problem Description: http://adventofcode.com/2021/day/13
* Blog Post/Commentary: https://todd.ginsberg.com/post/advent-of-code/2021/day13/
*/
package com.ginsberg.advent2021
class Day13(input: List<String>) {
private val paper: Set<Point2d> = parsePoints(input)
private val instructions: List<Point2d> = parseInstructions(input)
fun solvePart1(): Int =
paper.crease(instructions.first()).size
fun solvePart2(): Unit =
instructions.fold(paper) { paper, instruction -> paper.crease(instruction) }.printout()
private fun Set<Point2d>.printout() {
(0..this.maxOf { it.y }).forEach { y ->
(0..this.maxOf { it.x }).forEach { x ->
print(if (Point2d(x, y) in this) "#" else " ")
}
println()
}
}
private fun Set<Point2d>.crease(instruction: Point2d): Set<Point2d> =
if (instruction.x != 0) this.map { it.copy(x = it.x.creaseAt(instruction.x)) }.toSet()
else this.map { it.copy(y = it.y.creaseAt(instruction.y)) }.toSet()
private fun Int.creaseAt(crease: Int): Int =
if (this < crease) this else (crease * 2) - this
private fun parsePoints(input: List<String>): Set<Point2d> =
input.takeWhile { it.isNotEmpty() }
.map { it.split(",") }
.map { Point2d(it.first().toInt(), it.last().toInt()) }
.toSet()
private fun parseInstructions(input: List<String>): List<Point2d> =
input.takeLastWhile { it.isNotEmpty() }
.map { it.split("=") }
.map {
if (it.first().endsWith("y")) {
Point2d(0, it.last().toInt())
} else {
Point2d(it.last().toInt(), 0)
}
}
}