summaryrefslogtreecommitdiffstats
path: root/prog/aoc
diff options
context:
space:
mode:
authorAnton Luka Šijanec <anton@sijanec.eu>2023-12-08 20:17:50 +0100
committerAnton Luka Šijanec <anton@sijanec.eu>2023-12-08 20:17:50 +0100
commita7d227edcbeefbeb37df118664ac9b202bb064fd (patch)
tree6e98d5b71606db0ba68001b54fbd0c67fac8b31c /prog/aoc
parentnew research (diff)
parentaoc8 (diff)
downloadr-a7d227edcbeefbeb37df118664ac9b202bb064fd.tar
r-a7d227edcbeefbeb37df118664ac9b202bb064fd.tar.gz
r-a7d227edcbeefbeb37df118664ac9b202bb064fd.tar.bz2
r-a7d227edcbeefbeb37df118664ac9b202bb064fd.tar.lz
r-a7d227edcbeefbeb37df118664ac9b202bb064fd.tar.xz
r-a7d227edcbeefbeb37df118664ac9b202bb064fd.tar.zst
r-a7d227edcbeefbeb37df118664ac9b202bb064fd.zip
Diffstat (limited to 'prog/aoc')
-rwxr-xr-xprog/aoc/23/5/1.py82
-rw-r--r--prog/aoc/23/5/in.txt33
-rwxr-xr-xprog/aoc/23/6/1.py32
-rw-r--r--prog/aoc/23/6/in.txt2
-rwxr-xr-xprog/aoc/23/7/1.py84
-rw-r--r--prog/aoc/23/7/in.txt5
-rwxr-xr-xprog/aoc/23/8/1.py43
-rw-r--r--prog/aoc/23/8/1.txt9
-rw-r--r--prog/aoc/23/8/2.txt5
-rw-r--r--prog/aoc/23/8/3.txt10
10 files changed, 305 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..a64ee26
--- /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])[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[0][0])
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
diff --git a/prog/aoc/23/6/1.py b/prog/aoc/23/6/1.py
new file mode 100755
index 0000000..1539b09
--- /dev/null
+++ b/prog/aoc/23/6/1.py
@@ -0,0 +1,32 @@
+#!/usr/bin/python3
+timestr = input().split(":")[1]
+time = timestr.split(" ")
+try:
+ while True:
+ time.remove("")
+except:
+ pass
+time = [x for x in map(int, time)]
+distancestr = input().split(":")[1]
+distance = distancestr.split(" ")
+try:
+ while True:
+ distance.remove("")
+except:
+ pass
+distance = [x for x in map(int, distance)]
+product = 1
+for i in range(len(time)):
+ ways = 0
+ for j in range(time[i]):
+ if (time[i]-j)*j > distance[i]:
+ ways += 1
+ product *= ways
+print(product)
+ways = 0
+t = int(timestr.replace(" ", ""))
+d = int(distancestr.replace(" ", ""))
+for j in range(t):
+ if (t-j)*j > d:
+ ways += 1
+print(ways)
diff --git a/prog/aoc/23/6/in.txt b/prog/aoc/23/6/in.txt
new file mode 100644
index 0000000..28f5ae9
--- /dev/null
+++ b/prog/aoc/23/6/in.txt
@@ -0,0 +1,2 @@
+Time: 7 15 30
+Distance: 9 40 200
diff --git a/prog/aoc/23/7/1.py b/prog/aoc/23/7/1.py
new file mode 100755
index 0000000..e1d66da
--- /dev/null
+++ b/prog/aoc/23/7/1.py
@@ -0,0 +1,84 @@
+#!/usr/bin/python3
+from functools import cmp_to_key
+hands = []
+try:
+ while True:
+ hands.append(input().split(" "))
+except EOFError:
+ pass
+alphabet = ["A", "K", "Q", "J", "T", "9", "8", "7", "6", "5", "4", "3", "2"];
+def level(a):
+ if len(set(a)) == 1:
+ return 6
+ if len(set(a)) == 2:
+ if a.count(a[0]) == 1 or a.count(a[0]) == 4:
+ return 5
+ return 4
+ if len(set(a)) == 3:
+ if a.count(list(set(a))[0]) == 3 or a.count(list(set(a))[1]) == 3 or a.count(list(set(a))[2]) == 3:
+ return 3
+ return 2
+ if len(set(a)) == 5:
+ return 0
+ return 1
+def compar(a, b):
+ a = a[0]
+ b = b[0]
+ if level(a) > level(b):
+ return 1
+ if level(b) > level(a):
+ return -1
+ for i in range(len(a)):
+ if alphabet.index(a[i]) < alphabet.index(b[i]):
+ return 1
+ if alphabet.index(b[i]) < alphabet.index(a[i]):
+ return -1
+ return 0
+hands.sort(key=cmp_to_key(compar))
+i = 1
+s = 0
+for h in hands:
+ print(h, level(h[0]))
+ s += int(h[1])*i
+ i += 1
+print(s)
+alphabet = ["A", "K", "Q", "T", "9", "8", "7", "6", "5", "4", "3", "2", "J"];
+def level(a):
+ r = set(a)-set(["J"])
+ if len(r) == 0 or len(r) == 1:
+ return 6
+ if len(r) == 5:
+ return 0
+ if len(r) == 2:
+ c2 = a.count("J")+a.count(list(r)[0])
+ c1 = a.count("J")+a.count(list(r)[1])
+ if c1 == 4 or c2 == 4:
+ return 5
+ return 4
+ if len(r) == 3:
+ l = a.count("J")
+ if l+a.count(list(set(r))[0]) == 3 or l+a.count(list(set(r))[1]) == 3 or l+a.count(list(set(r))[2]) == 3:
+ return 3
+ return 2
+ return 1
+def compar(a, b):
+ a = a[0]
+ b = b[0]
+ if level(a) > level(b):
+ return 1
+ if level(b) > level(a):
+ return -1
+ for i in range(len(a)):
+ if alphabet.index(a[i]) < alphabet.index(b[i]):
+ return 1
+ if alphabet.index(b[i]) < alphabet.index(a[i]):
+ return -1
+ return 0
+hands.sort(key=cmp_to_key(compar))
+i = 1
+s = 0
+for h in hands:
+ print(h, level(h[0]))
+ s += int(h[1])*i
+ i += 1
+print(s)
diff --git a/prog/aoc/23/7/in.txt b/prog/aoc/23/7/in.txt
new file mode 100644
index 0000000..e3500c3
--- /dev/null
+++ b/prog/aoc/23/7/in.txt
@@ -0,0 +1,5 @@
+32T3K 765
+T55J5 684
+KK677 28
+KTJJT 220
+QQQJA 483
diff --git a/prog/aoc/23/8/1.py b/prog/aoc/23/8/1.py
new file mode 100755
index 0000000..9ff0113
--- /dev/null
+++ b/prog/aoc/23/8/1.py
@@ -0,0 +1,43 @@
+#!/usr/bin/python3
+inst = input()
+input()
+graph = {}
+try:
+ while True:
+ a = input().split(" = ")
+ graph[a[0]] = a[1].replace("(", "").replace(")", "").split(", ")
+except EOFError:
+ pass
+cur = "AAA"
+runs = 0
+while True:
+ for d in inst:
+ if cur == "ZZZ":
+ break
+ if d == "L":
+ cur = graph[cur][0]
+ else:
+ cur = graph[cur][1]
+ runs += 1
+ else:
+ continue
+ break
+print(runs)
+r = []
+for s in [x for x in graph if x[2] == "A"]:
+ runs = 0
+ while True:
+ for d in inst:
+ if s[2] == "Z":
+ break
+ if d == "L":
+ s = graph[s][0]
+ else:
+ s = graph[s][1]
+ runs += 1
+ else:
+ continue
+ break
+ r.append(runs)
+from math import lcm
+print(lcm(*r))
diff --git a/prog/aoc/23/8/1.txt b/prog/aoc/23/8/1.txt
new file mode 100644
index 0000000..9029a1b
--- /dev/null
+++ b/prog/aoc/23/8/1.txt
@@ -0,0 +1,9 @@
+RL
+
+AAA = (BBB, CCC)
+BBB = (DDD, EEE)
+CCC = (ZZZ, GGG)
+DDD = (DDD, DDD)
+EEE = (EEE, EEE)
+GGG = (GGG, GGG)
+ZZZ = (ZZZ, ZZZ)
diff --git a/prog/aoc/23/8/2.txt b/prog/aoc/23/8/2.txt
new file mode 100644
index 0000000..7d1b58d
--- /dev/null
+++ b/prog/aoc/23/8/2.txt
@@ -0,0 +1,5 @@
+LLR
+
+AAA = (BBB, BBB)
+BBB = (AAA, ZZZ)
+ZZZ = (ZZZ, ZZZ)
diff --git a/prog/aoc/23/8/3.txt b/prog/aoc/23/8/3.txt
new file mode 100644
index 0000000..5b3fa58
--- /dev/null
+++ b/prog/aoc/23/8/3.txt
@@ -0,0 +1,10 @@
+LR
+
+11A = (11B, XXX)
+11B = (XXX, 11Z)
+11Z = (11B, XXX)
+22A = (22B, XXX)
+22B = (22C, 22C)
+22C = (22Z, 22Z)
+22Z = (22B, 22B)
+XXX = (XXX, XXX)