Day 4 is here! Our first puzzle launched on a weeknight. Hopefully, we're able to keep up this pace!
Today's challenge makes me feel very much like a secret agent as we observe elfy guards falling asleep on duty: figuring out who is sleeping, when, and how much.
I haven't even started this challenge yet (hoping to get to it this evening or maybe at lunch), but I wanted to get the post up for everybody else to share their solutions.
Top comments (36)
May refactor later, but this problem was a little less fun for me than other ones so far, to be honest.
Agree that this one was less fun, and I kind of hate my Python solution, which is much muddier than yours. I don't know that I have the energy to try to do this in Go.
Yeah, I like the algorithm-y ones, this one was more string parsing I think!
It was a complicated description though, I had a lot of problems parsing what I was supposed to be finding.
TOTALLY -- I only figured it out once I looked at the input. The graph actually totally threw me off.
First missunderstood the task, so I wasted my whole time before university for the wrong solution... Fortunately I had some time after the first seminar to fix my solution.
Then found out that I can just call sorted and sort the data and I don't need any extra key for this. This made solving the puzzle much easier.
I used the same approach, but I really like the tools you have for searching the structure. The Clojure ones work, but were much more manual, making the code a little more clunky.
Did that yesterday! Elegantly solved exactly the wrong problem, had to go back and re-read the whole page to figure out what I was doing wrong. Devastating.
Yeah it is just super frustrating... Especially if you notice then that you have to move on to do other things and will loose expensive points on your private leaderboards 🙈
Tomorrow I will read the task at least twice before I start typing.
Damn life, getting in the way of AoC. The very nerve.
Had to brush up on the difference between usort, uasort, and uksort for this one, ahaha.
UKSort works the same, but is just more polite and has a slight accent, right? :]
The first part made me think a little about the data structure, so I decided to create a map of the guard ID to the guard's times, where the times is a map of the minute to the number of times that the guard was asleep in that minute.
Fortunately, the date/time format is year/month/day/hour/minute, which can be sorted alphanumerically, and once that's done all sleep and wake events occur in pairs together.
Given the fixed-width, it seemed easier to substring out the bits I was looking for.
Now that I had this table, I just needed to search through it for the bits I was looking for. After trying to pull out a key/value entry from a map for the largest value, I decided to create a helper function that I could send to
reducewhich let me select the "largest" value, using an arbitrary function on the value in order to compare:
Later on, someone pointed out that Clojure already has the function
max-keywhich finds the key for the largest value, but if you want to look for a non-numeric value then you need to update the entries of the map. That meant that I ended up with a lot more code, and I don't think it was as elegant.
Anyway, now that I was reading the table and had my helper function, here is the first part:
Part 2 was similar. This time I created a temporary var to hold the function that
(max-finder identity)returns, so that the
mapfunction doesn't keep calling it unnecessarily:
I like how having the correct data structure meant that the final calculations could be encapsulated in just a couple of lines.
Anybody feel like this today?
YES I had to read the question like 18 times.
Indeed, it was the longest by far so far. The challenge was posted at 9pm, PST time. I started about 9:30pm. I went to bed around midnight. Still worth it :)
Timezones really matter if you're trying to get a good score (which is one reason why I'm not trying). I really feel for the Europeans who want to compete.
Yeah, it's possible for me to change the leaderboard to order based on number of stars. It still breaks ties based on who got the most recent star sooner, but it would be a little bit less demoralizing. Does that seem like a good idea?
Also, while it may seem that it's mighty convenient that I decide to make the leaderboard more forgiving on the first day that I miss a 9PM start, that honestly wasn't why I'm thinking about this. It just seems like more fun if there's not a difference of 400 points between 1st place and last place. :)
I totally support this -- keeps the staying up to 1 AM pressure low.
Done and done!
Awesome! Thanks @rpalo !
I have been trying to learn F#, coming from C#, and I'm quite "happy" with my previous solutions. This one challenged me more, having to think 'functional'! I am not happy with how it looks, but here it is anyway!
My other attempts are on github. Critique is highly welcome!
I made a datastructure that mapped guards to days to minutes with type:
Map<string,Map<string,bool array>which made it 'easy' (or so I though) to extract the data.
Then Part 1
and Part 2
My Python solution, which I'm less than jazzed about. And I definitely have no interest in redoing it in Go today. I do not have the drive or energy. /shrug
I found today the easiest thus far, but it ended up being the most verbose solution. Despite that I'm relatively pleased with it - it runs fast and I find it readable.
Non-functional-paradigm kotlin solution
See Neil Gall's wonderful post for a more functional-paradigm approach. Kotlin not being pure-functional makes me lean into the procedural style more often than I'd like.
I didn't read the problem properly. Some key things I missed:
Fortunately, once I knew what I was supposed to be recording, it was just a simple matter of adding sleeping events to guards.
So, the thing that threw me here is that some guards don't fall asleep, leading
null. Otherwise, this was a fairly simple modification of the first part, just aggregating at different points. At this point, I'm wondering if Eric Wastl is experimenting with reversing the difficulties this year. This is two in a row where the heavy lifting gets done in part 1. We'll see when tomorrow's drops!
and since I got bored waiting for some scans to complete, here's what my schedule looks like!
I should probably just pick the days that don't have guards!
I like your use of
-ensuffixes to denote verbs. Makes a lot of sense, actually, easy to glean intent.
Haha, I was tired and started with
awakenand I thought
asleepenwas really hilarious.
Of course once I realized that there was a distinction between going on duty and waking up from a sleep, I was honor-bound to add
Had a particularly long day at work, so got to the problem late and only just now finished. I'm with all of you that this one was less fun...I haven't refactored since I don't have much brain left for today, but here's my solution for now!
Didn't really have much time to do the challenges today, but yeah, whenever you did the first three days it's hard to skip one.
So here are my solutions in Elixir. Some function returns are a bit messy, but it does what it needs to do. The basic idea is that all lines are first parsed into a map with guards, where each guard is the key for another map containing the minutes and the number of times the guard was asleep during that minute.
Having that basic map makes the calculations for both strategies relatively easy.
Done! Completed my solution with just minutes to spare before the next day goes live! I thought I would like this challenge. I did not. It was fine, for the most part. But I spent at least fifteen minutes wondering why my solution wasn't working before I realized the #!@%#@!$! LOGS WERE NOT IN CHRONOLOGICAL ORDER.
Let me tell you: I was not calm in that moment.
After I used VS Code's built-in sorting functionality to pre-sort the input text, it was mostly smooth sailing. However, rust gurus, before I sorted my input, every time I ran
cargo run, the output would be a different answer, even with no changes to the code. Anybody know why that might be? I would expect a static input file and static code to make the same result every time.
Part 2 went pretty quick now that I've got all this infrastructure in place.
Someone needs to talk to these elves about the awful ways they're recording and storing data.
Data integrity is important people. Especially at this kind of scale!