summaryrefslogtreecommitdiff
path: root/2020/aoc2020-d23.py
blob: b0bb6c02bd77c5876d8aab9ff5dad7665aaf253f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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);