• Skip to main content
  • Skip to primary sidebar
BMA

BeMyAficionado

Inspire Affection

Advent Of Code 2020 – Day 6 – Custom Customs

December 17, 2020 by varunshrivastava 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?

Related

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

Primary Sidebar

Subscribe to Blog via Email

Do you enjoy the content? Feel free to leave your email with me to receive new content straight to your inbox. I'm an engineer, you can trust me :)

Join 874 other subscribers

Latest Podcasts

Recent Posts

  • Is The Cosmos a Vast Computation?
  • Building Semantic Search for E-commerce Using Product Embeddings and OpenSearch
  • Leader Election with ZooKeeper: Simplifying Distributed Systems Management
  • AWS Serverless Event Driven Data Ingestion from Multiple and Diverse Sources
  • A Step-by-Step Guide to Deploy a Static Website with CloudFront and S3 Using CDK Behind A Custom Domain

Recent Comments

  • Varun Shrivastava on Deploy Lambda Function and API Gateway With Terraform
  • Vaibhav Shrivastava on Deploy Lambda Function and API Gateway With Terraform
  • Varun Shrivastava on Should Girls Wear Short Clothes?
  • D on Should Girls Wear Short Clothes?
  • disqus_X5PikVsRAg on Basic Calculator Leetcode Problem Using Object-Oriented Programming In Java

Categories

  • Blogging
  • Cooking
  • Fashion
  • Finance & Money
  • Programming
  • Reviews
  • Software Quality Assurance
  • Technology
  • Travelling
  • Tutorials
  • Web Hosting
  • Wordpress N SEO

Archives

  • November 2024
  • September 2024
  • July 2024
  • April 2024
  • February 2024
  • November 2023
  • June 2023
  • May 2023
  • April 2023
  • August 2022
  • May 2022
  • April 2022
  • February 2022
  • January 2022
  • November 2021
  • September 2021
  • August 2021
  • June 2021
  • May 2021
  • April 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • February 2020
  • December 2019
  • November 2019
  • October 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • January 2019
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016

Tags

Affordable Hosting (4) algorithms (4) amazon (3) aoc-2020 (7) believe in yourself (4) best (4) database (4) earn money blogging (5) education (4) elementary sorting algorithms (4) experience (3) fashion (4) finance (6) Financial Freedom (7) food (7) friends (3) goals (5) google (5) india (10) indian cuisine (5) indian education system (4) java (16) life (16) life changing (4) love (4) make money (3) microservices (9) motivation (4) oops (4) podcast (6) poor education system (4) principles of microservices (5) problem-solving (7) programmer (5) programming (28) python (5) reality (3) seo (6) spring (3) success (10) success factor (4) technology (4) top 5 (7) typescript (3) wordpress (7)

Copyright © 2025 · Be My Aficionado · WordPress · Log in

Go to mobile version