advent2020/two.py

39 lines
996 B
Python
Executable File

#!/usr/bin/env python3
import sys
import numpy as np
def tpl(l): # get nice type correct tuple from line
ssf = l.split(' ') # space separated fields
lohi = ssf[0].split('-')
lo = int(lohi[0])
hi = int(lohi[1])
char = ssf[1][0]
strg = ssf[2]
return lo, hi, char, strg
def good_string(lo, hi, char, strg):
try:
idx = strg.index(char)
# succeeded in finding char
if hi == 0: # too many chars
return False
return good_string(lo - 1, hi - 1, char,
strg[:idx] + strg[idx + 1:])
except:
# failed at finding char
return lo <= 0 # enough chars
def new_good_string(lo, hi, char, strg):
return (strg[lo - 1] == char) != (strg[hi - 1] == char)
fn = sys.argv[1] if len(sys.argv) > 1 else 'input2'
with open(fn) as f:
lines = f.readlines()
ct = sum(good_string(*tpl(l)) for l in lines)
print(ct)
ct = sum(new_good_string(*tpl(l)) for l in lines)
print(ct)