OK, the first day was awesome, I'm super excited about all of the solutions people posted. And I'm learning a lot! We've got a solid 20 people on the DEV leaderboard, which means there are still spots for 180 more -- use code 224198-25048a19 to join!
On to Day 2!
Day 2 of Advent of Code, and I'm pretty sure that Google is tired of me asking it questions every 15 seconds about "How Do I Do X in Rust."
Today's challenge involves an inventory system. Boxes have IDs that are a jumble of letters, and we've got a warehouse full of boxes to check. The first part asks us to analyze the frequency of letters in each ID. The second part gets into Hamming Distances, which are a familiar sight after mentoring on Exercism.
I got both parts working, and even part 2 ran pretty fast, but I'm not algorithmically happy with the double-loop (O(n^2)) runtime. Did anybody come up with anything tricky to do it more efficiently?
I'm going to post my solution in the comments like the rest of the cool kids.
How'd everybody else do?
Latest comments (64)
Here's my C# .Net solution:
A bit late to the party, here are my solutions on Ruby:
Part 1:
Part 2:
I learnt about the combination method :)
I'm not super thrilled with my Day 2 code, but I haven't really had the time to tweak it with everything going on at work currently.
Swift Solutions
Part 1
This one was fairly simple. Just count how many times each letter appears in each
String
and act appropriately. I do like the fact that a SwiftString
type is really anArray
ofCharacters
.Part 2
This one feels clunky, if I get a chance I'll revisit it.
I break on the first hit for a solution to short circuit everything and return the answer, this can help a lot with so many
Characters
to test.I use
zip(_:_:)
with a filter and count to quickly test how many differences there are in the same positions. When I see two strings that differ by one character in the same position I move to the next step.In the second part I cast the Arrays into
Set
types so that I can use the Collection Algebra features to quickly find the actual character to remove by subtracting one collection from the other. With that done I can just remove it from the sourceArray
and return what's left.Normally I would
import Foundation
here so that I could just useNSOrderedSet
and skip a few steps. I wanted to try and keep it all in the Swift Standard Library though, so I didn't!A little late, to the party. I tried really hard to think of a solution to part 2 that only involved iterating the list once, but no luck. Here is my solution in Elixir.
Part one:
Part 2:
Part 1: C# + LINQ = one-liner
Part 2
My solution in JavaScript / Node 11, using the
readline
interface:readLines.js
02a.js
02b.js
This one was difficult for me, but I eventually got it! I'm also not happy about that double for loop in part 2, but I think I sped it up by removing the elements as I compared them?
github.com/stevieoberg/advent-of-c...
Late to the party!
Still digging F#, but I'm definitely hindered by my lack of familiarity with what's available in .NET
I’m trying to use a broader range of languages than I do usually, so I figured I’d try not to use one I’d already used before through the days. I use bash all the time, so I thought I’d get it out of the way early.
This was not one of my better decisions, but worked fine!
Part 1 uses regular expressions; I could have used an associative array like some other people in the thread, but for some reason I went here first. The sort function wasn’t necessary, but helped with debugging.
Part 2 uses the same double
for
loop lamented elsewhere, but it gets the job done.Neither of these is what I’d call “fast”.
Woah, nice! It's always really cool to see bigger things done with Bash :)
P.S. Depending on your Bash version, you can save yourself some typing with
{a..z}
.Oh yes, good call, I missed that compaction.
Javascript lazy solution
I don't have much time to solve the challenges :( So I'm just trying to get the stars.
part 1
Part 2
I did my solutions at midnight last night, but I was surviving on very little sleep at the time, so the resulting code was below standard. I tried again this morning and felt better about it.
For anyone reading this, I'm still using the simple
lines
function from Day 1 which reads a file into a sequence of strings.Part 1
This was my solution last night:
This is embarrassing code. I totally forgot about the
frequencies
function, which is why I usedgroup-by
followed bycount
. But the 2filter
operations in the final calculation meant that the results of themap
get processed twice.My morning attempt fixed these:
This time I accumulated the 2/3 count values while processing the list, so it only goes through it once.
Part 2
Since each element needs to be compared to every other element, I can't see any way around the O(n2 ) complexity. Of course, each element should only be compared to the ones after it, so as to avoid comparing each one twice (since comparing A/B has the same result as comparing B/A).
When doing list processing, the only ways I know to refer to everything following an element are by using indexes (yuck) or with a
loop
. Unfortunately, I got fixated on the loop construct, and nested it:The other way you can tell that I wrote this on very little sleep was the use of ridiculously terse var names.
On reflection this morning, I realized that the inner loop should have been a
some
operation. This does a predicate test and terminates processing early, which is what I was doing manually with the innerloop
.Also, my
close
function has several issues. First is the name! I was thinking of "A is close to B", but when I saw it again this morning I realized that it's the same function name for closing a resource. Something else that bothers me is that it processes the entirety of each string before returning, when a false result can usuall terminate early. Finally, a minor issue is that the anonymous function#(when (= %1 %2) %1)
would be more idiomatic to use a singleton set on%1
to compare to%2
.The
nearly=
function now returns a string, rather than the array of characters, but hasn't changed much. I was still unsatisfied with it not terminating the test early, so I rewrote it to be faster. However, the resulting code lacks any kind of elegance, and I'm not convinced that it's worth the effort:Hopefully I'll get some sleep before attempting day 3. 😊
Clojure (inefficient part 2)
Part 1:
Part 2:
I like the threaded use of
update
here in part 1 - my method used a transient map and returned a persistent copy at the end:Nice one. Is definitely faster than mine.
Terrible C++ solution for part 1 !
Terrible is better than never finished! And this looks pretty good to me, not knowing C++ if that makes you feel better 😄
Thanks for hosting the private leaderboard! Never been on a leaderboard before lol so that'll be fun. :)
I am curious - how is everyone posting their code? Is there a code tag on here, like there is on Slack? Is everyone sharing screenshots? I haven't posted a whole lot on here yet, so I'm not sure of the best way to share code.
I'm using JS this year, so here's my day 2 solutions: not the prettiest / most succinct, but they work!
Part 1:
Part 2:
There's an enhanced form of markdown for code blocks: triple backticks for start and end, and if you immediately follow the opening backticks with the language you get syntax highlighting. Difficult to show raw markdown in markdown unfortunately.
Excellent, thank you! Much better than screenshots. :)
My solution to day 2, in Elixir. The double for-loop in part 2 is certainly not optimal, but it works. The available time was short today. :)
Part one:
Part two:
Common: