Added 2025 day7 and day8

This commit is contained in:
2025-12-08 22:33:24 +01:00
parent b4dedd0c08
commit 911eaad03a
3 changed files with 251 additions and 2 deletions
+99
View File
@@ -0,0 +1,99 @@
#!/bin/python3
import sys,time,re,os,copy
from pprint import pprint
sys.path.insert(0, '../../')
from fred import *
start_time = time.time()
if sys.argv[1] == 'test':
input_f = 'test'
elif sys.argv[1] == 'input':
input_f = 'input'
else:
print('No argv provided')
exit()
#########################################
# #
# Part 1 #
# #
#########################################
def shoot_rays(grid:list, pos:set, rays:list, score:list):
rays.append(pos)
down = get_value_in_direction(grid, pos, 'down')
if down in rays:
return rays,score
if down == '.':
pos = addTuples(pos,(1,0))
return shoot_rays(grid,pos,rays,score)
elif down == '^':
if addTuples(pos,(1,0)) not in score:
score.append(addTuples(pos,(1,0)))
left = addTuples(pos,(1,-1))
right = addTuples(pos,(1,1))
if not grid_valid(left[0],left[1],grid):
return rays,score
if left not in rays:
rays.append(left)
r, s = shoot_rays(grid,left,rays,score)
if right not in rays:
rays.append(right)
return shoot_rays(grid,right,rays,score)
return rays,score
def part1():
score = []
rays = []
grid = toGrid(input_f)
start = findInGrid(grid,'S')
rays, score = shoot_rays(grid,start,rays,score)
if sys.argv[1] == 'test':
nprint(grid,sign='|',positions=rays)
return len(set(score))
return score
start_time = time.time()
p1 = part1()
print('Part 1:',p1, '\t\t', round((time.time() - start_time)*1000), 'ms')
#########################################
# #
# Part 2 #
# #
#########################################
def part2():
score = []
rays = []
grid = toGrid(input_f)
start = findInGrid(grid,'S')
count = []
for i in range(0,len(grid[0])):
count.append(0)
count[start[1]] = 1
for x, cols in enumerate(grid):
for y, rows in enumerate(cols):
if grid[x][y] == '^':
count[y+1] += count[y]
count[y-1] += count[y]
count[y] = 0
print(count)
return sum(count)
start_time = time.time()
p2 = part2()
print('Part 2:',p2, '', round((time.time() - start_time)*1000), 'ms')
+148
View File
@@ -0,0 +1,148 @@
#!/bin/python3
import sys,time,re,os,copy
from pprint import pprint
sys.path.insert(0, '../../')
from fred import *
import heapq,math
start_time = time.time()
if sys.argv[1] == 'test':
input_f = 'test'
elif sys.argv[1] == 'input':
input_f = 'input'
else:
print('No argv provided')
exit()
#########################################
# #
# Part 1 #
# #
#########################################
def part1():
score = 1
lines = loadFile(input_f,',')
heap = []
for i in range(len(lines)):
for j in range(i + 1, len(lines)):
cords1 = list2int(lines[i])
cords2 = list2int(lines[j])
dist = math.dist(cords1, cords2)
heapq.heappush(heap, (dist, tuple(cords1), tuple(cords2)))
if sys.argv[1] == 'test':
limit = 10
else:
limit = 1000
junctions = []
while limit > 0:
box = heapq.heappop(heap)
left = tuple(box[1])
right = tuple(box[2])
left_junction = None
right_junction = None
for j in junctions:
if left in j:
left_junction = j
if right in j:
right_junction = j
if left_junction != right_junction and (left_junction != None and right_junction != None):
left_junction += right_junction
junctions.remove(right_junction) #forgot to remove the right junction
if left_junction is not None and right_junction is None:
left_junction.append(right)
if left_junction is None and right_junction is not None:
right_junction.append(left)
if left_junction is None and right_junction is None:
junctions.append([left,right])
limit -= 1
out = sorted(junctions, key=lambda item: len(item), reverse=True)
for i in range(0,3):
score *= len(out[i])
return score
start_time = time.time()
p1 = part1()
print('Part 1:',p1, '\t\t', round((time.time() - start_time)*1000), 'ms')
#########################################
# #
# Part 2 #
# #
#########################################
def part2():
score = 1
lines = loadFile(input_f,',')
heap = []
for i in range(len(lines)):
for j in range(i + 1, len(lines)):
cords1 = list2int(lines[i])
cords2 = list2int(lines[j])
dist = math.dist(cords1, cords2)
heapq.heappush(heap, (dist, tuple(cords1), tuple(cords2)))
if sys.argv[1] == 'test':
limit = 10
else:
limit = 1000
junctions = []
while limit > 0:
box = heapq.heappop(heap)
left = tuple(box[1])
right = tuple(box[2])
left_junction = None
right_junction = None
for j in junctions:
if left in j:
left_junction = j
if right in j:
right_junction = j
if left_junction != right_junction and (left_junction != None and right_junction != None):
left_junction += right_junction
junctions.remove(right_junction) #forgot to remove the right junction
if left_junction is not None and right_junction is None:
left_junction.append(right)
if left_junction is None and right_junction is not None:
right_junction.append(left)
if left_junction is None and right_junction is None:
junctions.append([left,right])
if len(junctions[0]) == len(lines):
break
return left[0]*right[0]
start_time = time.time()
p2 = part2()
print('Part 2:',p2, '', round((time.time() - start_time)*1000), 'ms')
+4 -2
View File
@@ -16,8 +16,10 @@
|_\_|^|_]_|==|_T_T_T_T_T_...' 5 ** |_\_|^|_]_|==|_T_T_T_T_T_...' 5 **
|| . ____________ || . ____________
_||__/ \_ 6 ** _||__/ \_ 6 **
|^@ | 1 2 3 () | | '..'/ \'..' '..' |____
|&%;]__[]_[]_[]__<>| | |H_/|\ \\\\\\ | | | 7 **
'...|<>__|H__|_\__|_____|_[_O_|
| 8 **
## 2024 ## 2024
.-----. .------------------. .-----. .------------------.