summaryrefslogtreecommitdiff
path: root/2020/aoc2020-d23.py
diff options
context:
space:
mode:
authorblenovo <bk@gmail.com>2025-08-06 00:51:06 +0200
committerblenovo <bk@gmail.com>2025-08-06 00:51:06 +0200
commit0a441ce2306ae9edefb009fbc0596f4d511cdc7f (patch)
tree11d5aca4383bbce8a35acb912698f473b8180215 /2020/aoc2020-d23.py
parent12f551dbfef138880b29ba6abe4576714ae942cb (diff)
summertime warmup session - finish
Diffstat (limited to '2020/aoc2020-d23.py')
-rw-r--r--2020/aoc2020-d23.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/2020/aoc2020-d23.py b/2020/aoc2020-d23.py
new file mode 100644
index 0000000..b0bb6c0
--- /dev/null
+++ b/2020/aoc2020-d23.py
@@ -0,0 +1,58 @@
+#advent of code 2020
+#day 23
+#kinda cheated, I used another guy's solution for debugging
+# https://0xdf.gitlab.io/adventofcode2020/23 (cool source btw)
+#turns out my dictionary was getting one value from input replaced
+#which caused "cutting off" most of the other numbers
+#also tried filling out the dictionary with all numbers
+#but I was getting confused by different indexation
+#puzzle uses (0,1mil] instead of "standard" [0,1mil)
+#also minor mistakes in substitution ordering
+PuzzleInput = open("23.in","r").read().replace("\n","");
+cups = [int(v) for v in PuzzleInput];
+
+def cupgame(cuplabels, lim, cuplim):
+ cupdict = dict();
+ for c_i in range(len(cuplabels) -1):
+ cupdict[cuplabels[c_i]] = cuplabels[c_i+1];
+ if cuplim == len(cuplabels):
+ cupdict[cuplabels[-1]] = cuplabels[0];
+ else:
+ cupdict[cuplabels[-1]] = 10;
+ for c_i in range(len(cuplabels)+1,cuplim):
+ cupdict[c_i] = c_i + 1;
+ cupdict[ cuplim ] = cuplabels[0];
+
+ c0 = 0 + cuplabels[0];
+ for step in range(lim):
+ if step%1000000 == 0: print(step//1000000,"/",lim//1000000);
+ c1 = cupdict[c0] #,(c0+1)%(cuplim+1) +1);
+ c2 = cupdict[c1] #,(c1+1)%(cuplim+1) +1);
+ c3 = cupdict[c2] #,(c2+1)%(cuplim+1) +1);
+ cupdict[c0] = cupdict[c3] #,(c3+1)%(cuplim+1) +1);
+ d0 = c0 - 1;
+ if d0 == 0: d0 = cuplim;
+ while True:
+ if d0 not in [c1,c2,c3]: break;
+ d0 -= 1;
+ if d0 == 0: d0 = cuplim;
+ cupdict[c3] = cupdict[d0];
+ cupdict[d0] = c1;
+ c0 = cupdict[c0];
+
+ if cuplim == len(cuplabels):
+ solution = "";
+ NextVal = 1;
+ for s in range(8):
+ NextVal = cupdict[NextVal];
+ solution += str(NextVal);
+ else:
+ solution = cupdict[1] * cupdict[cupdict[1]];
+
+ return solution;
+
+p1 = cupgame(cups,100, len(cups));
+p2 = cupgame(cups,10000000, 1000000);
+print("part 1 =",p1);
+print("part 2 =",p2);
+