• Skip to main content
  • Skip to primary sidebar
BMA

BeMyAficionado

Inspire Affection

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

December 9, 2020 by varunshrivastava Leave a Comment

The Password Philosophy is the topic of today’s discussion. Since there is not much to do with today’s problem apart from the string parsing, I thought of doing it in an object-oriented way.

The problem statement is mostly based on the string parsing. You will get a list of lines with the password and password check policy. The task is to parse the policy and password from the string and validate that the password obeys the policy.

For example – following is the list of password policies and password,

1-3 a: abcde
1-3 b: cdefg
2-9 c: ccccccccc

Each line gives the password policy and then the password. The password policy indicates the lowest and highest number of times a given letter must appear for the password to be valid. For example, 1-3 a means that the password must contain a at least 1 time and at most 3 times.

In the above example, 2 passwords are valid. The middle password, cdefg, is not; it contains no instances of b, but needs at least 1. The first and third passwords are valid: they contain one a or nine c, both within the limits of their respective policies.

How many passwords are valid according to their policies?

So let’s see how can we solve this problem.

Table of Contents

  • PART 1 – Classes & Algorithm
    • Password Class
    • Policy Class
  • PART – 2
    • Time for creating a new password policy

PART 1 – Classes & Algorithm

The task at hand are:

  • Parse the line to separate out password policy and the password.
  • Check if the given password obeys the policy

Here we have two clearly defined entities.

  • Password
  • Policy

The relation is also pretty clear. We apply the policy to password to check whether the password is correct or not. Password validity could be an attribute of a password but policy is the one to decide whether a given password is valid or not.

Another thing is that if policy changes there should be no need for a password to change. Another word, same password could become valid if a different policy operates on it.

So, with this understanding, let’s do some coding.

Password Class

Let’s start by creating a Password class. This class is very simple, it will hold the password and attribute that will tell whether the password is valid or not.

There’s one more method to find the count of the given character in the password.

Password Class. You should be more cautious in defining them.
Password class, should be more cautious in defining these

Policy Class

This class is responsible for the rules that comes with the policy. It knows how to parse the information from the passed string. It also know how to validate whether a given password is valid or not.

And if password doesn’t adheres to this policy then it would be marked invalid.

Here’s the Policy class.

It's all about defining the policy
It’s all about defining the policy

Another thing that we need is a file parser. But that we will do in the file itself (for simplicity).

And as we parse the line, we will be creating policy and password and applying the policy to the password to check if the password is valid.

If the password is valid then we will increment the count by one or else not.

So, this is how we can do that:

Parsing each line to create password and its policy
Line parsing to create password and policy

After running this code on the file we get the correct output: 572

Let’s see if this count is correct or not.

Viola!!!

The count is correct and elves are happy 🙂

Let’s move to the second part of this question.

PART – 2

Seems like the Shopkeeper made a mistake in telling us the password policy. He told us something from his prior workplace but now he checked and came up with the correct policy.

And the new policy goes like this:

Each policy actually describes two positions in the password, where 1 means the first character, 2 means the second character, and so on. (Be careful; Toboggan Corporate Policies have no concept of “index zero”!) Exactly one of these positions must contain the given letter. Other occurrences of the letter are irrelevant for the purposes of policy enforcement.

Given the same example list from above:

  • 1-3 a: abcde is valid: position 1 contains a and position 3 does not.
  • 1-3 b: cdefg is invalid: neither position 1 nor position 3 contains b.
  • 2-9 c: ccccccccc is invalid: both position 2 and position 9 contain c.

How many passwords are valid according to the new interpretation of the policies?

Time for creating a new password policy

Since we have everything in place already, the only difference is in the policy so that is the only thing that we will change in our program.

Let’s create a new Password Policy then. Let’s name it NewPolicy and extend it from Policy.

New Policy to determine if the passwords are same or different.
New policy to identify if the password is valid or not

Let’s try running the code with this new policy in place.

For that we just have to make change in one place, i.e,

policy = Policy(parts[0])

policy = NewPolicy(parts[0])

The rest of the code will stay the same.

After running the program I got the correct answer. Finally, all the elves are happy now.

Here’s the entire code:

Entire code for Advent of code day 2 - Password Philosophy
Entire Code as an Image because I want you to write for yourself.

Let me know if you have any questions or you want to discuss anything in the comments below. And don’t forget to like and share.

Related

Filed Under: Programming Tagged With: aoc-2020, day-2, 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