Files
AdventOfCode2016/04/04-1.py
2024-12-18 23:45:17 +01:00

40 lines
1.1 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
file = "./input.txt"
#file = "./ex.txt"
from time import time
import re
start_time = time()
def sortiere_und_verbinde_buchstaben(dictionary:dict[str:int])->str:
sortierte_liste = sorted(dictionary.keys(), key=lambda x: (-dictionary[x], x))
return ''.join(sortierte_liste[:5])
def create_checksum(txt:str)-> str:
dic = {}
txt = "".join(txt.split("-"))
for b in txt:
if b in dic:
dic[b] = dic[b]+1
else:
dic[b] = 1
return sortiere_und_verbinde_buchstaben(dic)
if __name__ == "__main__":
sol = 0
input_file = open(file, "r")
for line in input_file:
line = line.strip()
if line == "":
continue
else:
pat = r'^([a-z-]+)-(\d+)\[([a-z]+)\]$'
match = re.match(pat,line)
check = create_checksum(match.group(1))
if check == match.group(3):
sol += int(match.group(2))
input_file.close()
print(f"Solution - Part1: {sol}")
print(f'Runtime: {time()-start_time:.4f} s')