r/adventofcode • • Dec 02 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 02 Solutions -🎄-

--- Day 2: Password Philosophy ---


Advent of Code 2020: Gettin' Crafty With It


Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.

Reminder: Top-level posts in Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:02:31, megathread unlocked!

101 Upvotes

1.2k comments sorted by

View all comments

2

u/CSEcon Dec 02 '20

Python:
probably way too much code than necessary but it's honest work:

def is_valid2(password_triple):
    number_letter_min = password_triple["min"]
    number_letter_max = password_triple["max"]
    letter = password_triple["letter"]
    password = password_triple["password"]

    case1 = password[number_letter_min - 1] == letter and password[number_letter_max - 1]
    case2 = password[number_letter_min - 1] != letter and password[number_letter_max - 1] == letter
    case3 = password[number_letter_min - 1] == letter and password[number_letter_max - 1] == letter

    return (case1 or case2) and (not case3)

def is_valid1(password_triple):
    number_letter_min = password_triple["min"]
    number_letter_min_max = password_triple["max"]
    letter = password_triple["letter"]
    password = password_triple["password"]

    count = 0
    for p in password:
        if p == letter:
            count += 1 
    return count >= number_letter_min and count <= number_letter_min_max

def convert_to_map(value):
    password_triple = {}
    array = value.split(" ")
    password_triple["min"], password_triple ["max"] = int(array[0].split("-")[0]) , int(array[0].split("-")[1])
    password_triple["letter"] = array[1].replace(":","")
    password_triple["password"] = array[2]
    return password_triple
    
if __name__ == "__main__":
    input = open("input.txt", "r")
    data = [convert_to_map(line) for line in input.readlines()]
    isValid = [is_valid1(d) for d in data]
    isValid2 = [is_valid2(d) for d in data]
    print("solution to 1 is: " + str(isValid.count(True)))
    print("solution to 2 is: " + str(isValid2.count(True)))

1

u/daggerdragon Dec 02 '20

Please add the language used to your post to make it easier for folks who Ctrl-F the megathreads looking for a specific language. Thanks!