Files
aocpy/2018/d2.py
felixm 3a915cb9e3 Start solving 2018 problems
I have also updated get.py to download the problems as
`d<day>.txt` instead of `i<day>.txt`. That allows me
to get the day input via `__input__.replace('.py', '.txt')`
which is a little more concise. I don't know why
I didn't do this earlier.
2024-07-04 11:10:27 -04:00

54 lines
1023 B
Python

from lib import LETTERS_LOWER
def part_1(data):
two_count, three_count = 0, 0
for line in data.splitlines():
count = [line.count(c) for c in LETTERS_LOWER if line.count(c) > 1]
if 2 in count:
two_count += 1
if 3 in count:
three_count += 1
r = two_count * three_count
print(r)
def equal(a, b):
c = 0
if len(a) != len(b):
return False
for i in range(len(a)):
if a[i] != b[i]:
c += 1
if c == 1:
return True
return False
def strip(a, b):
r = ""
for a, b in zip(a, b):
if a == b:
r += a
return r
def part_2(data):
lines = list(data.splitlines())
for i in range(len(lines)):
for j in range(i + 1, len(lines)):
a, b = lines[i], lines[j]
if equal(a, b):
print(strip(a, b))
def main():
with open("i2.txt") as f:
data = f.read()
part_1(data)
part_2(data)
if __name__ == "__main__":
main()