Added 2017/13 part 1

This commit is contained in:
FrederikBaerentsen 2024-11-25 12:27:11 +01:00
parent 47d321a993
commit 31fec7fe38
4 changed files with 285 additions and 5 deletions

View File

@ -191,6 +191,136 @@ the example above, the trip severity is `0*3 + 6*4 = 24`.
Given the details of the firewall you\'ve recorded, if you leave Given the details of the firewall you\'ve recorded, if you leave
immediately, *what is the severity of your whole trip*? immediately, *what is the severity of your whole trip*?
To begin, [get your puzzle input](13/input). Your puzzle answer was `1844`.
The first half of this puzzle is complete! It provides one gold star: \*
## \-\-- Part Two \-\-- {#part2}
Now, you need to pass through the firewall without being caught - easier
said than done.
You can\'t control the [speed of the
packet]{title="Seriously, what network stack doesn't let you adjust the speed of light?"},
but you can *delay* it any number of picoseconds. For each picosecond
you delay the packet before beginning your trip, all security scanners
move one step. You\'re not in the firewall during this time; you don\'t
enter layer `0` until you stop delaying the packet.
In the example above, if you delay `10` picoseconds (picoseconds `0` -
`9`), you won\'t get caught:
State after delaying:
0 1 2 3 4 5 6
[ ] [S] ... ... [ ] ... [ ]
[ ] [ ] [ ] [ ]
[S] [S] [S]
[ ] [ ]
Picosecond 10:
0 1 2 3 4 5 6
( ) [S] ... ... [ ] ... [ ]
[ ] [ ] [ ] [ ]
[S] [S] [S]
[ ] [ ]
0 1 2 3 4 5 6
( ) [ ] ... ... [ ] ... [ ]
[S] [S] [S] [S]
[ ] [ ] [ ]
[ ] [ ]
Picosecond 11:
0 1 2 3 4 5 6
[ ] ( ) ... ... [ ] ... [ ]
[S] [S] [S] [S]
[ ] [ ] [ ]
[ ] [ ]
0 1 2 3 4 5 6
[S] (S) ... ... [S] ... [S]
[ ] [ ] [ ] [ ]
[ ] [ ] [ ]
[ ] [ ]
Picosecond 12:
0 1 2 3 4 5 6
[S] [S] (.) ... [S] ... [S]
[ ] [ ] [ ] [ ]
[ ] [ ] [ ]
[ ] [ ]
0 1 2 3 4 5 6
[ ] [ ] (.) ... [ ] ... [ ]
[S] [S] [S] [S]
[ ] [ ] [ ]
[ ] [ ]
Picosecond 13:
0 1 2 3 4 5 6
[ ] [ ] ... (.) [ ] ... [ ]
[S] [S] [S] [S]
[ ] [ ] [ ]
[ ] [ ]
0 1 2 3 4 5 6
[ ] [S] ... (.) [ ] ... [ ]
[ ] [ ] [ ] [ ]
[S] [S] [S]
[ ] [ ]
Picosecond 14:
0 1 2 3 4 5 6
[ ] [S] ... ... ( ) ... [ ]
[ ] [ ] [ ] [ ]
[S] [S] [S]
[ ] [ ]
0 1 2 3 4 5 6
[ ] [ ] ... ... ( ) ... [ ]
[S] [S] [ ] [ ]
[ ] [ ] [ ]
[S] [S]
Picosecond 15:
0 1 2 3 4 5 6
[ ] [ ] ... ... [ ] (.) [ ]
[S] [S] [ ] [ ]
[ ] [ ] [ ]
[S] [S]
0 1 2 3 4 5 6
[S] [S] ... ... [ ] (.) [ ]
[ ] [ ] [ ] [ ]
[ ] [S] [S]
[ ] [ ]
Picosecond 16:
0 1 2 3 4 5 6
[S] [S] ... ... [ ] ... ( )
[ ] [ ] [ ] [ ]
[ ] [S] [S]
[ ] [ ]
0 1 2 3 4 5 6
[ ] [ ] ... ... [ ] ... ( )
[S] [S] [S] [S]
[ ] [ ] [ ]
[ ] [ ]
Because all smaller delays would get you caught, the fewest number of
picoseconds you would need to delay to get through safely is `10`.
*What is the fewest number of picoseconds* that you need to delay the
packet to pass through the firewall without being caught?
Answer: Answer:
Although it hasn\'t changed, you can still [get your puzzle
input](13/input).

View File

@ -2,22 +2,99 @@
import sys,re import sys,re
from pprint import pprint from pprint import pprint
sys.path.insert(0, '../../') sys.path.insert(0, '../../')
from fred import list2int from fred import list2int, ppprint
input_f = 'test' input_f = 'input'
part = 2
part = 1
######################################### #########################################
# # # #
# Part 1 # # Part 1 #
# # # #
######################################### #########################################
grid = []
if part == 1: if part == 1:
for i in range(0,100):
grid.append([])
with open(input_f) as file: with open(input_f) as file:
for line in file: for line in file:
l = line.rstrip().split(': ')
l = list2int(l)
for i in range(0,l[1]):
grid[l[0]].append('[ ]')
count = 0
hit = []
direction = 1
packet_loc = 0
scanner_loc = [0 if row else ' ' for row in grid]
print(scanner_loc)
directions = [1 if row else 0 for row in grid]
hit = []
for x in range(0,100):
#input()
for idx,i in enumerate(grid):
if scanner_loc[idx] == 0 and packet_loc == idx:
hit.append(idx)
if len(grid[idx]) != 0:
loc = scanner_loc[idx] % len(grid[idx])
scanner_loc[idx] += directions[idx]
if scanner_loc[idx] == len(grid[idx])-1:
directions[idx] = -1
elif scanner_loc[idx] == 0:
directions[idx] = 1
packet_loc += 1
print(hit)
damage = 0
for i in hit:
damage += (i*len(grid[i]))
print(damage)
# for x in range(0,7):
# for idx,i in enumerate(grid):
# for jdx,j in enumerate(i):
# new_idx = jdx + directions[idx]
# #print(new_idx,directions[idx])
# if jdx == len(i)-1:
# print(jdx,len(i))
# if new_idx >= len(i)-1:
# directions[idx] = -1
# new_idx = len(i) - 2
# elif new_idx < 0:
# directions[idx] = 1
# new_idx = 1
# print(':',jdx,new_idx)
# if jdx == new_idx:
# if place == idx and jdx == 0:
# grid[idx][jdx] = '(S)'
# else:
# grid[idx][jdx] = '[S]'
# #print('[S]',end='')
# else:
# if place == idx and jdx == 0:
# grid[idx][jdx] = '( )'
# else:
# grid[idx][jdx] = '[ ]'
# place += 1
# pprint(grid[0])
# #pprint(grid)
# input()
# count += 1
######################################### #########################################
@ -26,4 +103,71 @@ if part == 1:
# # # #
######################################### #########################################
if part == 2: if part == 2:
exit() grid_length = 100
for i in range(0,grid_length):
grid.append([])
with open(input_f) as file:
for line in file:
l = line.rstrip().split(': ')
l = list2int(l)
for i in range(0,l[1]):
grid[l[0]].append('[ ]')
count = 0
hit = []
direction = 1
packet_loc = 0
emp_grid = [0 if row else ' ' for row in grid]
scanner_loc = list(emp_grid)
#print(scanner_loc)
emp_dir = [1 if row else 0 for row in grid]
directions = list(emp_dir)
hit = []
End = False
start_loc = 0
got_hit = False
while not End:
if len(hit) > 0:
start_loc -= 1
print('Moving back',start_loc)
packet_loc = 0
hit = []
scanner_loc = list(emp_grid)
directions = list(emp_dir)
got_hit = False
packet_loc += start_loc
while packet_loc < grid_length and not got_hit:
#print(packet_loc,scanner_loc)
for idx,i in enumerate(grid):
if scanner_loc[idx] == 0 and packet_loc == idx:
hit.append(idx)
#print('Got hit at',idx)
got_hit = True
elif len(grid[idx]) != 0:
loc = scanner_loc[idx] % len(grid[idx])
scanner_loc[idx] += directions[idx]
if scanner_loc[idx] == len(grid[idx])-1:
directions[idx] = -1
elif scanner_loc[idx] == 0:
directions[idx] = 1
if got_hit:
break
packet_loc += 1
if len(hit) == 0:
print(abs(start_loc))
break
#input()
#print(hit)
#damage = 0
#for i in hit:
# damage += (i*len(grid[i]))
#print(damage)

Binary file not shown.

View File

@ -1,2 +1,8 @@
def list2int(x): def list2int(x):
return list(map(int, x)) return list(map(int, x))
def ppprint(x):
for idx,i in enumerate(x):
for jdx,j in enumerate(i):
print(x[idx][jdx],end='')
print()