Day three! Our DEV leaderboard is up to 44 people, which is awesome!
Also, check out the much classier cover images for these posts that @aspittel came up with! 🎅🥇💻
Anyways, today's challenge asks us to calculate which cells are or are not overlapped as it gives us a bunch of grid rectangles (x, y, height, width) to plot out.
How did everybody do?
Latest comments (39)
I've been solving all of these in Elixir thus far, and it's been pretty fun! Day 3 is the first time I dealt with "off by one" errors...
My solution (Elixir) for the first part was:
Takes about a second. The first version used lists and list-diffing to find duplicates, but this took minutes to compute. Maps were much faster at this size (hundeds of thousands of entries).
I should mention I'm just learning Elixir, so this is not to be understood as "good code" or "knows what he's doing".
Here's the code:
Cool! Thanks for sharing!
Just a heads up, you can put the word “elixir” immediately after the opening triple backtick, and your code will show up with syntax highlighting. 😁
Hi guys! Here is my solution on #typescript.
Repo: github.com/room-js/adventOfCode201...
This seemed like a natural job for Fortran!
Part 1
No, really, it has nice array operation and slicing syntax! I've just stretched out a big array, added 1 everywhere there's a claim, and then counted the number of elements where there's an element greater than 1.
Part 2
Part 2 was trickier, and I had to use a similar solution to @rpalo , going over each claim again; but then I checked whether the sum of the cut elements was the same as its area to determine whether it was a unique claim. I could have done a similar
count
-based method to part 1, but I thought of this way first.I don't write a lot of Fortran, and peering at about 7 descriptions of how advanced IO worked didn't get me very far, so I used
sed
to strip out everything that wasn't a number or a space and that made it much more amenable to Fortran'sread
input preferences.Woah, this is super cool! The right tool for the right job, huh? 😎 thanks for sharing!
c# again
Tests:
Ah, regexes and string splitting. The One True Way to parse text is with parser combinators.
Part 1
I really wanted to do Part 1 geometrically by splitting and merging claims into non-overlapping regions. It would be O(N²) however, and I was short of actual time to build it so went for the big map of positions like many others:
That uses a modified
Claim
class as follows:Part 2
Fairly simple extension. The tricky bit was remembering to remove both overlapping claims from the candidate result set:
Node.js
Reused my work for part 1 in part 2.
I make the 1000 x 1000 array and fill it every slot with a
*
.First time a claim is added, it gets a
#
If a claim overlaps with another, those cells get marked with an
X
.Part 1: Filter matrix for all
X
values.Part 2: Check every coordinate for each claim. If every coordinate is a
#
, then that claim is the answer.Main.js:
I did my part 1 similarly: I wrote the ID to each cell in it’s rectangle, unless the cell wasn’t zero, in which case I wrote -1. At the end, I counted the -1 values.
The second step was a little different. First of all, I added the ID to a set of IDs that were OK. Then I went through each cell of its rectangle. If the cell is zero, then update it to the current row’s ID. If it had a number in it, then that’s the ID of the most recent rectangle to overlap that cell; so remove the found ID and the current ID from the set of good IDs. Then set all the cells in the entire rectangle to its ID. At the end, the set of good IDs has a single element.
It sounds tricky, but it’s literally just a couple of lines of code
Puh, that was a tough one for me today. I started out by counting the number of claims for each square on the "canvas" which gave me a correct aswer. However, I later misunderstand part 2 thinking I should find completely unclaimed squares that could be filled out by one claim...
After cheating a little and getting inspirations for solutions I have implemented a Golang solution for today. At least I am happy that I could implement it in Golang without any prior experience using Golang:
Part 1 and 2 together:
Perl solutions. The first part was easy, just counting how many times each square occurred. The second part was trickier and the naive solution was too slow, so I summoned some regular expressions to help me: