From a76b1148372688af1acdf7a2937132e143ef0c87 Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Fri, 8 Dec 2023 12:36:03 +0100 Subject: [PATCH] updated gitignore --- .gitignore | 1 + 2023/day8/p2.py | 47 +++++++++++++++++++ 2023/day8/part2.py | 113 +++++++++++++++++++++++++++++++++++++++++++++ 2023/day8/test_p2 | 10 ++++ 4 files changed, 171 insertions(+) create mode 100644 2023/day8/p2.py create mode 100644 2023/day8/part2.py create mode 100644 2023/day8/test_p2 diff --git a/.gitignore b/.gitignore index 6a0eb28..e70e186 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ input test +p2.py diff --git a/2023/day8/p2.py b/2023/day8/p2.py new file mode 100644 index 0000000..a229195 --- /dev/null +++ b/2023/day8/p2.py @@ -0,0 +1,47 @@ +from math import gcd +import sys + +steps, _, *rest = open(sys.argv[1]).read().splitlines() + +network = {} + +for line in rest: + pos, targets = line.split(" = ") + network[pos] = targets[1:-1].split(", ") + +positions = [key for key in network if key.endswith("A")] +cycles = [] + +for current in positions: + cycle = [] + + current_steps = steps + step_count = 0 + first_z = None + + while True: + print(current_steps) + while step_count == 0 or not current.endswith("Z"): + step_count += 1 + current = network[current][0 if current_steps[0] == "L" else 1] + print(current) + current_steps = current_steps[1:] + current_steps[0] + print(current_steps) + cycle.append(step_count) + + if first_z is None: + first_z = current + step_count = 0 + elif current == first_z: + break + + cycles.append(cycle) + +nums = [cycle[0] for cycle in cycles] + +lcm = nums.pop() + +for num in nums: + lcm = lcm * num // gcd(lcm, num) + +print(lcm) diff --git a/2023/day8/part2.py b/2023/day8/part2.py new file mode 100644 index 0000000..25c77ec --- /dev/null +++ b/2023/day8/part2.py @@ -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)) diff --git a/2023/day8/test_p2 b/2023/day8/test_p2 new file mode 100644 index 0000000..5b3fa58 --- /dev/null +++ b/2023/day8/test_p2 @@ -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)