# Ideas and Solutions for Advent of Code 2021 in Kotlin -- Part 4/4

DevFeed: [Ideas and Solutions for Advent of Code 2021 in Kotlin -- Part 4/4](<https://devfeed.tech/articles/ideas-and-solutions-for-advent-of-code-2021-in-kotlin-part-4-4-24736.md>)

Original publisher: [Read original article](<https://medium.com/xorum-io/ideas-and-solutions-for-advent-of-code-2021-in-kotlin-part-4-4-75c207692bdb?source=rss----92bb7980cc9f---4>)

Author: Yev Kanivets

Published: 2021-12-26T14:39:41Z

Content type: tutorial

Language: en

Sources: [xorum.io - Medium](<https://devfeed.tech/sources/xorum-io-medium.md>)

Topics: [Advent of Code](<https://devfeed.tech/topics/advent-of-code.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [3D](<https://devfeed.tech/topics/3d.md>), [pixel](<https://devfeed.tech/topics/pixel.md>)

Tags: [3d](<https://devfeed.tech/tags/3d.md>), [advent-of-code](<https://devfeed.tech/tags/advent-of-code.md>), [algorithms](<https://devfeed.tech/tags/algorithms.md>), [array](<https://devfeed.tech/tags/array.md>), [beacon](<https://devfeed.tech/tags/beacon.md>), [competitive-programming](<https://devfeed.tech/tags/competitive-programming.md>), [data-structures](<https://devfeed.tech/tags/data-structures.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [pixel](<https://devfeed.tech/tags/pixel.md>), [points](<https://devfeed.tech/tags/points.md>), [scanner](<https://devfeed.tech/tags/scanner.md>), [solutions](<https://devfeed.tech/tags/solutions.md>)

## AI overview

A Kotlin walkthrough of Advent of Code 2021 tasks 19 and 20. It explains matching rotated point sets in 3D space using beacon intersections, then enhancing an image represented as a 2D pixel array while accounting for infinite padding and changing background values.

## Source excerpt

Ideas and Solutions for Advent of Code 2021 in Kotlin -- Part 4/4 The final week of Advent of Code was, obviously, the most challenging one. To be honest I was close to being happy with just a silver star multiple times. But at the end, tenacity made the difference. 50 stars are mine this year 🌟 Ideas and Solutions for tasks 1 to 6 can be found here, for tasks 7 to 12 -- here, for tasks 13 to 18 -- here. Day 19: Beacon Scanner We are provided with several sets of points in 3D space. Those points are rotated differently in each set, so we don't know how X, Y, and Z coordinates are aligned between sets. Our goal is to merge all those sets into a single group by matching points (sub-task one) and find the most significant distance between coordinate origins after they are matched. Here is the complete task. This is an exciting task! I've initially learned about this idea from The Three-Body Problem book, which introduced the concept of locating any star in the Universe by the distances from a few nearest stars -- in our case, "a few" means 12. Think about it as a star fingerprint ;) So to match the sets of points in 3D space, we need: check all combinations of rotations in three dimensions (in our task, the step is 90 degrees, so there are 48 combinations) for the whole set assume that each pair of points from the first set and the second set is an intersection by adjusting all points to that pair check how many adjusted points intersect between the two sets if there are more than 12 intersections, voilá -- the sets are matched The embedded Kotlin's functions like intersect, map, and mapNotNull make the solution, even for such a complex task, elegant. Day 20: Trench Map This image-enhancing task provides us with the initial photo (2D array of black and white pixels), and enhancing guidance (1D array). Enhancing happens in steps, where for each pixel, we found its enhanced value as a result of a combination of itself + all eight adjacent pixels. Here is the complete task. We