Advent of Code 2020, Day 6
By Terrana
Not much to write about for day 6, though the premise is again very relatable. It's nice to help people out when you're the only one that can make sense of a system, though I believe more in teaching them how to understand the system than doing the work for them. I'd far rather have someone say to me, "let me show you how to do it," than, "let me do it for you."
The puzzle was pretty simple, though, as you can see from my solution. For part 1, take several groups of answers and count how many answers in each group were given by at least one person.
So I just borrowed my dnlsplit
function from day 4 to break up the groups, and took the union of all the lists in each group. From there, the answer is just the length of each list. Easy.
Part 2, it turns out that, oh no, we misread the instructions! It's answers that everyone in the group gave, not anyone. Well, it's a one-word change to the code, too! Instead of the union of the lists, we just take their intersection.
I should probably have done something clever to just spit out both answers in one go, but I'm sleepy today so I just copy-pasted the part 1 code to part 2. It's not like I've got code quality metrics to meet, just having a bit of fun.
EDIT: Okay, so you know I said I wasn't going to make it do both answers in one go? I made it do both answers in one go.
I changed my groups
function from just finding the union or intersection as it was before to finding both and spitting them out as a two-element list. Easy enough, but now I can't just do sum . map length
to get the total number of answers any more.
What to do about that? Well, the Data.List
module I was already using for union and intersection also has a function called transpose
. That takes a table in the form of a two-dimensional list, like the one I just made, and flips its rows and columns.
So now, instead of a long list of sets of two answers, I've got two long lists of answers. That is something I can sum, just doing map (sum . map length)
to apply the same calculation I was doing before to both lists of answers. That gives me the answers to both parts at once!
EDIT 2: Despite how simple today's puzzle was, I just can't seem to leave this one alone. After talking to Emi, I realised I could do away with that transpose by using a recursive function to just construct the two lists in one go rather than building pairs and then switching them around.
The result actually isn't any more complicated. Have a look.