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?
Oldest comments (39)
My messy af Python solutions. If I’m feeling energetic later today I’ll clean these up and write a Golang solution and benchmark the 2 :)
Part 1:
Part 2
github.com/thejessleigh/aoc18/tree...
Kotlin Solution
UGH! This one seemed like a reversal, the first part was much harder than the second part.
I started out as I always do when pretending to be a video game developer: swearing loudly because my rectangles were overlapping and I forgot that it's way easier to find out if they're not overlapping.
Then I kept trying to optimize, but that wasn't getting me anywhere. I ended up brute-forcing my way through. This is ugly... maybe 25 seconds to chunk through.
Part 1
Part 2
After calming down a little (rectangles are dumb), I set in to work on the second part. This turned out much simpler. It was just another n2 with an early escape function. Almost identical to yesterday. The key is making sure our find eliminates candidates as fast as possible. Enter
none, which returns false as soon as we see an overlap with our current. Yes, I was lazy and just added a quick "same id == no overlap" check instead of making sure I was checking unique pairs. I'm getting sleepy, and the first one frustrated me more than I would have liked.From my pain, your gain! An image of the overlapping areas plotted.
I totally agree that the second part seemed so much easier that the first. It really threw me off.
I initially thought that I should use my data structure from part one to solve part 2, but while figuring it out I realized that a different structure was much more effective. I like to make my functions for the first and second parts independent anyway, so it didn’t bother me to do it again. And it came out much smaller!
Part 1
I really struggled with part 1. Not because I couldn't figure out the problem, or get my code to compile. I had my tests running pretty quickly! But I kept getting the wrong answer! I was on the very precipice of giving up and checking the subreddit when I realized that
str.matches(|c| c.is_digit(10))only finds single digit at a time -- it doesn't build consecutive digits into a single "find." So with input string#1 @ 55,22: 10x10, it was reading this asid: 1, left: 5, top: 5, width: 2, height: 2and throwing away the rest. 💩After scratching my head and then bringing in my first external dependency ever in Rust (pretty painless, all things considered) things worked out just fine.
Since the dependency I brought in was just a
regexcrate, which would be built-in in some languages, I figured that was OK. I wasn't bringing in thefind-overlapping-squarescrate.Anyhow, here's part 1:
Part 2
Actually, for part 1, I didn't even keep track of the ID of the claims -- I just threw that data away! And then I read part two and sat there in sadness for a few minutes.
But!
Then I realized that I wouldn't have to come up with an entirely new approach. I could process the claims like normal, and then re-run through the claims and recheck all of their squares, to see if any have all cells with only one claim on them. Yeah, it doubles the run-time, but O(n) is O(n), even if you double it (sort of). Anyways, I'm pretty happy with today's challenge. Especially my top level functions
count_conflicting_squaresandfind_unconflicting_id: I was able to make them abstract enough that they're pretty easy to read and figure out.I made the exact same mistake with my initial attempt, where I was only grabbing the first digit with my regex. I'm glad it wasn't just me 😅I was so frustrated because the test input worked, since each number in the test input was only one digit! Sometimes I wish that AoC gave just a little more test data to work with before grabbing your final input.
Yes! The first test input kept passing, and then I wrote like 4 or 5 more tests to check different things, and they all passed! But I never checked double-digit numbers. :|
Here goes my
Typescriptsolution:Javascript solution using regex capture groups (
/^#(?<id>\d*)\s@\s(?<left>\d*),(?<top>\d*):\s(?<width>\d*)x(?<height>\d*)$/):readFile.js
03a.js
03b.js
Part 1
Part 2
If someone still doesn't get how to do it, I put up a pretty simple explaination with the code on my repo And here's the matrix that I got!
Cool visual!
Thanks! Love using p5.js
Adding my Clojure solution here - will write another post at some point. I'm oncall this week and got paged at 3am for something out of my control, so I figured "hey, I'm up, might as well."
get-overlapsolves part 1, whilefind-no-overlapsolves part 2. Second part is a little brute-force, but still worked.PHP
One of those days when there's a big hint in the name, seems like. The slice/splice functions did the heavy lifting on this one.
Part 1:
Part 2:
Solved last night, refactored this morning! Actually pretty proud of this. Python solution:
I love how clean this solution is!
Thank you so much -- that means a lot (I've been super sad this morning because somebody was mean about my solution on Twitter).
🙄People can be the worst sometimes. Sorry you had to deal with that.