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.
This commit is contained in:
53
2018/d2.py
Normal file
53
2018/d2.py
Normal file
@@ -0,0 +1,53 @@
|
||||
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()
|
||||
Reference in New Issue
Block a user