Yan Cui
I help clients go faster for less using serverless technologies.
This article is brought to you by
Don’t reinvent the patterns. Catalyst gives you consistent APIs for messaging, data, and workflow with key microservice patterns like circuit-breakers and retries for free.
(this post is part of the F# Advent Calendar project, check out all the other great posts there! And special thanks to Sergey Tihon for organizing this!)
The source code for this post (both Part 1 and Part 2) is available here and you can click here to see my solutions for the other Advent of Code challenges.
Description for today’s challenge is here.
Day 16
The input for today’s challenge looks like this:
Sue 1: goldfish: 6, trees: 9, akitas: 0
Sue 2: goldfish: 7, trees: 1, akitas: 0
Sue 3: cars: 10, akitas: 6, perfumes: 7
Sue 4: perfumes: 2, vizslas: 0, cars: 6
…
so to make parsing a little easier, I decided to use Regex with a bit of help from an active pattern:
and we also need a type to represent what we remember about each Aunt Sue. (ok, we don’t strictly need one, a tuple can also do the trick, but as we’ll be passing the payload around I think it’s a lightweight record would be easier to work with in this case )
For each line in the input:
Sue 1: goldfish: 6, trees: 9, akitas: 0
we need to fetch the number, and a key-value pair for each of the things we remember about that particular Aunt Sue. Something along the lines of:
Now, we have all the information we can remember about all 500 Aunt Sue’s, as well as the facts our My First Crime Scene Analysis Machine has told us about the Aunt Sue that we need to send a thank you card for:
Finally, we just need to go through all the Aunt Sue’s, and find the one that matches up with the facts.
As the requirement specified,
Things missing from your list aren’t zero – you simply don’t remember the value.
so if we can’t find a given key (from the list of facts) in the things we remember about an Aunt Sue we should just presume it to be true:
Day 16 (Part 2)
Oh, what shock horror, that we managed to misread the machine’s output! Luckily, our code is written in F# and pattern matching makes the required change a piece of cake
Since the new requirements are:
the cats and trees readings indicates that there are greater than that many, while the pomeranians and goldfish readings indicate that there are fewer than that many…
so we just need to tweak our logic slightly with additional match cases to cater for those two special cases:
and job done! F# saves the day and Aunt Sue gets her thank you card! ho ho ho
Whenever you’re ready, here are 3 ways I can help you:
- Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop for quickly levelling up your serverless skills.
- I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
- Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.
Pingback: F# Advent Calendar in English 2015 | Sergey Tihon's Blog
Would it a compile regex make sense if input 500 lines?
Good question, so I tried comparing the two versions and turns out using a compiled regex is actually slower in this case – might need a bigger input size to amortize the initial cost of compiling the regex.
The current version runs between 7-9ms, whereas with compiled regex it takes between 10-13ms.
At a 1000 lines of input, no-compiled-regex starts to see more volatile and swings between 10-30ms, but avgs around 15ms. The compiled-regex version is less volatile but also slower – avg around 22ms after a few runs.
So there doesn’t seem to be any benefit from compiling the regex in this case, and according to this article from Jeff Attwood way back (http://blog.codinghorror.com/to-compile-or-not-to-compile/) there are also other trade-offs around memory footprint and startup time as well. I reckon this is worth a more comprehensive investigation/benchmark.
Pingback: F# Weekly #51, 2015 | Sergey Tihon's Blog
Pingback: F# Weekly #51, 2015-IT??
Pingback: The week in .NET - 12/22/2015 - .NET Blog - Site Home - MSDN Blogs
Pingback: ???? 2015? 12? 22? - Korea Evangelist - Site Home - MSDN Blogs