add day 12 and the new part 2 for day 11 (which was so hard).

This commit is contained in:
Peter 2020-12-12 22:38:21 +08:00
parent 3e0c92c972
commit 3d128f86ec
8 changed files with 9389 additions and 0 deletions

9087
11/LOG.TXT Normal file

File diff suppressed because it is too large Load Diff

150
11/a.py Normal file
View File

@ -0,0 +1,150 @@
from itertools import chain
from copy import deepcopy
mat = [ list(x) for x in open('input').read().replace('L','#').splitlines() ]
def adj(adjrows, i):
return list(chain(*[[ x[i] for i in range(i-1,i+2) ] for x in adjrows ]))
def simulate(mat): # Part A.
idom = range(len(mat[0]))
jdom = range(len(mat))
adjrows = [None, [ '.' for x in idom ]+['.'], mat[0]+['.']]
mat_ = deepcopy(mat)
d = 0
for j in jdom:
adjrows.pop(0)
adjrows.append(mat[j+1]+['.']) if j+1 in jdom else adjrows.append([ '.' for x in idom ]+['.'])
# print(adjrows)
for i in idom:
cell = mat[j][i]
# print(i,j, cell)
if cell == "#":
# print(adj(adjrows))
# print( [[ x[i] for i in range(i-1,i+2) ] for x in adjrows ] )
adjm = adj(adjrows, i).count('#')-1
if adjm > 3:
mat_[j][i] = 'L'
d += 1
elif cell == "L":
adjm = adj(adjrows, i).count('#')
if adjm == 0:
mat_[j][i] = '#'
d += 1
# [ print(x) for x in mat_ ]
if d > 0:
mat = simulate(mat_)
return mat
return mat
def a(mat): #Holy [] this is slow!
# [ print(''.join(x)) for x in simulate(mat) ]
count = []
[ count.append(x.count('#')) for x in simulate(mat) ]
return sum(count)
# SEE b.py FOR PART 2 ANSWER
# SEE b.py FOR PART 2 ANSWER
# SEE b.py FOR PART 2 ANSWER
# NOT USING THIS AS MY ANSWER.
# # print(a(mat))
# def diagrtx(mat, r, nc, pc, jdom):
# [c, pb, nb] = [0, False, False]
# for x in r:
# y = -x + nc
# if nb == False and -y in jdom:
# cell = mat[-y][x]
# if cell == '#':
# c += 1
# nb = True
# elif cell == "L":
# nb = True
# y = x + pc
# if pb == False and -y in jdom:
# cell = mat[-y][x]
# if cell == '#':
# c += 1
# pb = True
# elif cell == "L":
# pb = True
# return c
# def hrtx(mat, r, j, idom):
# for x in r:
# if x in idom:
# cell = mat[j][x]
# if cell == "#":
# return 1
# break
# elif cell == "L":
# break
# return 0
# def vrtx(mat, r, i, jdom):
# for y in r:
# if -y in jdom:
# cell = mat[-y][i]
# if cell == "#":
# return 1
# break
# elif cell == "L":
# break
# return 0
# def RTX(mat, idom, jdom, i, j): #generate diagonals - this is quality '3am' code right here
# nc = i + j
# pc = j - i
# # print(nc, pc)
# # print(list(range(i,len(mat[0]))))
# # [ print(x) for x in mat ]
# rl = range(i-1, -1, -1)
# lr = range(i+1, len(mat[0]))
# u = range(j+1, 1)
# d = range(j-1, -len(mat), -1)
# return (
# diagrtx(mat, rl, nc, pc, jdom) + hrtx(mat, rl, -j, idom)
# + diagrtx(mat, lr, nc, pc, jdom) + hrtx(mat, lr, -j, idom)
# + vrtx(mat, u, i, idom) + vrtx(mat, d, i, idom)
# )
# #wtf part over.
# def simulate2(mat):
# idom = range(len(mat[0]))
# jdom = range(len(mat))
# mat_ = deepcopy(mat)
# d = 0
# for j in jdom:
# for i in idom:
# c = RTX(mat, idom, jdom, i, j)
# cell = mat[j][i]
# # print(i,j, c, cell)
# if cell == "#":
# if c > 4:
# mat_[j][i] = 'L'
# d += 1
# elif cell == "L":
# if c == 0:
# mat_[j][i] = '#'
# d += 1
# [ print(''.join(x)) for x in mat_ ]
# print('')
# if d > 0:
# mat = simulate2(mat_)
# return mat
# return mat
# #increment x
# # print([-x + nc if -(-x + nc) in jdom else None, x + pc if -(x + pc) in jdom else None])
# # print({x:[-x + nc if -(-x + nc) in jdom else None, x + pc if -(x + pc) in jdom else None] for x in idom})
# def b(mat): #Holy shit this is even slower!
# count = []
# [ count.append(x.count('#')) for x in simulate2(mat) ]
# return sum(count)
# print(b(mat))
# # def rtx(mat, i, j): #raytracer

100
11/b.py Normal file
View File

@ -0,0 +1,100 @@
from copy import deepcopy
mat = [ ['.']+list(x)+['.'] for x in open('input').read().replace('L','#').splitlines() ] #
null = [ '.' for x in range(len(mat[0])) ]
# cmat = [null] + [ ['.']+[ '0' for y in range(len(mat[0])-2) ]+['.'] for x in mat ] + [null]
mat = [null] + mat + [null]
def RTX_side(r, d, y, mat): # Yes i know this isn't proper raytracing but haha tech tip nvidia rtx 6900ti funny moment.
top = True
bottom = True
side = True
c = 0
j_d = 1
for i in r:
if top and y+j_d in d:
cell = mat[y+j_d][i]
if cell == 'L':
top = False
elif cell == '#':
top = False
c += 1
# mat[y+j_d][i] = "Y"
if y-j_d in d and bottom:
cell = mat[y-j_d][i]
if cell == 'L':
bottom = False
elif cell == '#':
bottom = False
c += 1
# mat[y-j_d][i] = "Y"
if side:
if mat[y][i] == "L":
side = False
if mat[y][i] == "#":
side = False
c += 1
# mat[y][i] = 'Y'
if c == 3: break
j_d += 1
return c
def RTX_top(r, i, mat):
for j in r:
if mat[j][i] == 'L':
return 0
elif mat[j][i] == '#':
return 1
# mat[j][i] = "Y"
return 0
# mat_ = list(map(list, zip(*mat)))
dim_j = len(mat)-1
# dim_j = len(mat_)-2
dim_i = len(mat[0])-1
domVERT = range(1,dim_j+1) #Domain for vertical / j values
# [ print(x) for x in mat ]
def RTX_ON(mat):
mat_ = deepcopy(mat)
d = False
for x in range(1, dim_i):
for y in range(1, dim_j):
# [ print(x) for x in mat ]
# mat[y][x] = 'X'
rN = range(y-1,0,-1)
rS = range(y+1,dim_j+1)
rE = range(x+1, dim_i+1)
rW = range(x-1, 0, -1)
# print(list(rN), list(rS))
# print(list(rE), list(rW))
c = (RTX_side(rE, domVERT, y, mat) + RTX_side(rW, domVERT, y, mat) + RTX_top(rN, x, mat) + RTX_top(rS, x, mat))
# cmat[y][x] = (str)(c)
if mat[y][x] == '#' and c > 4:
mat_[y][x] = 'L'
d = True
elif mat[y][x] == 'L' and c == 0:
mat_[y][x] = '#'
d = True
if d == True:
# print("")
# print("")
# [ print(x) for x in mat ]
# [ print(x) for x in cmat ]
# print("")
# [ print(x) for x in mat_ ]
# print("")
mat = RTX_ON(mat_)
return mat
return mat
simulated = RTX_ON(mat) #This is slow AF
[ print(''.join(x)) for x in simulated ] #output ascii
print(sum([ x.count('#') for x in simulated ]))

BIN
11/whiteboard.pdn Normal file

Binary file not shown.

BIN
11/whiteboard.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 305 KiB

52
12/a.py Normal file
View File

@ -0,0 +1,52 @@
# I love <3 complex numbers :3
import math
commands = [ [x[0],int(x[1:])] for x in open('input').read().splitlines()]
def pvec(d, m):
return {
'N' : complex(0,m),
'E' : complex(m,0),
'S' : complex(0,-m),
'W' : complex(-m,0)
}[d]
def a(commands):
fv = 0+0j
phase = 0
for v in commands:
d = v[0]
m = v[1]
if d == 'F':
fv += (m*(1j)**(phase/90))
elif d in ['L', 'R']:
phase += {
'L' : m,
'R' : -m
}[d]
else:
fv += pvec(d, m)
return (int)(abs(fv.real) + abs(fv.imag))
print(a(commands))
def b(commands):
fv = 0+0j
wv = 10+1j
for v in commands:
d = v[0]
m = v[1]
if d == 'F':
fv += m*wv
elif d in ['L', 'R']:
wv *= {
'L' : (1j)**(m/90),
'R' : (-1j)**(m/90)
}[d]
else:
wv += pvec(d, m)
return (int)(abs(fv.real) + abs(fv.imag))
print(b(commands))

BIN
12/whiteboard.pdn Normal file

Binary file not shown.

BIN
12/whiteboard.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB