diff --git a/5/advent_1.py b/5/advent_1.py index f177f7f..2037abf 100755 --- a/5/advent_1.py +++ b/5/advent_1.py @@ -1,15 +1,60 @@ #!/usr/bin/env python3 from dataclasses import dataclass +import re @dataclass class Range: - start: int - last: int + target: int + source: int + length: int with open("data.txt") as file: lines = [line.rstrip() for line in file] -state = "seeds" -for i in lines: - match state: \ No newline at end of file +tables = [] +table = [] +seeds = [] + +for line in lines: + if line == '': + print('Tühi rida') + continue + if line.startswith("seeds: "): + temp = re.findall(r'\d+', line) + seeds = list(map(int, temp)) + continue + + if line[0].isalpha(): + if len(table)>0: + tables.append(table) + table = [] + continue + + temp = re.findall(r'\d+', line) + tableitems = list(map(int, temp)) + table.append(Range(*tableitems)) + +print(seeds) +print(tables) + +pisim = 0 +for arv in seeds: + print(arv) + for table in tables: + muudetud = False + print("tabel") + for mapping in table: + if arv >= mapping.source and arv < (mapping.source + mapping.length): + print(mapping) + arv = mapping.target + (arv - mapping.source) + print(f"Uus arv: {arv}") + break + + print(arv) + if pisim == 0: + pisim = arv + if pisim > arv: + pisim = arv + +print(pisim) \ No newline at end of file