13
votes
Day 13: Transparent Origami
Today's problem description: https://adventofcode.com/2021/day/13
Please post your solutions in your own top-level comment. Here's a template you can copy-paste into your comment to format it nicely, with the code collapsed by default inside an expandable section with syntax highlighting (you can replace python
with any of the "short names" listed in this page of supported languages):
<details>
<summary>Part 1</summary>
```python
Your code here.
```
</details>
This one was fun, but pretty straightforward compared to this point in some of the previous years.
Part 1 Ruby
Part 2 Ruby
This year has felt easier than previous years. There were so many complaints about 2019 and I think Eric takes the feedback very seriously. I'm expecting the 18th & 19th to be more challenging since they're the weekend and close to the end.
Day 13 (Rust)
Today was the first time I actually timed myself (by making a screen recording). Took me about 1 hour to solve both tasks, though, despite not intending to show the recording to anyone, I was still somewhat nervous about going fast this time — which, ironically, made me end up being slower.
As an aside: I knew I was going to confuse the axes going in, and of course I did, after thinking I had gotten it right...
Imports & Data structures
Parsing
Helper methods
I love how this turned out. Folding the paper is just mapping
fold_position()
over the current coordinates, and Rust does the rest.I don't like the manual step required here.
Solving
My rule now is strictly (x, y[,z]) for co-ordinates.
Eric usually displays everything with y starting at 0 and increasing down the screen, so the trick is to display increasing y in your outer loop, then iterate with x increasing left to right.
It's become a habit now, I don't even have to think - except those few sneaky problems - like with negative numbers or a hex grid.
I've standardized on (y, x) coordinates myself actually, because then they are arranged exactly like you would print them out (in row-then-column/scanline order), with grid[0][0] being the top-left. But since I was using a HashSet with named coordinates, that actually wasn't the problem.
The part where I was confused was the mapping of "fold along x/y" -> "horizontal/vertical": folding along the x-axis actually makes a vertical (y-axis) crease, although the fold itself is horizontal... I couldn't keep that straight.
This was fun, simple and clean.
Part 1
Part 2
Part 2 just consisted of running my debug visualizer. It would have taken 3 seconds but I wasn't sure if this was a V or U and got locked out.(It was the latter)
Day 13 Part A - Python
I thought my code was somehow not working because I missed the part where we only needed to do the first flip for part one. I thought there was a bug in the problem description. I actually had to go back and make it only do the first fold, since I already made it do the whole list
Day 13 Part B - Python
I already had the folding algorithm working, so I only really had to figure out the visualization portion. More keeping x and y straight, my biggest weakness
Python
Data Structure
A hash table from point -> "#".
My submissions used a
set
but I changed to a hash because:hash-count
is constant time (which means it is stored internally - so I don't have to!).I don't think that
set-count
has the same guarantee.My input is the whole file contents split by '\n\n'
My utility
lines
splits on a single '\n'Part 1
fold
is purely functional - returning a new hash.It matches the instruction with constants "x" (vertical) & "y" (horizontal).
fold/>
only changes co-ordinates greater than the fold instruction.It amuses me greatly to name it
fold
.Part 2
Basically the same except for all instructions and displays the
paper at the end. I use " " for because it's easier to read.
Part 1 + 2, Rust
Nothing too crazy here. I feel I could probably cut the time down a bit by using a 2D array instead of a hashset, but it already runs fast enough at around 400 microseconds.
Rust
Edit: Replaced
HashMap
withHashSet
since the value of the map was irrelevantRust
Python
Elixir
This one was pretty straightforward, and luckily I had defined a
CharSpace
module while doing last year's puzzles which made it pretty simple to print out the final result. I'm not going to include that code because it's really over-engineered for the relatively basic needs of this specific puzzle (needed to make it generalized for up to 4 dimensions for last year's puzzle), so just know that it's a thing that can take a list of coordinate pairs and produce a string that "draws" them on an appropriately-sized canvas.I've found it really helpful/intuitive to use streams whenever a puzzle involves iteration over the initial input. At the top level, this approach transforms the problem into simply reading values from an enumerable. For this problem, the appropriate stream function to do the fold iterations was
Stream.unfold/2
, which gave me a chuckle.Both parts
Rust
Today wasn't too bad but I spent an embarrassingly long amount of time trying to figure out the relation for which spots on the right/bottom map to which spots on the left/top after a fold.
Rust (both days)
I definitely could clean my code up quite a bit but it works so, eh, maybe later :).
Friggin' yeesh. With everyone saying how easy this year is compared to previous, it's no wonder I couldn't hang in this far in previous years.
This did end up being a very fun (and visually pleasing!) problem once I worked out the kinks. I knew I'd get in trouble for "standardizing" my test folds to a centralized axis, so that did come back to bite me. But I went ahead and coded for all the folds since the problem implied that was the next step, and ended up getting some fun map/filter practice to backwardly-derive my answer to part 1.
Part 1
Part 2
In retrospect a set of some kind would have saved me a lot of checking and probably have been faster, but that's not how it panned out. Just glad to make a bit of progress in catching up!
Parts 1 and 2