Guest User

Untitled

a guest
Dec 14th, 2021
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. fun main() {
  2. fun fold(grid: Set<Point2d>, folds: List<Pair<String, Int>>): Set<Point2d> {
  3. return folds.fold(grid) { pt, i ->
  4. val (axis, pos) = i
  5. when (axis) {
  6. "y" -> pt.map { if (it.y > pos) Point2d(it.x, 2 * pos - it.y) else it }.toSet()
  7. else -> pt.map { if (it.x > pos) Point2d(2 * pos - it.x, it.y) else it }.toSet()
  8. }
  9. }
  10. }
  11.  
  12. fun draw(input: Set<Point2d>) {
  13. println((0..input.maxOf { it.y }).map{ y ->
  14. (0..input.maxOf { it.x }).map{ x ->
  15. when(input.contains(Point2d(x,y))){
  16. true -> "█"
  17. false -> " "
  18. }
  19. }.joinToString("","","\n")
  20. }.joinToString(""))
  21. }
  22.  
  23. measureTimeMillisPrint {
  24. val input = readInput("Day13").map { it.trim() }
  25. val grid = input.asSequence().filterNot { s -> s.contains("=") }.filter { it.isNotEmpty() }.map { it.split(",") }
  26. .map { p -> Point2d(p.first().toInt(), p.last().toInt()) }.toSet()
  27. val folds = input.filter { s -> s.contains("=") }.map { it.split("=") }
  28. .map { p -> p.first().split(" ").last() to p.last().toInt() }
  29. println("Part 1 : " + fold(grid, folds.take(1)).size)
  30. draw(fold(grid, folds))
  31. }
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment