Solved 2024/14 P2

This commit is contained in:
2024-12-14 13:07:16 +01:00
parent 3fca76a62c
commit a7eafca0a2
4 changed files with 131 additions and 13 deletions
+8 -4
View File
@@ -156,8 +156,6 @@ after exactly 100 seconds have elapsed?*
Your puzzle answer was `230686500`.
The first half of this puzzle is complete! It provides one gold star: \*
## \-\-- Part Two \-\-- {#part2}
During the bathroom break, someone notices that these robots seem
@@ -170,8 +168,14 @@ picture of a Christmas tree*.
*What is the fewest number of seconds that must elapse for the robots to
display the Easter egg?*
Answer:
Your puzzle answer was `7672`.
Although it hasn\'t changed, you can still [get your puzzle
Both parts of this puzzle are complete! They provide two gold stars:
\*\*
At this point, you should [return to your Advent calendar](/2024) and
try another puzzle.
If you still want to see it, you can [get your puzzle
input](14/input).
+62 -8
View File
@@ -1,13 +1,15 @@
#!/bin/python3
import sys,time,re
from pprint import pprint
from termcolor import colored
sys.path.insert(0, '../../')
from fred import list2int,get_re,lprint,loadFile,addTuples,grid_valid
from fred import list2int,get_re,lprint,loadFile,addTuples,grid_valid,bfs,dfs,flood_fill
start_time = time.time()
# input_f = 'test'
# size_r = 7
# size_c = 11
input_f = 'test'
size_r = 7
size_c = 11
input_f = 'input'
size_r = 103
@@ -15,13 +17,18 @@ size_c = 101
grid = [['.']*size_c]*size_r
def nprint(grid,pos,x):
def nprint(grid,pos=None,x=None,positions:list=None):
for r in range(size_r):
for c in range(size_c):
if (c,r) == pos:
if (c,r) == pos and positions is None:
print(x,end='')
elif positions is not None:
if (c,r) in positions:
print(x,end='')
else:
print(colored(grid[r][c],'red'),end='')
else:
print(grid[r][c],end='')
print(colored(grid[r][c],'red'),end='')
print()
#########################################
@@ -129,7 +136,54 @@ print('Part 1:',part1(), '\t\t', round((time.time() - start_time)*1000), 'ms')
# #
#########################################
def part2():
return
instructions = loadFile(input_f)
cords = []
for idx,inst in enumerate(instructions):
match = get_re(r"^p=(-?\d+),(-?\d+) v=(-?\d+),(-?\d+)",inst)
instructions[idx] = [(int(match.group(1)),int(match.group(2))),(int(match.group(3)),int(match.group(4)))]
cords.append(instructions[idx][0])
count = 0
def generate():
coordinates = {}
for idx,inst in enumerate(instructions):
pos = inst[0]
vel = inst[1]
pos = addTuples(pos,vel)
if pos[0] < 0:
pos = (pos[0]+size_c,pos[1])
if pos[0] >= size_c:
pos = (pos[0]-size_c,pos[1])
if pos[1] < 0:
pos = (pos[0],pos[1]+size_r)
if pos[1] >= size_r:
pos = (pos[0],pos[1]-size_r)
instructions[idx] = [pos,vel]
cords[idx] = pos
if pos not in coordinates:
coordinates[pos] = 0
coordinates[pos] += 1
return coordinates
while True:
coordinates = generate()
count += 1
visited = []
if 2 not in coordinates.values():
for pos in coordinates.keys():
if pos not in visited:
t_visited = flood_fill(coordinates.keys(),pos)
visited += t_visited
if len(t_visited) > 10:
nprint(grid,x='*',positions=coordinates)
return count
start_time = time.time()
print('Part 2:',part2(), '\t\t', round((time.time() - start_time)*1000), 'ms')