89 lines
2.1 KiB
Python
89 lines
2.1 KiB
Python
#!/usr/bin/env python
|
|
|
|
# https://adventofcode.com/2025/day/10
|
|
|
|
from itertools import combinations, product
|
|
|
|
f = open("day10input.txt", "r")
|
|
|
|
# regular input
|
|
#input = f.readlines()
|
|
# testinput
|
|
input = "[.##.] (3) (1,3) (2) (2,3) (0,2) (0,1) {3,5,4,7}\n[...#.] (0,2,3,4) (2,3) (0,4) (0,1,2) (1,2,3,4) {7,5,12,7,2}\n[.###.#] (0,1,2,3,4) (0,3,4) (0,1,2,4,5) (1,2) {10,11,11,5,10,5}".split("\n")
|
|
|
|
machines = []
|
|
|
|
for input_line in input:
|
|
line = input_line.rstrip("\n").split(" ")
|
|
machine = {}
|
|
|
|
# final state
|
|
fs = line[0][1:-1].replace(".", "0").replace("#", "1")
|
|
machine["fs"] = int(fs, base=2)
|
|
|
|
# switches
|
|
switches = []
|
|
for switch in line[1:-1]:
|
|
s = ["0"] * len(fs)
|
|
for i in [int(j) for j in switch[1:-1].split(",")]:
|
|
s[i] = "1"
|
|
switches.append("".join(s))
|
|
machine["s"] = sorted([int(s, base=2) for s in switches], key=int.bit_count)
|
|
|
|
# joltage req's
|
|
machine["j"] = [int(n) for n in line[-1][1:-1].split(",")]
|
|
|
|
machines.append(machine)
|
|
|
|
|
|
def print_machines(machines: list[dict]) -> None:
|
|
for i in range(len(machines)):
|
|
print(f"Machine no. {i}")
|
|
for k in machines[i].keys():
|
|
print(f"key:\t{k}\tvalue:\t{machines[i][k]}")
|
|
|
|
|
|
def part1_bfs(m):
|
|
for steps in range(1, len(m["s"])):
|
|
# check if current step no yields a solution
|
|
for c in list(combinations(m["s"], steps)):
|
|
lights = 0
|
|
for i in c:
|
|
lights ^= i
|
|
if lights == m["fs"]:
|
|
print(f"Success with combo {i} and step count of {steps}")
|
|
return steps
|
|
return None
|
|
|
|
|
|
# PART 2 IS NOT WORKING YET
|
|
def part2_bfs(m):
|
|
l = len(m["j"])
|
|
switches = ["{0:b}".format(n).zfill(l) for n in m["s"]]
|
|
|
|
steps = 1
|
|
|
|
while True:
|
|
print(f"{steps=}")
|
|
# check if current step no yields a solution
|
|
for c in list(product(switches, repeat=steps)):
|
|
# print(f"{c=}")
|
|
j = []
|
|
for i in range(l):
|
|
j.append(sum([int(s[i]) for s in c]))
|
|
|
|
# print(f"{j}\t{m['j']}")
|
|
if j == m["j"]:
|
|
print(f"Success with combo {i} and step count of {steps}")
|
|
return steps
|
|
|
|
steps += 1
|
|
return None
|
|
|
|
|
|
|
|
|
|
print_machines(machines)
|
|
print(sum(list(map(part1_bfs, machines))))
|
|
#print(sum(list(map(part2_bfs, machines))))
|