summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnton Luka Šijanec <anton@sijanec.eu>2023-12-06 00:53:09 +0100
committerAnton Luka Šijanec <anton@sijanec.eu>2023-12-06 00:53:09 +0100
commit67b9ee7cb165998a478c2f81ba4ff5d566e0ae4a (patch)
tree9dce4e1f36dd82a2a56650576fcbce6334d5f068
parentkolokvij1.lyx (diff)
downloadr-67b9ee7cb165998a478c2f81ba4ff5d566e0ae4a.tar
r-67b9ee7cb165998a478c2f81ba4ff5d566e0ae4a.tar.gz
r-67b9ee7cb165998a478c2f81ba4ff5d566e0ae4a.tar.bz2
r-67b9ee7cb165998a478c2f81ba4ff5d566e0ae4a.tar.lz
r-67b9ee7cb165998a478c2f81ba4ff5d566e0ae4a.tar.xz
r-67b9ee7cb165998a478c2f81ba4ff5d566e0ae4a.tar.zst
r-67b9ee7cb165998a478c2f81ba4ff5d566e0ae4a.zip
-rwxr-xr-xprog/aoc/23/5/1.py82
-rw-r--r--prog/aoc/23/5/in.txt33
2 files changed, 115 insertions, 0 deletions
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