• Skip to main content
  • Skip to primary sidebar
  • Home
  • About
  • Subscribe BMA
  • Contact Us!

Be My Aficionado

Inspire Affection

You are here: Home / Programming / Advent Of Code 2020 – Day 6 – Custom Customs

Advent Of Code 2020 – Day 6 – Custom Customs5 min read

December 17, 2020 by Varun Shrivastava Leave a Comment

This problem was a bit hard to understand at first because the information is not directly given, you have to derive it from the given input.

The story goes like this, your aeroplane has reached a regional office where you will switch to a much larger plane, custom declaration forms are distributed to passengers.

The form asks a series of yes-or-no questions marked from a-z. And here’s your task – you need to identify questions for which anyone in your group answers “yes”. However, you don’t just have to answer this for your group (because that would be easy), you have to answer this for all the groups sitting there. Your input is basically their yes answer to any of the questions in the single line. For example –

abcx
abcy
abcz

Here’s each line consists of the question number (a-z) for which anyone in the group have answered “yes”.

So, in the example above there are 6 questions to which anyone answered yes: a, b, c, x, y, z. Duplicate answers to the same questions do not count.

I’ll copy paste one more example from the Advent of Code Website.

So, for each group, count the number of questions for anyone answered “yes”.

What is the sum of those counts?

Part One – Count the number of questions for anyone answered YES

The first part of the problem is understanding the format in which we get the input and parse it accordingly. So, as you can see from the above example every group is separated by a new line. So, as we encounter the empty line we would know that all the people of that group have answered the question and then we will count the total questions which were answered YES.

(In the original question I solved it using c++, but in the blog, I will use python to explain the solution because it feels more readable for someone who is new to the programming. So if you are looking for the solution in c++ language, do comment below and I will provide the code there)

Below code makes use of the fileinput python module that could read the file from the path passed to the script as a parameter. To execute below code use: python3 day6.py <input_file_path>.

   1 import fileinput
   2
   3 lines = [line.strip() for line in fileinput.input()]
   4
   5 # since we are relying on the empty line
   6 # to identify the group of people
   7 # empty line at the end is required to count
   8 # the last group
   9 lines.append('')
  10
  11 # we need all the unique answers
  12 # set is the right data structure to keep 
  13 # track of the distinct objects
  14 yes_questions = set()
  15 sum = 0
  16 for line in lines:
  17     if not line:
  18         # if we encounter an empty line
  19         # count the total questions answered YES
  20         # and reset the set
  21         sum += len(yes_questions)
  22         yes_questions.clear()
  23     else:
  24         for q in line:
  25             yes_questions.add(q)
  26
  27 print(sum)
 

Viola!!!

That’s all for part one. The problem seemed difficult but it was actually easy once you understand what is asked for. So, all the logic goes in the line 17-22.

Try running the solution for you given set of input.

So, let’s see what is asked in the part two of the problem.

Part Two – Count the questions for which everyone answered YES

Turns out we misread the instruction. We were suppose to count all questions for which EVERYONE in the group answered YES. So, let’s see how can we build our current solution to solve the second part.

While solving the second part in I realized something that the solution is purely mathematical. And python is a great language to express mathematical expressions via code. So, if you observe closely, what the problem actually asks is the intersection between each line of the input. So, the problem can actually be solved by doing it the following way:

    for line in lines:  
       if not line:
           sum += len(all_yes)
           all_yes = None
       else:
           if all_yes is None:
               all_yes = set(line)
           else:
               all_yes = all_yes & set(line)

Let’s take the given example to understand it better.

abc

a
b
c

ab
ac

a
a
a
a

b

Dry Run Of The Above Code

lineall_yes (SET = starts with None)SUM
abc{a,b,c}0
”RESET0 + 3 = 3
a{a}3
b{a} & {b} = {}3
c{} & {c} = {}3
”RESET3 + 0 = 3
ab{a,b}3
ac{a,b} & {a,c} = {a}3
”RESET3 + 1 = 4
a{a}4
a{a} & {a} = {a}4
a{a} & {a} = {a}4
a{a} & {a} = {a}4
”RESET4 + 1 = 5
b{b}5
”RESET5 + 1 = 6
Dry Run of the Code

So at the end we get 6 which is the correct answer for the example problem.

Let me give you the complete code for the second part.

   1 import fileinput
   2
   3 lines = [line.strip() for line in fileinput.input()]
   4
   5 # since we are relying on the empty line
   6 # to identify the group of people
   7 # empty line at the end is required to count
   8 # the last group
   9 lines.append('')
  10
  14 yes_questions = set()
  15 all_yes = None
  16 sum = 0
  17 for line in lines:
  18     if not line:
  19         # if we encounter an empty line
  20         # count the number of question 
             # to which everyone answered YES
  22         sum += len(all_yes)
  23         all_yes = None
  24     else:
  25         if all_yes is None:
  26             all_yes = set(line)
  27         else:
  28             all_yes = all_yes & set(line)
  29
  30 print(sum)

I hope this problem was fun to solve. If you are following along then do subscribe to solve Advent of Code questions with me.

Do let me know if there is anything else that you would like to read about?

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • More
  • Click to print (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Pocket (Opens in new window)
  • Click to share on Telegram (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Skype (Opens in new window)
  • Click to email this to a friend (Opens in new window)

Filed Under: Programming Tagged With: aoc-2020, day-6, problem-solving, python

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Featured Posts

Things India Need to Improve

February 21, 2017 By Priyanka Yadav Leave a Comment

HACK-Free 1 year Amazon Prime Membership

August 10, 2017 By Varun Shrivastava 26 Comments

Advent Of Code 2020 – DAY 2 – Object-Oriented Way

December 9, 2020 By Varun Shrivastava Leave a Comment

How to Earn Money From Amazon Affiliate Network

May 6, 2018 By Varun Shrivastava Leave a Comment

3 Best Life Lessons Learned from Naruto Uzumaki

July 4, 2020 By Annie Tenma Leave a Comment

Latest Posts

  • Study Abroad Destinations : Research and Review
  • Advent Of Code 2020 – Day 7 – Handy Haversacks
  • Advent Of Code 2020 – Day 6 – Custom Customs
  • Advent Of Code 2020 – Day 5 – Binary Boarding
  • Advent Of Code 2020 – Day 4 – Passport Processing

Categories

  • Blogging (101)
  • Cooking (11)
  • Fashion (7)
  • Finance & Money (12)
  • Programming (50)
  • Reviews (2)
  • Technology (22)
  • Travelling (4)
  • Tutorials (12)
  • Web Hosting (8)
  • Wordpress N SEO (19)

Follow us on facebook

Follow us on facebook

Grab the Deal Now!

Hostgator Starting @$3.95/mo

DigitalOcean Free Credits

Trending

Affordable Hosting amazon aoc-2020 bad luck believe in yourself best database earn money blogging education experience fashion finance Financial Freedom food friends goals google india indian cuisine indian education system java javascript life life changing love make money microservices motivation oops poor education system principles of microservices problem-solving programmer programming reality search engines seo SSD Hosting success technology tips top 5 web web developer wordpress

Copyright © 2021 · BeMyAficionado by Varun Shrivastava · WordPress

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.