Compare commits

...

2 Commits

Author SHA1 Message Date
d9fe2d6dd0 Add first attempt at problem 8 2025-12-08 23:47:43 -05:00
9170d0c97c Add solution for problem 7 2025-12-07 12:03:56 -05:00
6 changed files with 1408 additions and 0 deletions

66
Problem7_1.py Normal file
View File

@@ -0,0 +1,66 @@
def start() :
location = tachyonManifold[0].find("S")
tachyonManifold[1] = tachyonManifold[1][0:location] + "|" + tachyonManifold[1][location + 1:]
def registerSplits(row: int) :
numSplits = 0
numExtensions = 0
for cnt in range(len(tachyonManifold[row])) :
if row > 0 and tachyonManifold[row - 1][cnt] == "|":
if tachyonManifold[row][cnt] == "." :
tachyonManifold[row] = tachyonManifold[row][0:cnt] + "|" + tachyonManifold[row][cnt + 1:]
numExtensions += 1
elif tachyonManifold[row][cnt] == "^":
if cnt > 0 and cnt + 1 < len(tachyonManifold[row]) :
tachyonManifold[row] = tachyonManifold[row][0:cnt - 1] + "|^|" + tachyonManifold[row][cnt + 2:]
elif cnt == 0 :
tachyonManifold[row] = "^|" + tachyonManifold[row][2:]
else :
tachyonManifold[row] = tachyonManifold[row][0:-2] + "|^"
numSplits += 1
return numSplits
def printManifold() -> int:
for line in tachyonManifold :
print(line)
print("\n")
testInput = [
".......S.......",
"...............",
".......^.......",
"...............",
"......^.^......",
"...............",
".....^.^.^.....",
"...............",
"....^.^...^....",
"...............",
"...^.^...^.^...",
"...............",
"..^...^.....^..",
"...............",
".^.^.^.^.^...^.",
"..............."
]
def readFile() :
ary = []
with open("files/Problem7.txt", "r") as file :
for line in file :
ary.append(line.replace("\n", ""))
return ary
#tachyonManifold = testInput
tachyonManifold = readFile()
start()
#printManifold()
totalSplits = 0
for row in range(1, len(tachyonManifold)) :
numberSplits = registerSplits(row)
totalSplits += numberSplits
#print(f"Row = {row}")
#printManifold()
print(f"Total splits = {totalSplits}")
#Total splits = 1562

84
Problem7_2.py Normal file
View File

@@ -0,0 +1,84 @@
def start() :
location = tachyonManifold[0].find("S")
tachyonManifold[1] = tachyonManifold[1][0:location] + "|" + tachyonManifold[1][location + 1:]
accumulator[location] = 1
def registerSplits(row: int) :
numSplits = 0
numExtensions = 0
newAccumulator = []
newManifold = tachyonManifold[row]
for cnt in range(len(accumulator)) :
newAccumulator.append(0)
for cnt in range(len(tachyonManifold[row])) :
char = tachyonManifold[row][cnt]
if row > 0 and tachyonManifold[row - 1][cnt] == "|":
if tachyonManifold[row][cnt] == "." :
newManifold = newManifold[0:cnt] + "|" + newManifold[cnt + 1:]
newAccumulator[cnt] += accumulator[cnt]
numExtensions += 1
elif tachyonManifold[row][cnt] == "^":
if cnt > 0 and cnt + 1 < len(tachyonManifold[row]) :
newManifold = newManifold[0:cnt - 1] + "|^|" + newManifold[cnt + 2:]
newAccumulator[cnt - 1] += accumulator[cnt]
newAccumulator[cnt + 1] += accumulator[cnt]
elif cnt == 0 :
newManifold = "^|" + newManifold[2:]
newAccumulator[cnt + 1] += accumulator[cnt]
else :
newManifold = newManifold[0:-2] + "|^"
newAccumulator[cnt - 1] += accumulator[cnt]
numSplits += 1
elif tachyonManifold[row][cnt] == "|" :
newAccumulator[cnt] = accumulator[cnt]
tachyonManifold[row] = newManifold
return numSplits, newAccumulator
def printManifold() -> int:
for line in tachyonManifold :
print(line)
print("\n")
testInput = [
".......S.......",
"...............",
".......^.......",
"...............",
"......^.^......",
"...............",
".....^.^.^.....",
"...............",
"....^.^...^....",
"...............",
"...^.^...^.^...",
"...............",
"..^...^.....^..",
"...............",
".^.^.^.^.^...^.",
"..............."
]
def readFile() :
ary = []
with open("files/Problem7.txt", "r") as file :
for line in file :
ary.append(line.replace("\n", ""))
return ary
#tachyonManifold = testInput
tachyonManifold = readFile()
accumulator: list[int] = []
for cnt in range(len(tachyonManifold[0])) :
accumulator.append(0)
start()
#printManifold()
totalSplits = 0
for row in range(1, len(tachyonManifold)) :
numberSplits, newAccumulator = registerSplits(row)
totalSplits += numberSplits
accumulator = newAccumulator
#print(f"Row = {row}")
#printManifold()
print(f"Total splits = {totalSplits}, Accumulator = {sum(accumulator)}")
#Total splits = 1562, Accumulator = 24292631346665

116
Problem8_1.py Normal file
View File

@@ -0,0 +1,116 @@
import math
MAX_NUM = 9999999999999999999999999999
def parseTuples(rawInput: list[str]) -> list[tuple[int, int, int]] :
tuples: list[tuple[int, int, int]] = []
for line in rawInput :
nums = line.split(",")
tuples.append((int(nums[0]), int(nums[1]), int(nums[2])))
return tuples
def calcDistance(first: tuple[int, int, int], second: tuple[int, int, int]) -> int :
return math.sqrt((first[0] - second[0])**2 + (first[1] - second[1])**2 + (first[2] - second[2])**2)
def addToConnections(minConnection: tuple[int, int]) :
for circuitCnt in range(len(connections)) :
circuit = connections[circuitCnt]
for boxCnt in range(len(circuit)) :
box = circuit[boxCnt]
if minConnection[0] == box[0] or minConnection[1] == box[0] or minConnection[0] == box[1] or minConnection[1] == box[1]:
circuit.append(minConnection)
return (circuitCnt, len(connections[circuitCnt]) - 1)
connections.append([])
connections[-1].append(minConnection)
return (len(connections) - 1, 0)
def clearRedundantConnections(newConnectionLocation: tuple[int, int]) :
newConnection = connections[newConnectionLocation[0]][newConnectionLocation[1]]
for box in connections[newConnectionLocation[0]] :
distances[box[0]][newConnection[0]] = MAX_NUM
distances[box[0]][newConnection[1]] = MAX_NUM
distances[box[1]][newConnection[0]] = MAX_NUM
distances[box[1]][newConnection[1]] = MAX_NUM
distances[newConnection[0]][box[0]] = MAX_NUM
distances[newConnection[0]][box[1]] = MAX_NUM
distances[newConnection[1]][box[0]] = MAX_NUM
distances[newConnection[1]][box[1]] = MAX_NUM
def getUniqueBoxes(circuit: list[tuple[int, int]]) -> list[int]:
boxes: list[int] = []
boxes.append(circuit[0][0])
boxes.append(circuit[0][1])
for box in circuit :
if not box[0] in boxes :
boxes.append(box[0])
elif not box[1] in boxes :
boxes.append(box[1])
return boxes
testInput = [
"162,817,812",
"57,618,57",
"906,360,560",
"592,479,940",
"352,342,300",
"466,668,158",
"542,29,236",
"431,825,988",
"739,650,466",
"52,470,668",
"216,146,977",
"819,987,18",
"117,168,530",
"805,96,715",
"346,949,466",
"970,615,88",
"941,993,340",
"862,61,35",
"984,92,344",
"425,690,689"
]
def readFile() :
ary = []
with open("files/Problem8.txt", "r") as file :
for line in file :
ary.append(line.replace("\n", ""))
return ary
#rawInput = testInput
rawInput = readFile()
parsedInput = parseTuples(rawInput)
distances: list[list[int]] = []
for outerCnt in range(len(parsedInput)) :
distances.append([])
for innerCnt in range(len(parsedInput)) :
if innerCnt == outerCnt :
distances[-1].append(MAX_NUM)
else :
distances[-1].append(calcDistance(parsedInput[outerCnt], parsedInput[innerCnt]))
connections: list[list[tuple[int, int]]] = []
minDistance = MAX_NUM
minConnection = (MAX_NUM, MAX_NUM)
for cnt in range(1000) :
print(f"number = {cnt}")
minDistance = MAX_NUM
minConnection = (MAX_NUM, MAX_NUM)
for box1Cnt in range(len(distances)) :
for box2Cnt in range(len(distances[box1Cnt])) :
if distances[box1Cnt][box2Cnt] < minDistance :
minDistance = distances[box1Cnt][box2Cnt]
minConnection = (box1Cnt, box2Cnt)
connection = addToConnections(minConnection)
clearRedundantConnections(connection)
sizes: list[int] = []
for circuit in connections :
sizes.append(len(getUniqueBoxes(circuit)))
sizes.sort(reverse=True)
print(f"sizes = {sizes}")
print(f"minConnections = {connections}, multiplication = {sizes[0] * sizes[1] * sizes[2]}")
#Too low 6000

0
Problem8_2.py Normal file
View File

142
files/Problem7.txt Normal file
View File

@@ -0,0 +1,142 @@
......................................................................S......................................................................
.............................................................................................................................................
......................................................................^......................................................................
.............................................................................................................................................
.....................................................................^.^.....................................................................
.............................................................................................................................................
....................................................................^.^.^....................................................................
.............................................................................................................................................
...................................................................^.....^...................................................................
.............................................................................................................................................
..................................................................^.^.^.^.^..................................................................
.............................................................................................................................................
.................................................................^.^...^...^.................................................................
.............................................................................................................................................
................................................................^.^.^.^.^.^.^................................................................
.............................................................................................................................................
...............................................................^.^.^.....^.^.^...............................................................
.............................................................................................................................................
..............................................................^.^.^.^...^.^.^.^..............................................................
.............................................................................................................................................
.............................................................^.^.^.^.......^.^.^.............................................................
.............................................................................................................................................
............................................................^.^.^...^.^...^.^.^.^............................................................
.............................................................................................................................................
...........................................................^.^.^...^.^...^.^.^.^.^...........................................................
.............................................................................................................................................
..........................................................^...^.....^.^.^.^.^.^...^..........................................................
.............................................................................................................................................
.........................................................^.^.......^.^...^.^.^...^.^.........................................................
.............................................................................................................................................
........................................................^.^.^.^.^...^.....^.^.....^.^........................................................
.............................................................................................................................................
.......................................................^.^.^.^.^.^...^.^.....^.^...^.^.......................................................
.............................................................................................................................................
......................................................^.^.^...^...^...^...^.^.^.^.^.^.^......................................................
.............................................................................................................................................
.....................................................^.^...^...^...^.^.^.^.^.^...^.^...^.....................................................
.............................................................................................................................................
....................................................^.^.^.....^.^.........^...^.^.^.....^....................................................
.............................................................................................................................................
...................................................^...^.^.^.....^.^.^...^...^.....^.^.^.^...................................................
.............................................................................................................................................
..................................................^.^.^.^.^.^.^...^.^.^...^...^.......^...^..................................................
.............................................................................................................................................
.................................................^...^.....^.^.^.^.....^.^...^.....^.^.^...^.................................................
.............................................................................................................................................
................................................^...^.....^.^.^.^.^.^.^.^.^.^.............^.^................................................
.............................................................................................................................................
...............................................^.^.^.....^.^.....^.^.^.^.^.....^.....^...^.^.^...............................................
.............................................................................................................................................
..............................................^...^.^.^.^.^.^.^.^.^.^.^.^.^.....^...^.^.^.^...^..............................................
.............................................................................................................................................
.............................................^.^.^...^.^.^...^...^.^.^.....^...^.....^.^.^...^.^.............................................
.............................................................................................................................................
............................................^.^.......^.^.^.^.....^.......^.^.^...^.^.........^.^............................................
.............................................................................................................................................
...........................................^...^...^...^...^.^.........^.^...^.^.....^.....^.^.^.^...........................................
.............................................................................................................................................
..........................................^...^.^...^.^.^.^.^...^.^.^.^.^.^.....^...^...^.^.^.^.^.^..........................................
.............................................................................................................................................
.........................................^.^...^.^...^.^.....^.^.......^.^.....^.^.^.^.^.^...^.^.^.^.........................................
.............................................................................................................................................
........................................^.^...^.^...^.^.......^.......^.^.^.^.^.^.^.......^.^.^...^.^........................................
.............................................................................................................................................
.......................................^.^.^...^.^...^.^...^.^...^.....^.^.^...^.......^...^.^.^.^.^.^.......................................
.............................................................................................................................................
......................................^.....^.^.....^.....^...^...^.^.^...^.^.^.^.....^.^.^...^.^.^...^......................................
.............................................................................................................................................
.....................................^.^.....^.^.^...^.....^.^.^.^...^.^.......^.^...^.^.^...^.^.^.^.^.^.....................................
.............................................................................................................................................
....................................^.^.^.^...^.......^.^...^...^.....^...^...^.^...^.^.^.......^.^.^...^....................................
.............................................................................................................................................
...................................^.^.....^.....^.^.^.^.^.^...^.^.^.^.^.^.....^.^...^.^...^.^.^...^.....^...................................
.............................................................................................................................................
..................................^...^...^.^.^...^.^.^.....^.^.^.....^.^.^.^.^.^.^.^.......^.^...^.^.^.^.^..................................
.............................................................................................................................................
.................................^.^...^.^.^.^.^...^.^...^.^.^.^.......^.^.^.^.^...^...^.^.^.^.^.^.^.......^.................................
.............................................................................................................................................
................................^...^.^.^...^.^.^.^.^...^.^.^...^...^.^...^.^.......^.^.^.^.^.....^.^.^.^.^.^................................
.............................................................................................................................................
...............................^.^.^.^.^...^.^.^.....^.^...^...^.^...^.^.^.^.^...^.^.^.^.^...^.^.^.^.^.^.^.^.^...............................
.............................................................................................................................................
..............................^.^.^.....^...^.^.^.^...^...^.^.^.^.^.^.^...^.^.^.......^...^.^.^.^.^.^...^.^.^.^..............................
.............................................................................................................................................
.............................^.....^.^...^.^.^.^...^.^.^.^...^...^...^.^.^.^.....^.^.......^...^.^...^.^.^.^...^.............................
.............................................................................................................................................
............................^.^.^...^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^...^.^.^.^.^.^.......^.^.^...^.^...^.....^............................
.............................................................................................................................................
...........................^.^...^...^...^.^.^.^...^.....^...^.^.^.^.^.^.^...^.......^.^.^.^.^...^...^.......^.^.^...........................
.............................................................................................................................................
..........................^.....^...^.^.^.....^.^.^...^...^...^.....^...^...^.^.^.^.^.^.^.^.^.....^...^.^.^.......^..........................
.............................................................................................................................................
.........................^.^.^...^.^.....^.^...^.^.^...^.....^.^.^.^.....^...^...^.^.^...^.^...^.....^.^.^.^.....^.^.........................
.............................................................................................................................................
........................^...^.^.^.^.^.......^.^.^.......^.^.^...^.^.....^.^.^.^.^...^.^.^.......^.^.^.........^.^.^.^........................
.............................................................................................................................................
.......................^.^.^.^.^.....^.^...^.^.^...^.^.^.^...^.^...^...^...^.....^.^.^.^...^.^.^.^...^.......^.^.^.^.^.......................
.............................................................................................................................................
......................^.^.^.^.^.^.....^.^.......^.^.^...^.^.^.^.^.^...^.^.^.^...^.^...^...^.......^.^.^.^...^.^.^.^...^......................
.............................................................................................................................................
.....................^.^.^.^...^.^.^.^.^.^.^...^...^.^.^.....^.^.^...^.^.^.....^.^.^.....^...^.^.^.^.^.^.^.^.^.^...^...^.....................
.............................................................................................................................................
....................^.^...^.^.^.^...^...^.........^.^...^...^...^...^.^...^.^.^...^...^...^...^.^.^.^.^.^.^...^.^.^.....^....................
.............................................................................................................................................
...................^.^.^...^...^...^.^.^...^.^.^...^.....^.^.^.^.....^.^...^...^.^...^...^.^.^.^.^.^...^.^.^.^...^.....^.^...................
.............................................................................................................................................
..................^.^.......^.^.^...^.^.^...^.^.....^.^.^.^.^.^.^.^.....^...^.^.^.........^.............^.......^...^.^.^.^..................
.............................................................................................................................................
.................^.^.........^...^.^.......^.^.^.^.^.^.^.^.^...^.....^.^.^.^.........^.^.^.^.^.....^...^...^.^.^.^.^...^...^.................
.............................................................................................................................................
................^.^.^...^.^.^.....^.^.^...^.^.^.^...^...^.^...^.^.^.^.^.^...^.^...^.^.^.....^...^.^...^...^.^...^.^.^.....^.^................
.............................................................................................................................................
...............^.^.^.^.^.^.^.^.....^...^.^.^.^.^...^.........^.^.^.^...^.^.^.....^...^...^.^.^.^.^.^.^.^.^.^...^...^.^.^.^.^.^...............
.............................................................................................................................................
..............^.^.^...^.^.^.^...^.^...^.^.^.^.^.^.^.^.^.^...^.^...^.....^.^...^.............^.^...^.^.^.^.^...^.^.^.^.....^.^.^..............
.............................................................................................................................................
.............^.^.^.^.....^...^...^.....^.^...^...^.......^...^...^.^...^...^.^.^.^.^.^.^.^.^.....^.^.^.^.^.^.^...^...^.^.^.....^.............
.............................................................................................................................................
............^...^.^.^.^.^...^...^.^.^.....^.^.^.^...^.^...^...^...^...^.^.......^.^.^.....^...^...^.^.^.^.^.^.......^.^.^...^.^.^............
.............................................................................................................................................
...........^.^.^.^.^.^.^.^...^.^...^.^.^.^.^.......^.^.^...^.^.^.^.^.^.^.^...^.^...^.........^.^.^.^.....^...^.^...^.^.^.^.....^.^...........
.............................................................................................................................................
..........^.....^.^.^.....^.....^.....^.^.^.^.^.^.^.^.^.....^.^.^.^.^...^.^.^...^.......^.^...^...^...^.....^...^.^.^.^.^.^.^.^.^.^..........
.............................................................................................................................................
.........^...^...^.^.^.....^.^.^.^.^.^...^...^...^...^.......^.^.^.^.^.^.^...^.^.^.^.^.^.........^...^.^.^...^.^.....^.^.^.^.^.....^.........
.............................................................................................................................................
........^.^.....^.^.^.^...^.^.^.^...^.^.^.^.^.^...^.^.^...^.^.^.^...^...^...^.........^.^.^.^.^.....^.^.^...^.^.^.^.^...^...^.^.^...^........
.............................................................................................................................................
.......^.^.^.^.^.^.^.......^.^.........^.^.^.^.^.^.^.^.^.^.^...^...^.^...^...^...^...^...^.^.....^...^...^...^.^.^.....^...^...^.....^.......
.............................................................................................................................................
......^.^.^...^.^.^.^.^.^.^.......^.^.^.^.^.^.^.^.....^.....^.....^.^.^.^.^.^.^.^...^.^.^.^.^.^...^.^.^.......^.^.....^.^.^.^.....^.^.^......
.............................................................................................................................................
.....^...^.^.^.^.....^...^...^.....^.^.^.^.^.^.^.....^.^.^...^...^.^.^.^.^.^...^...^.^.^.^.....^.^.^.^...^.....^.^...^...^.^...^.^.^.^.^.....
.............................................................................................................................................
....^.^.^...^.^.^.^.^.^...^...^.^.^.^.^...^.^.^.^...^...^.^...^.^.^...^.^.^.^.^.^.....^.^.^.^.^.^.....^...^.^.^.^.^...^.......^...^.....^....
.............................................................................................................................................
...^.^.^.....^.^.^.^.^.....^...^.....^...^.^.^.^.^...^...^.^.^.^.^.^.^...^.^.^...^.^.^.....^.....^...^.^.^.^.^...^.^...^...^.^...^.^.^.^.^...
.............................................................................................................................................
..^.^.......^.^.^.^...^...^.^.....^.^.^.^.^.^...^.^.....^...^.^.....^.^...^.^.^...^...^.^.^.^.....^.^.^...^...^...^.^.^.^...^.^.^...^.^...^..
.............................................................................................................................................
.^.^...^.^.^...^...^...^.^.....^.^.....^.^.^.^...^.....^.^.^.^...^.^.^.^.^.^...^.^.^.....^.^.^.^.^.^.^...^.^.^.^.....^.^.^.^.^.^.^.^.....^.^.
.............................................................................................................................................

1000
files/Problem8.txt Normal file

File diff suppressed because it is too large Load Diff