Skip to content

Instantly share code, notes, and snippets.

@pawlos
Created December 22, 2017 15:08
Show Gist options
  • Save pawlos/2c2bb4d29cfaceef30ad93379aece410 to your computer and use it in GitHub Desktop.
Save pawlos/2c2bb4d29cfaceef30ad93379aece410 to your computer and use it in GitHub Desktop.
Solution to Day 22: Sporifica Virus
#adc_d22.py
inp = [
"..#",
"#..",
"..."]
#
inp = open("input_d22.txt").readlines()
for i in range(len(inp)):
inp[i] = inp[i].replace('\n','')
#print inp
start_w = 0
for i in inp:
for j in i:
if j == '#':
start_w += 1
#start_w = sum(x == '#' for x in inp)
print start_w
l = 501
d = ['.'*l]*l
#print 'd: ',d
p = (len(d)-len(inp))/2
#print 'p: ',p
for i in range(p,p+len(inp)):
#print i, d[i][0:p]
d[i] = d[i][0:p]+inp[i-p]+d[i][p:]
for i in range(len(d)):
d[i] = [c for c in d[i]]
#print 'd after: ',d
x = len(d)/2
y = len(d)/2
dir_x = 0
dir_y = -1
#inp = d
infections = 0
print x,y, d[y][x]
for i in range(10000000):
if x >= len(d) or x < 0:
print 'error'
break
if y >= len(d) or y < 0:
print 'error'
break
#print x,y
#print dir_x,dir_y
if d[y][x] == '.':
d[y][x] = 'W'
#print 'turn left'
#turn left
if dir_x == 0 and dir_y == -1:
dir_x = -1
dir_y = 0
elif dir_x == -1 and dir_y == 0:
dir_x = 0
dir_y = 1
elif dir_x == 0 and dir_y == 1:
dir_x = 1
dir_y = 0
else:
dir_x = 0
dir_y = -1
elif d[y][x] == '#':
d[y][x] = 'F'
#print 'turn right'
# turn right
if dir_x == 0 and dir_y == -1:
dir_x = 1
dir_y = 0
elif dir_x == -1 and dir_y == 0:
dir_x = 0
dir_y = -1
elif dir_x == 0 and dir_y == 1:
dir_x = -1
dir_y = 0
else:
dir_x = 0
dir_y = 1
elif d[y][x] == 'W':
#print 'clean'
infections += 1
d[y][x] = '#'
elif d[y][x] == 'F':
#print 'infected'
d[y][x] = '.'
dir_x *= -1
dir_y *= -1
else:
print ord(d[y][x])
print 'sth not right'
break
#print dir_x,dir_y
y += dir_y
x += dir_x
#print inp
#print '---'
#print inp
#p,p+len(inp)
org_infected = 0
for i in range(len(d)):
for j in range(len(d)):
if i >= p and i < p+len(inp) and j>=p and j < p+len(inp):
#print inp[i-p][j-p], d[i][j]
if inp[i-p][j-p] == '#' and d[i][j] == '#':
#print 'i-p:',i-p,' j-p: ',j-p, "i: ",i, "j: ",j
org_infected += 1
#print i, d[i][0:p]
print dir_x, dir_y
print infections, org_infected, start_w, infections - org_infected
#print d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment