From 67b9ee7cb165998a478c2f81ba4ff5d566e0ae4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20Luka=20=C5=A0ijanec?= Date: Wed, 6 Dec 2023 00:53:09 +0100 Subject: =?UTF-8?q?aoc5:=20with=20sweat=20and=20tears,=20jutri=C5=A1nji=20?= =?UTF-8?q?bo=20verjetno=20nere=C5=A1ljiv?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- prog/aoc/23/5/1.py | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++ prog/aoc/23/5/in.txt | 33 +++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100755 prog/aoc/23/5/1.py create mode 100644 prog/aoc/23/5/in.txt (limited to 'prog/aoc') diff --git a/prog/aoc/23/5/1.py b/prog/aoc/23/5/1.py new file mode 100755 index 0000000..be46b5f --- /dev/null +++ b/prog/aoc/23/5/1.py @@ -0,0 +1,82 @@ +#!/usr/bin/python3 +seeds = [] +maps = [] +from os import getenv +file = open("/root/dl/input5") +if getenv("IN") != None: + file = open("in.txt") +seeds.extend([x for x in map(int, file.readline().strip().split(": ")[1].split(" "))]) +file.readline().strip() +def read_map(): + l = file.readline() + # s = l.split("-")[0] # ne rabimo + # d = l.split("-")[2].split(" ")[0] # ne rabimo + l = file.readline() + v = [] + while l != "\n" and l != "": + v.append((int(l.strip().split(" ")[0]), int(l.strip().split(" ")[1]), int(l.strip().split(" ")[2]))) + l = file.readline() + maps.append(sorted(v, key=lambda x: x[1])) + if l == "": + return False + return True +while read_map(): + pass +def get_next_step(target, steps): # možnost izboljšave z bisekcijo + def helper(): + for index in range(len(steps)): + if index+1 == len(steps): + return steps[index] + if steps[index+1][1] > target: + return steps[index] + r = helper() + # print("r", r) + if r[1] > target: + return target + if r[1] + r[2] - 1 < target: + return target + return r[0]+target-r[1] +locations = {} +for s in seeds: + l = get_next_step(s, maps[0]) + # print("l", l) + for m in maps[1:]: + l = get_next_step(l, m) + # print("l", l) + locations[s] = l +print(min(locations.items(), key=lambda x: x[1])) +from interval import interval, inf +semena = interval() +for i in range(len(seeds)//2): + semena |= interval([seeds[2*i], seeds[2*i]+seeds[2*i+1]]) +print(semena) +def celoštevilski_komplement(intervala): + r = interval() + for i in range(len(intervala)): + if i == 0: + if intervala[i][0] == -inf: + continue + r |= interval([-inf, intervala[i][0]-1]) + if i+1 == len(intervala): + if intervala[i][1] == inf: + continue + r |= interval([intervala[i][1]+1, inf]) + if i != 0: + r |= interval([intervala[i-1][1]+1, intervala[i][0]-1]) + return r +def go_deeper(m, vhod): + r = interval() + porabljeni_sources = interval() + for m in m: + source = interval([m[1], m[1]+m[2]]) + # print("source", source, "!source", celoštevilski_komplement(source)) + # print("m", m, "source", source, "source^C", celoštevilski_komplement(source)) + r |= (source & vhod) + m[0] - m[1] + porabljeni_sources |= source + vhod &= celoštevilski_komplement(porabljeni_sources) + r |= vhod + # print("go_deeper r", r) + return r +for i in range(len(maps)): + semena = go_deeper(maps[i], semena) +print(semena) diff --git a/prog/aoc/23/5/in.txt b/prog/aoc/23/5/in.txt new file mode 100644 index 0000000..f756727 --- /dev/null +++ b/prog/aoc/23/5/in.txt @@ -0,0 +1,33 @@ +seeds: 79 14 55 13 + +seed-to-soil map: +50 98 2 +52 50 48 + +soil-to-fertilizer map: +0 15 37 +37 52 2 +39 0 15 + +fertilizer-to-water map: +49 53 8 +0 11 42 +42 0 7 +57 7 4 + +water-to-light map: +88 18 7 +18 25 70 + +light-to-temperature map: +45 77 23 +81 45 19 +68 64 13 + +temperature-to-humidity map: +0 69 1 +1 0 69 + +humidity-to-location map: +60 56 37 +56 93 4 -- cgit v1.2.3