16
votes
Day 4: Camp Cleanup
Today's problem description: https://adventofcode.com/2022/day/4
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. Just like yesterday, sets are perfect here.
Part 1
Part 2
Pretty similar to yesterday if you ask me. Still love Swift!
Part 1 and 2
advent of code is the only time that i write dynamically typed code these days. and lo and behold! 2/3rds of the time i took for part 1 was spent because i forgot to convert strings to ints and didn't realize what was going wrong :')
Part 1, in Python-ish
Python code generated from the above
Part 2, in Python-ish
Python code generated from the above
Python-ish looks interesting. I could only find a github compiler to generate C++ from a python subset.
Is it a pre-processor, interpreter, compiler? Something else?
it is a little pre-processor i cobbled together. it has two stages:
sed 's/λ/lambda/g'
. (in the future i'd like to actually extend the grammar of an existing Python implementation instead of using this hack to mimic an extended grammar.)pyp
with apyprc.py
that is (for the most part)`pyprc.py`
much of the cool stuff happening is actually not done by preprocessing—it's
pipetools
's pure-Python tricks with heavy usage of(more_)?itertools
on top.Same story here.
Today was way easier in bash than yesterday. I made a mistake in part 1 (see spoiler) and tried to get too clever in part 2. A simple solution was good enough.
Part 1+2
In part 1, I missed the 'continue' command and some overlaps were double counted. Adding it got the right answer.I'm really annoyed at my performance today, but I'm always too hard on myself.
I was delayed about ½ hour and in my haste to grab those sweet, sweet leaderboard points, made so many fundamental and stupid mistakes I feel ashamed at myself, leading to annoyance, bordering anger. :(
Request: Since I've mistyped it about 100,00 times...
Note
mina,minb not min-efla/b etc..Part 1
Part 2
Speedrun
ta;wf Too annoyed, won't fixOh well, there's always tomorrow :)
This, I feel, went much better than day 03.
Part 1
And I'm quite proud of the overlap function. xD
Part 2
I know the destructuring in
process-line
andoverlap?
aren't necessary, but it was easier to think about, this way. :DYou can simplify de-structuring of lists (and structs and others) with
match-define
(define a (first <alist>))
(define b (second <alist>)
(define c (second <alist>)
(match-define (list a b c) <alist>)
you can go even further
(match-define (list (list a1 a2) (list a3 a4)) '((1 2) (3 4)))
matching is Racket is very powerful.
One thing I would point out is that in Racket, the only false value is the boolean value
#f
, sometimes written#false
. Every other value is logically#t
. So in conditionsnull
,0
,-1
,""
, ... even the void value returned by calling e.g.(vector-set! v 1 7)
is considered logically#t
.That means writing
(if <expression> #t #f)
is redundant; it's logically identical to<expression>
. Whatever is using that (and expecting a#t
or#f
value) will respond exactly the same when you just return<expression>
by itself.If you want the opposite,
(if <expression> #f #t)
, use(not <expression>)
.Of course it's your decision, and it doesn't mean you should never return a bare #t or #f - I'll often do that with a complex
cond
with several cases, but it's never needed withif
.That's very helpful, thank you!
Easier one than yesterday for those of us without sets. Tiny AWK solutions:
Part 1
Part 2
feeling incredibly stupid now looking at my test for overlaps...
which translated would be
(($1 <= $4 && $3 <= $2) || ($3 <= $2 && $1 <= $4))
Nah I had something even longer at first. I just took the time afterwards to distill it to a simpler form. :P
Python
Updated
Elixir
Both parts
Definitely could have done this better if I had some more knowledge, which thanks to other solutions I have now! So hopefully that will help in future days, but for now you're stuck with this slightly messy code
Parts 1 & 2
Google Sheets!
EDIT! I am changing my answer.
this took me forever -- and it all boiled down to me not converting REGEX outputs to values. Anyway, this is a few characters lighter anyway...
Part 1
Part 2
Rust
Rust
I ended up writing my own Range implementation, didnt think to look in std for one.
I struggled with trying to convert each elf's assignment to a set. Nim is confusing...
Nim (both parts)
I didn't really use sets (did
<=
andif x in a..b
instead) but here's a bit of a cleaner procedure:assignmentToSet
Thanks :)
Decided to start impl on my custom types. Once again, rusts type system is coming in handy.
Driver
Utils
Part 1
Part 2
When I first read the problem description, I went to sets, just like I did yesterday and like it seems like many people did here. Then, I got worried that this was a trick, and that I was going to create some kind of monstrosity with huge sets if I went that path, so I just went with keeping the high and low of each assignment and having to think about the conditionals for overlapping, which tripped me up for a little bit on part 2.
Parts 1 and 2
More sets!
I did wonder if maybe there'd be some trick to part 2 that would mean using sets
would blow up somehow, but figured that we're still only on day 4.
Clojure (both parts)
Here's my TypeScript solution
Part 1
Part 2
I got lazy here and decided to count the non-overlapping pairs, and subtract that number from the whole. Gets the job done.
JavaScript solution, both parts
Ruby
Did this back-to-back with Day 3, this one was quite a bit easier IMO. Was able to hit part 2 merely adding a second "if".
Typescript Solution
I'm using Rust this year, and trying to keep it
std
-only throughout the month.Part 1
Part 2