Created
December 2, 2020 22:10
-
-
Save AlbertVeli/c4fb985db25b16095935f181d11e641a to your computer and use it in GitHub Desktop.
aoc day2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
# Return list of [(min, max), char, password] | |
def read_input(fname): | |
r = [] | |
for line in open(fname).read().splitlines(): | |
a = line.split() | |
a[0] = tuple(map(int, a[0].split('-'))) | |
a[1] = a[1].replace(':', '') | |
r.append(a) | |
return r | |
a = read_input(sys.argv[1]) | |
valid1 = 0 | |
valid2 = 0 | |
for mm, c, s in a: | |
# part 1 | |
cnt1 = s.count(c) | |
if cnt1 >= mm[0] and cnt1 <= mm[1]: | |
valid1 += 1 | |
# part 2 | |
cnt2 = 0 | |
for i in mm: | |
if s[i - 1] == c: | |
cnt2 += 1 | |
if cnt2 == 1: | |
valid2 += 1 | |
print(valid1) | |
print(valid2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment