From 190d3a87e242e6062acb06072ee5634f2857476f Mon Sep 17 00:00:00 2001 From: Lauri Jesmin Date: Tue, 5 Dec 2023 16:11:17 +0200 Subject: [PATCH] pooleli --- 5/advent_2.py | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100755 5/advent_2.py diff --git a/5/advent_2.py b/5/advent_2.py new file mode 100755 index 0000000..eba2efd --- /dev/null +++ b/5/advent_2.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python3 + +from dataclasses import dataclass +import re +import sys + + +@dataclass +class Range: + target: int + source: int + length: int + + +@dataclass +class SR: + source: int + length: int + + +def findmapping(sr,tables): + if len(tables) == 0: + return + table = tables.pop(0) + for row in table: + if sr.source > row. + print(sr) + + +with open("data.txt") as file: + lines = [line.rstrip() for line in file] + +tables = [] +table = [] +seeds = [] +seedranges = [] +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)) + print(len(seeds)) + for i in range(0, int(len(seeds)/2)): + seedranges.append(SR(seeds[i*2], seeds[i*2+1])) + print(seedranges) + 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)) +tables.append(table) + + +for table in tables: + print("tabel") + for rida in table: + print(rida) + +pisim = 0 +for sr in seedranges: + findmapping(sr,tables) + + 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(f"Vastus: {arv}") + if pisim == 0: + pisim = arv + if pisim > arv: + pisim = arv + +print(pisim)