From 5bcca706c8ea08b32f12f3b67e7d9224658b2d83 Mon Sep 17 00:00:00 2001 From: npc-strider Date: Thu, 24 Dec 2020 22:38:26 +0800 Subject: [PATCH] attempts for day 23 (unfinish) and 1.5 solutions for day 24 --- 23/a.py | 65 +++++++++++++++++++++++++++++++++++++++++++++ 23/b.py | 64 +++++++++++++++++++++++++++++++++++++++++++++ 24/a.py | 41 +++++++++++++++++++++++++++++ 24/b.py | 22 ++++++++++++++++ 24/c.py | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 273 insertions(+) create mode 100644 23/a.py create mode 100644 23/b.py create mode 100644 24/a.py create mode 100644 24/b.py create mode 100644 24/c.py diff --git a/23/a.py b/23/a.py new file mode 100644 index 0000000..357dde8 --- /dev/null +++ b/23/a.py @@ -0,0 +1,65 @@ +import itertools +from copy import deepcopy + +cups = [ int(x) for x in list(str(389125467)) ] + +size = len(cups) + +def showcursor(cups, idx): + cups_ = deepcopy(cups) + cups_[idx] = '('+str(cups_[idx])+')' + return cups_ + +maxv = max(cups) + +def getdest(cups, value): + idx_ = value - 1 + while True: + if idx_ in cups: + return cups.index(idx_) + else: + idx_ -= 1 + if idx_ < 0: + idx_ = maxv + +def pick(cups, idx): + buf = [] + d = 0 + for i in range(0,3): + if idx in range(len(cups)): + buf.append(cups.pop(idx)) + else: + buf.append(cups.pop(0)) + d += 1 + return [buf,d] + +pointer = 0 +i = 0 +# while i < 100: +while i < 10^6: + # print(showcursor(cups, pointer)) + val = cups[pointer] + buf = pick(cups,pointer+1) + if pointer+1 in range(len(cups)): + next_ptr_value = cups[pointer+1] + else: + next_ptr_value = cups[0] + + dest = getdest(cups,val)+1 + # print('dest:', dest, cups[dest-1]) + # print(next_ptr_value) + # print('buf:', buf) + if dest < len(cups): + [cups.insert(dest,x) for x in buf[0][::-1]] + else: + # print('alt') + [cups.append(x) for x in buf[0]] + i += 1 + pointer = cups.index(next_ptr_value) + # print('nextptr', pointer, next_ptr_value) + # print(showcursor(cups, pointer)) + # print() + +print(cups) +pointer = cups.index(1) +print(int(''.join([ str(cups[x%len(cups)]) for x in range(pointer,pointer+len(cups)) ][1:]))) \ No newline at end of file diff --git a/23/b.py b/23/b.py new file mode 100644 index 0000000..4c99f8f --- /dev/null +++ b/23/b.py @@ -0,0 +1,64 @@ +import itertools +from copy import deepcopy + +def convert_to_list(lpt,start): #For research purposes + next_v = lpt[start][1] + li = [start,next_v] + c = 0 + while next_v != start: + c += 1 + next_v = lpt[next_v][1] + li.append(next_v) + if c > 15: + break + return li[:-1] + +cups_l = [ int(x) for x in list(str(389125467)) ] +cups = { cups_l[i]:[cups_l[i-1],cups_l[i+1]] for i in range(len(cups_l)-1) } +cups.update({cups_l[-1]:[cups_l[-2],cups_l[0]]}) # Let's make a linked list! Today I learned : linked lists are pretty cool! +next_cursor = cups_l[0] # Initialize cursor at cursor of tape. + +def relink(cups, idx1, idx2): # Make link from idx1 to idx2 + cups[idx1][1] = idx2 + cups[idx2][0] = idx1 + return cups + +def next(cups,cursor,n): + for i in range(0,n+1): + cursor = cups[cursor][1] + return cursor + +max_val = max(cups.keys()) +def get_destination(idx,picked): + idx -= 1 + while True: + if idx > 0 and idx not in picked: + return idx + else: + idx -= 1 + if idx <= 0: + idx = max_val + +z = 0 +while z < 3: + cursor = next_cursor + next_cursor = next(cups,cursor,3) + picked = [ next(cups,cursor,n) for n in range(0,3) ] + dest = get_destination(cursor,picked) + after_picked = next(cups, cursor, 3) + after_dest = next(cups,dest,1) + after_picked_dest = next(cups, dest, 3) + print() + print('CURSOR',cursor,'NEXT_CURSOR',next_cursor, 'PICK', picked, 'DEST', dest) + print(after_picked, after_dest, after_picked_dest) + print(convert_to_list(cups,cups_l[0])) + + print(cups, cursor, after_picked) + cups = relink(cups, cursor, after_picked) # after picked + # print(cups, dest, picked[0]) + # cups = relink(cups, dest, after_dest) + cups = relink(cups, dest, picked[0]) + # print(cups, picked[2], after_picked_dest) + cups = relink(cups, picked[2], after_picked_dest) + z += 1 + print(cups) \ No newline at end of file diff --git a/24/a.py b/24/a.py new file mode 100644 index 0000000..65f866c --- /dev/null +++ b/24/a.py @@ -0,0 +1,41 @@ +# This isn't working... knew something was wrong with just using basic +/-1 translations, you would expect something with irrational numbers by exressing a hex grid as a 2d vector (square grid) + +# directions = [x.replace('e','e ').replace('w','w ')[:-1].split(' ') for x in open('input').read().splitlines()] + +# D = { +# 'ne': 0+1j, +# 'se': 0-1j, +# 'sw': -1-1j, +# 'nw': -1+1j, +# 'e': 1+0j, +# 'w': -1+0j +# } +# # print(directions) + +# #So a bit of a problem: NWSW != N+S+W+W. So we need to use the identity NWSW = W, NESE = E, ... +# vectors = [] +# for direction in directions: +# count = {x:direction.count(x) for x in D } +# vector = 0+0j +# print(direction,count) +# de = count['ne'] - count['se'] +# vector += min(count['ne'],count['se'])*D['e'] +# if de > 0: +# vector += de*D['ne'] +# else: +# vector += de*D['se'] +# dw = count['nw'] - count['sw'] +# vector += min(count['nw'],count['sw'])*D['w'] +# if dw > 0: +# vector += dw*D['nw'] +# else: +# vector += dw*D['sw'] +# vector += (count['e']*D['e'] + count['w']*D['w']) +# # print(vector) +# vectors.append(vector) +# # vectors.append(sum([ D[x] for x in direction ])) + +# print({x:vectors.count(x) for x in vectors}) +# print() +# print({x:vectors.count(x) for x in vectors if (vectors.count(x)%2 == 1)}) #196 too low, 181 too low +# print(len([x for x in vectors if (vectors.count(x)%2 == 1)])) #196 too low, 181 too low \ No newline at end of file diff --git a/24/b.py b/24/b.py new file mode 100644 index 0000000..d9208a7 --- /dev/null +++ b/24/b.py @@ -0,0 +1,22 @@ +directions = [x.replace('e','e ').replace('w','w ')[:-1].split(' ') for x in open('input').read().splitlines()] + +import math +import cmath + +def cround(z,n): + return round(z.real, n)+1j*round(z.imag, n) + +D = { + 'ne': math.sqrt(5)*cmath.exp(1j*math.pi/3), + 'se': math.sqrt(5)*cmath.exp(-1j*math.pi/3), + 'sw': math.sqrt(5)*cmath.exp(-1j*2*math.pi/3), + 'nw': math.sqrt(5)*cmath.exp(1j*2*math.pi/3), + 'e': math.sqrt(5)+0j, + 'w': -math.sqrt(5)+0j +} + +vectors = [] +for direction in directions: + vectors.append(cround(sum([ D[x] for x in direction ]),6)) + +print(len([x for x in vectors if (vectors.count(x)%2 == 1)])) \ No newline at end of file diff --git a/24/c.py b/24/c.py new file mode 100644 index 0000000..f2f35ab --- /dev/null +++ b/24/c.py @@ -0,0 +1,81 @@ +from operator import add, sub +from copy import deepcopy + +directions = [x.replace('e','e ').replace('w','w ')[:-1].split(' ') for x in open('input').read().splitlines()] + +def getHexAdjacent(vec): + adj = {} + di,dj,dk = (0,1,1) + adj.update({ + 'sw': (vec[0]+di,vec[1]+dj,vec[2]+dk), + 'ne': (vec[0]-di,vec[1]-dj,vec[2]-dk) + }) + di,dj,dk = (1,-1,0) + adj.update({ + 'e': (vec[0]+di,vec[1]+dj,vec[2]+dk), + 'w': (vec[0]-di,vec[1]-dj,vec[2]-dk) + }) + di,dj,dk = (1,0,1) + adj.update({ + 'se': (vec[0]+di,vec[1]+dj,vec[2]+dk), + 'nw': (vec[0]-di,vec[1]-dj,vec[2]-dk) + }) + return adj + +def getHexAdjacentState(vec): + getHexAdjacent + return True + +def toggle(boolean): + return (False if boolean else True) + +grid = {(0,0,0):{'state':False}}#'adj':getHexAdjacent((0,0,0)), + +vectors = [] +for direction in directions: + # print(direction) + vec = (0,0,0) + for dir_ in direction: + vec = getHexAdjacent(vec)[dir_] + grid.update({vec:{'state':toggle(grid[vec]['state'] if vec in grid else False)}}) + vectors.append(vec) +# print({x:vectors.count(x) for x in vectors}) +# print(len([vectors.count(x) for x in vectors if (vectors.count(x) % 2 == 1)])) + +#Part 1 +print(len([ x for x in grid if (grid[x]['state'] == True)])) # Black => True, white => false + +grid_ = deepcopy(grid) #expand our space by one hextile in each direction +for cell in grid: + adj = getHexAdjacent(cell) + for cell_ in adj: + cell_ = adj[cell_] + if cell_ not in grid: + grid_.update({cell_:{'state':False}}) + +i = 0 +while i < 100: + grid = grid_ + grid_ = deepcopy(grid) + for cell in grid: + adj = getHexAdjacent(cell) + c = 0 + for cell_ in adj: + cell_ = adj[cell_] + if cell_ in grid: + if grid[cell_]['state'] == True: + c+=1 + else: + # print(len(grid_)) + grid_.update({cell_:{'state':False}}) + + if grid[cell]['state'] == True and (c == 0 or c > 2): + # print('false') + grid_[cell]['state'] = False + elif grid[cell]['state'] == False and c == 2: + # print('true') + grid_[cell]['state'] = True + # print(len(grid)) + # print(len(grid_)) + i+=1 +print(len([ x for x in grid_ if (grid_[x]['state'] == True)]))