<Advent of Code 2021 day 13> :thread:
# advent-of-code
a
d
these ASCII-text answer puzzles always make me wonder how in the world he drafts the problem so it works out this way
👍 1
j
I’d start from answer (random 8 letters) then randomly create a set of unfolds, moving half of stars to new position (and cloning a random few)
4
m
I folded the folds for part2! And I guess I’m not alone 😉 https://github.com/MikeEnRegalia/AdventOfCode2021/blob/main/kotlin/src/main/kotlin/Day13.kts
n
My "fold" methods look pretty neat:
Copy code
private fun foldLeft(dots: Set<Dot>, amount: Int): Set<Dot> {
    return dots.mapTo(mutableSetOf()) { dot -> if (dot.x < amount) dot else Dot(2 * amount - dot.x, dot.y) }
}
(similar for
foldUp
).
j
Something’s off with my input data, could someone check it?
K 2
❤️ 1
n
@Jakub Gwóźdź what exactly is wrong? Does your code produce the "0" shape for the sample?
j
of course! 🙂
n
I get 8 letters.
for your input. You want me to share them?
j
no wait
really? hmmm…
n
oh, I think you are fooling us! Did you write a program to produce input from a result text?
I get 6 letters and a symbol for your input
j
today producing input was more fun than solving 🙂
n
Nice!
KOTLIN💜
K 2
m
just noticed that the creases do not contain points, which was extra work to find out wasted 😆
Pretty happy with how this worked out. I started off modeling a Fold as a sealed interface with an
apply
method but then moved to
(Point) -> Point
m
decided to check what my code looked like if I removed OOP. Are you proud of me yet @Michael Böiers? 😆
Copy code
object Day13Tiny : Challenge() {
    val parsed = input.split("\r\n\r\n").let { (dots, folds) ->
        folds.lines().map { fold -> fold.split("=").let { (ins, i) -> ins to i.toInt() } }
            .runningFold(dots.lines().map { dot -> dot.split(",").map(String::toInt) }) { list, (ins, i) ->
                list.mapTo(mutableSetOf()) { (x, y) ->
                    if (ins == "fold along x") listOf(i - abs(i - x), y)
                    else listOf(x, i - abs(i - y))
                }.toList()
            }
    }.let { results ->
        results[1].size to (0..results.last().maxOf { (_, y) -> y }).joinToString("\n", "\n") { y ->
            (0..results.last().maxOf { (x, _) -> x }).joinToString("") { x ->
                if (listOf(x, y) in results.last()) "█" else "░"
            }
        }
    }

    override fun part1() = parsed.first
    override fun part2() = parsed.second
}
e
https://github.com/ephemient/aoc2021/blob/main/kt/src/commonMain/kotlin/com/github/ephemient/aoc2021/Day13.kt
Copy code
folds.fold(it) { point, fold -> fold(point) }
not my best naming, but it works
m
@Michael de Kaste Looks a bit terse. “Functional” doesn’t mean as short as possible, or that you can’t define your own data classes / structs. 🙂
@Michael de Kaste BTW: Isn’t your example still using inheritance? 🙂
m
thats just my day setup that I use for metrics and stuff, easier to write in. The whole object and part1() part2() functions can be discarded if you just print "parsed"
t
Today’s challenge was really cool 😄
m
🙌 2
p
e
Similar to @ephemient, I ended up with
Copy code
folds.fold(dots) { dots, fold -> dots.fold(fold) }
which is rather amusing. https://github.com/edgars-supe/advent-of-code/blob/master/src/main/kotlin/lv/esupe/aoc/year2021/Day13.kt
p
I renamed it to
followInstruction
because too much folding isn't good
m
Yes, I’ve also taken the folding thing too far … not a good thing when a map will do. Well, I’m doing these challenges before my morning coffee 😉
t
I called mine
crease
because I can’t handle two things being called fold. Or explain it easily.
e
maybe I should rename the wrong
fold
to
reduce
just for extra confusion ;)
p
Or just map like the swift people have
e
that operator is named "reduce" in Swift though? https://developer.apple.com/documentation/swift/array/2298686-reduce
t
If you really wanted to get crazy you could just start calling everything “fold” but use different UTF-8 characters so it looks like “fold” but is spelled with a different o that looks identical but isn’t or whatever.
fold.fold(fold) { fold, fold -> fold.fold(fold) }
🙂 1
😈 1
p
Swift .map = Kotlin ?.let
t
Basically this, except with fold.
I’m a bit late in getting it written up because I got busy with Actual Work. I like the puzzles where we have to interpret the results, but I do wonder how a visually impaired developer would solve this. • CodeBlog
d
@Jakub Gwóźdź sooooo cooollll!
👍 1
🙇 1