8
votes
Day 13: Distress Signal
Today's problem description: https://adventofcode.com/2022/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>
My Python solutions. A somewhat easier problem is welcome on a Monday night!
Part 1
Initially I wanted to use Python's
sorted()
built-in, which optionally takes akey
function (thoughkey
must take one argument instead of two). Instead I just rolled my own bubble sort. 🤷Part 2
Would adding the markers and using zip(l, l[1:]) work?
I suppose you'd have to alter
ordered
to use a tuple instead of 2 arguments.I think the best way to adapt my comparison function would be to return one of
{1, 0, -1}
instead of{True, None, False}
, and then wrap it infunctools.cmp_to_key
. I saw this in someone else's solution, but I didn't know aboutcmp_to_key
before so it didn't occur to me when solving.hindsight is 20:20 huh?
Part 1
This is the end of quite a nasty logical struggle. I knew exactly what I needed to do but slipped up so many times on the way.
I'm sorry tildes, I've cheated!
I didn't parse anything, I let Racket
read
the lists - after getting rid of those garbage,
s. Why won't other languages see the light that list items are space separated?Part 2
No need to sort, just count the packets less than the 2 and 6 dividers. I am a bit sneaky though since I'm assuming there is at least 1 item less than the 2 divider, so start counting them at 1 and 2 respectively.
Refactor
Unhappy with
read-packets
checking foreof-object?
like some kind of feral beast, then I rememberedin-port
much nicer.
It works because (of course) Racket's
read
already knows very well how to deal with lists! You can use () [] or even {} for lists.Although this is (surprisingly to me) slower I prefer this reworked version of
packet<?
because SICP ❤️🔥Elixir
Pattern matching + multiple function clauses helped me separate the different cases out in my recursive
in_order?/2
function. The puzzle's comparison method is actually very close to Elixir/Erlang's default term ordering for lists—elements are compared in order left to right until a "tie-breaker" is found. I almost could have gotten away with simply comparing the lists withl < r
! But the case where you wrap a number in a list when comparing it to another list prevented me from getting to cheat that way, unfortunately. Oh well :)In a similar vein, all atoms except
nil
(aka:nil
) are truthy, so having the comparator return:tie
when two lists are "equal" results in the correct sorting when it's passed toEnum.sort/2
.I used
Code.eval_string/1
to convert the input lines into lists (after doing a basic check that the string is composed only of[
]
,
and digits), which saved some time.Both parts
Python
Python