aoc2018/day02.py
2021-12-29 16:42:03 +01:00

54 lines
1.3 KiB
Python

from itertools import combinations
from tools.aoc import AOCDay
from typing import Any
class Day(AOCDay):
inputs = [
[
(12, "test_input2"),
(7410, "input2")
],
[
("fgij", "test_input2_2"),
("cnjxoritzhvbosyewrmqhgkul", "input2")
]
]
def part1(self) -> Any:
two_count = 0
three_count = 0
for box_id in self.getInput():
has_two = has_three = False
for c in box_id:
if box_id.count(c) == 2:
has_two = True
elif box_id.count(c) == 3:
has_three = True
two_count, three_count = two_count + has_two, three_count + has_three
return two_count * three_count
def part2(self) -> Any:
for a, b in combinations(self.getInput(), 2):
diff_count = 0
diff_index = 0
for i, c in enumerate(a):
if c != b[i]:
diff_count += 1
diff_index = i
if diff_count > 1:
break
if diff_count != 1:
continue
return b[:diff_index] + b[diff_index + 1:]
if __name__ == '__main__':
day = Day(2018, 2)
day.run(verbose=True)