From a1df3dee02bad35b1f0561c972c2981de3884545 Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Fri, 8 Dec 2023 13:26:14 +0100 Subject: [PATCH] Optimizing 2023-12-08 --- 2023/day8/.part2.py.swp | Bin 0 -> 12288 bytes 2023/day8/part2.py | 93 ++++++--------------------------- 2023/day8/part2.py.bk | 113 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+), 76 deletions(-) create mode 100644 2023/day8/.part2.py.swp create mode 100644 2023/day8/part2.py.bk diff --git a/2023/day8/.part2.py.swp b/2023/day8/.part2.py.swp new file mode 100644 index 0000000000000000000000000000000000000000..8f8a2d1b86ee63d11701a12b2c63f977631d475b GIT binary patch literal 12288 zcmeI2&1=*^7>B1`Jk+Y_!4Cwdde~jv@2y%^dsvF7#Sajx*lk&|n@Kx0I|<38b`k5% zlRbM>{{%gHs(**x1aE@Xn;`g`Nmi{CFZEJ*0}qp#H}5-dp5(G@y4AbaZt%%+jo~=R z*vx1wCcf`wGq)LYqr?pY6ZrSximr52slM1Pd9kXMY^`ll*3yENmb)U9zU55^uBgC>THcejeLfpU{u;9>4P<}}kO4A42FL&zAOmE843GgbKnBRb zE;Qh{j2+p>*ytFV$N&Gs-~XEj82bV~f%jkoyaemu8AyNz65I!uK^1Ut5`5dw*jKO# zK7zO41$YV`fMw7F^I#U-0aw8la0y%lMQ{R)f*lkb~Q$H5ISB~W3l19LeTSLC^OvDUZ11pZ72t@2?L$2_6rz4frolf4= zE!tSo@e1WgIBvdBjzeGSd~ROlGQH@T zr_xiWW^RsjZTa`25wvUs$@C@pC2SRs8m)$5e$}*)?Z|O2@gOcc(c((G21BwR29f3} z>4yXE#9W2Ui-JC$i0=RGX*_);coxcL!)n-Jcp3?O_&Z+dN>vF5dMQv1 F_7ec^9;pBT literal 0 HcmV?d00001 diff --git a/2023/day8/part2.py b/2023/day8/part2.py index 25c77ec..fc0059e 100644 --- a/2023/day8/part2.py +++ b/2023/day8/part2.py @@ -17,13 +17,19 @@ def pp(x): d, *maps = open(input_f).read().split('\n') +class Node(): + def __init__(self,value,left,right): + self.value = value + self.left = left + self.right = right + s = [] l = [] r = [] maps = maps -nmaps = [] +nodes = {} for i in maps: if i == '': @@ -31,83 +37,18 @@ for i in maps: s = i[:3] l = i[7:10] r = i[12:15] - nmaps.append([s,l,r]) + nodes[s] = Node(s,l,r) +pprint(nodes) -#pprint(d) -#pprint(maps) -#pprint(nmaps) - -count = 0 -way = 'AAA' -first = True -found = False +current = 'AAA' +steps = 0 index = 0 +directions = d +while current != 'ZZZ': + steps += 1 + directions = directions[index] + if directions == 'L': -way = [] -for i in nmaps: - if i[0].endswith('A'): - way.append(i[0]) - -print(way) - -cycles = [] - -print(d) - -for cur in way: - cycle = [] - - count = 0 - cur_d = d - - while True: - while count == 0 or not cur.endswith('Z'): - - - count += 1 - - index = [(i,table.index(cur)) for i, table in enumerate(nmaps) if cur in table] - - for j in index: - if list(j)[1] == 0: - index = list(j)[0] - if cur_d[0] == 'R': - cur = nmaps[index][2] - if cur_d[0] == 'L': - cur = nmaps[index][1] - - - print(cur) - - cur_d = cur_d[1:] + cur_d[0] - - print(cur_d) - input() - - cycle.append(count) - cycles.append(cycle) - - - -print(cycles) - -""" -while found == False: - i = d[count%len(d)] - c = way - index = [(i,table.index(c)) for i, table in enumerate(nmaps) if c in table] - for j in index: - if list(j)[1] == 0: - index = list(j)[0] - if i == 'R': - way = nmaps[index][2] - if i == 'L': - way = nmaps[index][1] - count += 1 - if way == 'ZZZ': - found = True - continue -""" -print('Result: ' + str(count)) +print(steps) diff --git a/2023/day8/part2.py.bk b/2023/day8/part2.py.bk new file mode 100644 index 0000000..25c77ec --- /dev/null +++ b/2023/day8/part2.py.bk @@ -0,0 +1,113 @@ +#!/bin/python3 + +import re +import sys +from pprint import pprint +from collections import Counter +import numpy as np + +input_f = sys.argv[1] + +def pp(x): + for i in x: + for j in i: + print(j) + print() + +d, *maps = open(input_f).read().split('\n') + + +s = [] +l = [] +r = [] + +maps = maps + +nmaps = [] + +for i in maps: + if i == '': + continue + s = i[:3] + l = i[7:10] + r = i[12:15] + nmaps.append([s,l,r]) + + +#pprint(d) +#pprint(maps) +#pprint(nmaps) + +count = 0 +way = 'AAA' +first = True +found = False +index = 0 + +way = [] + +for i in nmaps: + if i[0].endswith('A'): + way.append(i[0]) + +print(way) + +cycles = [] + +print(d) + +for cur in way: + cycle = [] + + count = 0 + cur_d = d + + while True: + while count == 0 or not cur.endswith('Z'): + + + count += 1 + + index = [(i,table.index(cur)) for i, table in enumerate(nmaps) if cur in table] + + for j in index: + if list(j)[1] == 0: + index = list(j)[0] + if cur_d[0] == 'R': + cur = nmaps[index][2] + if cur_d[0] == 'L': + cur = nmaps[index][1] + + + print(cur) + + cur_d = cur_d[1:] + cur_d[0] + + print(cur_d) + input() + + cycle.append(count) + cycles.append(cycle) + + + +print(cycles) + +""" +while found == False: + i = d[count%len(d)] + c = way + index = [(i,table.index(c)) for i, table in enumerate(nmaps) if c in table] + for j in index: + if list(j)[1] == 0: + index = list(j)[0] + if i == 'R': + way = nmaps[index][2] + if i == 'L': + way = nmaps[index][1] + count += 1 + if way == 'ZZZ': + found = True + continue +""" +print('Result: ' + str(count))