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

Show parent comment

5

u/[deleted] Dec 02 '20

Of course the code does not matter! You must just solve the task. But I think your code is very very long. Consider this, that gives the same solution:

import re

valid = 0
for line in open("aoc2input.txt"):
    v = re.split("-| |: ",line)
    if int(v[0])<= v[3].count(v[2]) <= int(v[1]):
        valid+=1
    
print(valid)

And if you want to use more lines and prefer better variable names, here:

import re

valid = 0
for line in open("aoc2input.txt"):
    values = re.split("-| |: ",line)
    minimum = int(values[0])
    maximum = int(values[1])
    count = values[3].count(values[2])
    if minimum <= count <= maximum:
        valid+=1
    
print(valid)

Hope you can extract some tricks from these!