Skip to content

Instantly share code, notes, and snippets.

@AlbertVeli
Created December 2, 2020 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlbertVeli/c4fb985db25b16095935f181d11e641a to your computer and use it in GitHub Desktop.
Save AlbertVeli/c4fb985db25b16095935f181d11e641a to your computer and use it in GitHub Desktop.
aoc day2
#!/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