AdventOfCode/2017/13/solution.py

224 lines
5.8 KiB
Python
Raw Normal View History

2024-11-24 19:21:15 +01:00
#!/bin/python3
import sys,re
from pprint import pprint
sys.path.insert(0, '../../')
2024-11-25 12:27:11 +01:00
from fred import list2int, ppprint
2024-11-24 19:21:15 +01:00
2024-11-25 12:27:11 +01:00
input_f = 'input'
2024-11-25 17:42:14 +01:00
part = 2
2024-11-24 19:21:15 +01:00
#########################################
# #
# Part 1 #
# #
#########################################
2024-11-25 12:27:11 +01:00
grid = []
2024-11-24 19:21:15 +01:00
if part == 1:
2024-11-25 12:27:11 +01:00
for i in range(0,100):
grid.append([])
2024-11-24 19:21:15 +01:00
with open(input_f) as file:
for line in file:
2024-11-25 12:27:11 +01:00
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
2024-11-24 19:21:15 +01:00
2024-11-25 12:27:11 +01:00
for i in hit:
damage += (i*len(grid[i]))
print(damage)
2024-11-24 19:21:15 +01:00
2024-11-25 12:27:11 +01:00
# 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
2024-11-24 19:21:15 +01:00
#########################################
# #
# Part 2 #
# #
#########################################
2024-11-25 17:42:14 +01:00
if part == 3: #ugly not workng
2024-11-25 14:57:38 +01:00
grid_length = 7
2024-11-25 12:27:11 +01:00
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 = []
2024-11-25 14:57:38 +01:00
debug = 0
2024-11-25 12:27:11 +01:00
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):
2024-11-25 14:57:38 +01:00
debug += 1
2024-11-25 12:27:11 +01:00
if scanner_loc[idx] == 0 and packet_loc == idx:
2024-11-25 14:57:38 +01:00
hit.append(idx)
print('Got hit at',idx)
got_hit = True
2024-11-25 12:27:11 +01:00
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
2024-11-25 14:57:38 +01:00
#print(grid)
2024-11-25 12:27:11 +01:00
packet_loc += 1
2024-11-25 14:57:38 +01:00
2024-11-25 12:27:11 +01:00
if len(hit) == 0:
print(abs(start_loc))
2024-11-25 14:57:38 +01:00
print('Debug:',debug)
2024-11-25 12:27:11 +01:00
break
#input()
#print(hit)
#damage = 0
#for i in hit:
# damage += (i*len(grid[i]))
#print(damage)
2024-11-25 14:57:38 +01:00
2024-11-25 17:42:14 +01:00
if part == 2:
2024-11-25 14:57:38 +01:00
grid_length = 100
scanners = {}
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('[ ]')
for idx,i in enumerate(grid):
scanners[idx] = len(i)
debug = 0
damage = 0
delay = 0
End = False
while not End:
damage = False
for spot, range in enumerate(grid):
debug += 1
#print(spot%(2*(scanners[spot]-1)))
if not (delay+spot)%(2*(len(range)-1)) and len(range) > 0:
#print('hit at ', spot)
damage += 1
break
if damage:
delay += 1
else:
End = True
#print('-----')
#print()
#print('debug',debug)
#input()
print(debug)
print(delay)
#pprint(grid)
#print(scanners)