AdventOfCode/2017/22/solution.py

207 lines
4.7 KiB
Python

#!/bin/python3
import sys,re
from pprint import pprint
sys.path.insert(0, '../../')
from fred import list2int, toGrid,nprint,get_value_in_direction, expand_grid, getCenter
input_f = 'input'
part = 2
#########################################
# #
# Part 1 #
# #
#########################################
def goLeft(dir:str):
if dir == 'north':
new_dir = (0,-1)
facing = 'west'
elif dir == 'south':
new_dir = (0,1)
facing = 'east'
elif dir == 'east':
new_dir = (-1,0)
facing = 'north'
elif dir == 'west':
new_dir = (1,0)
facing = 'south'
else:
print('Something is wrong')
print(dir)
print(new_dir)
return new_dir,facing
def goRight(dir:str):
if dir == 'north':
new_dir = (0,1)
facing = 'east'
elif dir == 'south':
new_dir = (0,-1)
facing = 'west'
elif dir == 'east':
new_dir = (1,0)
facing = 'south'
elif dir == 'west':
new_dir = (-1,0)
facing = 'north'
else:
print('Something is wrong')
print(dir)
print(new_dir)
return new_dir,facing
if part == 1:
grid = toGrid(input_f)
start = getCenter(grid)
#nprint(grid)
#print(start)
end = False
dir = (-1,0)
facing = 'north'
cur = start
infect = 0
iter = 1
while not end:
#print('-----------START-----------')
#print(cur[0]-1)
if cur[0]-1 <= 0 or cur[0]+1 >= len(grid[0]) or cur[1]+1 >= len(grid) or cur[1]-1 <= 0:
#print('Expanding')
grid = expand_grid(grid)
cur = tuple(map(lambda i, j: i + j, cur, (1,1)))
#print(cur)
if get_value_in_direction(grid,cur) == '#':
#print('infected')
dir,facing = goRight(facing)
grid[cur[0]][cur[1]] = '.'
#cur = (cur[0]+dir[0],cur[1]+dir[1])
elif get_value_in_direction(grid,cur) == '.':
#print('not infected')
dir,facing = goLeft(facing)
grid[cur[0]][cur[1]] = '#'
infect += 1
#cur = (cur[0]+dir[0],cur[1]+dir[1])
cur = (cur[0]+dir[0],cur[1]+dir[1])
#print(cur,dir,facing)
#nprint(grid,cur,'X')
#print('-----------END-----------',iter,infect)
iter += 1
if iter > 10000:
end = True
#input()
print(infect)
#########################################
# #
# Part 2 #
# #
#########################################
def goBack(dir:str):
if dir == 'north':
new_dir = (1,0)
facing = 'south'
elif dir == 'south':
new_dir = (-1,0)
facing = 'north'
elif dir == 'east':
new_dir = (0,-1)
facing = 'west'
elif dir == 'west':
new_dir = (0,1)
facing = 'east'
else:
print('Something is wrong')
print(dir)
print(new_dir)
return new_dir,facing
if part == 2:
grid = toGrid(input_f)
start = getCenter(grid)
#nprint(grid)
#print(start)
end = False
dir = (-1,0)
facing = 'north'
cur = start
infect = 0
iter = 1
nprint(grid)
while not end:
#print('-----------START-----------')
#print(cur[0]-1)
if cur[0]-1 <= 0 or cur[0]+1 >= len(grid[0]) or cur[1]+1 >= len(grid) or cur[1]-1 <= 0:
#print('Expanding')
grid = expand_grid(grid)
cur = tuple(map(lambda i, j: i + j, cur, (1,1)))
#print(cur)
if get_value_in_direction(grid,cur) == '#':
#print('infected')
dir,facing = goRight(facing)
grid[cur[0]][cur[1]] = 'F'
#cur = (cur[0]+dir[0],cur[1]+dir[1])
elif get_value_in_direction(grid,cur) == 'W':
dir,facing = dir,facing
grid[cur[0]][cur[1]] = '#'
infect += 1
elif get_value_in_direction(grid,cur) == 'F':
dir,facing = goBack(facing)
grid[cur[0]][cur[1]] = '.'
elif get_value_in_direction(grid,cur) == '.':
#print('not infected')
dir,facing = goLeft(facing)
grid[cur[0]][cur[1]] = 'W'
#cur = (cur[0]+dir[0],cur[1]+dir[1])
cur = (cur[0]+dir[0],cur[1]+dir[1])
#print(cur,dir,facing)
#nprint(grid,cur,'X')
#print('-----------END-----------',iter,infect)
iter += 1
if iter > 10000000:
end = True
elif iter % 100000 == 0:
print(iter,infect)
#input()
print(infect)