224 lines
5.7 KiB
Python
224 lines
5.7 KiB
Python
#!/bin/python3
|
|
import sys,re
|
|
from pprint import pprint
|
|
sys.path.insert(0, '../../')
|
|
from fred import list2int, ppprint
|
|
|
|
input_f = 'input'
|
|
|
|
part = 3
|
|
|
|
#########################################
|
|
# #
|
|
# Part 1 #
|
|
# #
|
|
#########################################
|
|
|
|
grid = []
|
|
|
|
if part == 1:
|
|
for i in range(0,100):
|
|
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
|
|
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
|
|
|
|
|
|
#########################################
|
|
# #
|
|
# Part 2 #
|
|
# #
|
|
#########################################
|
|
if part == 2:
|
|
grid_length = 7
|
|
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 = []
|
|
|
|
debug = 0
|
|
|
|
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):
|
|
debug += 1
|
|
|
|
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
|
|
#print(grid)
|
|
packet_loc += 1
|
|
|
|
if len(hit) == 0:
|
|
print(abs(start_loc))
|
|
print('Debug:',debug)
|
|
break
|
|
#input()
|
|
#print(hit)
|
|
#damage = 0
|
|
|
|
#for i in hit:
|
|
# damage += (i*len(grid[i]))
|
|
#print(damage)
|
|
|
|
|
|
|
|
if part == 3:
|
|
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)
|
|
|