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!

99 Upvotes

1.2k comments sorted by

View all comments

1

u/OutofPlaceStuff Dec 02 '20 edited Dec 02 '20

Done part 1 in Ruby and in Python. Python is my "native" language. I'm just now picking up Ruby for a potential job. Always open to feedback.

Ruby


"""
password-philosophy.rb: from day_2.txt find out how many passwords are valid according to their policies; solve puzzle at https://adventofcode.com/2020/day/2
2 December 2020
@Vicki_Langer
"""

valid_passwords = 0

passwords_with_policies = File.readlines("inputs/day_2.txt")

# \d numerical digit
# \w alphanumeric characters
# * zero or more

passwords_with_policies. each do |line|
    low, high, character, password = line.match(/(\d*)-(\d*) (\w): (\w*)/).captures  # each () goes to the respective variable and is the captured in an array
        if password.count(character).between?(low.to_i, high.to_i)
            valid_passwords += 1
        end
    puts valid_passwords
end

Python

"""
password-philosophy.py: from day_2.txt find out how many passwords are valid according to their policies; solve puzzle at https://adventofcode.com/2020/day/2
2 December 2020
@Vicki_Langer / @VickiLanger
"""


def validate_passwords():
    num_valid_passwords = 0
    for line in open('inputs/day_2.txt').readlines():
        lower_upper, character, password = line.strip('\n').split()
        character = character[0]
        lower, upper = lower_upper.split('-')
        count = password.count(character)
        if (count >= int(lower)) & (count <= int(upper)):
            num_valid_passwords += 1
            print(num_valid_passwords)


validate_passwords()

2

u/daggerdragon Dec 02 '20

This code is really hard to read on old.reddit. Could you please edit it using old.reddit's four-spaces formatting instead of new.reddit's triple backticks? Note that if you're using the visual editor, you may have to "Switch to Markdown" to get Reddit to understand the formatting properly.

1

u/backtickbot Dec 02 '20

Hello, OutofPlaceStuff: code blocks using backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead. It's a bit annoying, but then your code blocks are properly formatted for everyone.

An easy way to do this is to use the code-block button in the editor. If it's not working, try switching to the fancy-pants editor and back again.

Comment with formatting fixed for old.reddit.com users

FAQ

You can opt out by replying with backtickopt6 to this comment.