45 lines
1.0 KiB
Python
45 lines
1.0 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
file = "./input.txt"
|
|
#file = "./ex.txt"
|
|
from time import time
|
|
start_time = time()
|
|
|
|
def read_file(filename:str)->list[str]:
|
|
result = []
|
|
input_file = open(filename, "r")
|
|
for line in input_file:
|
|
line = line.strip()
|
|
if line == "":
|
|
continue
|
|
else:
|
|
result.append(line)
|
|
input_file.close()
|
|
return result
|
|
|
|
def counter_dicts(da:list[str])->list[dict[str:int]]:
|
|
result = [{} for _ in range(len(da[0]))]
|
|
for d in da:
|
|
for i,b in enumerate(d):
|
|
if b in result[i]:
|
|
result[i][b] += 1
|
|
else:
|
|
result[i][b] = 1
|
|
return result
|
|
|
|
if __name__ == "__main__":
|
|
data = read_file(file)
|
|
dicts = counter_dicts(data)
|
|
|
|
sol = ""
|
|
for dic in dicts:
|
|
sol += max(dic, key=dic.get)
|
|
print(f"Solution Part1: {sol}")
|
|
|
|
sol = ""
|
|
for dic in dicts:
|
|
sol += min(dic, key=dic.get)
|
|
print(f"Solution Part2: {sol}")
|
|
|
|
print(f'Runtime: {time()-start_time:.4f} s') |