diff options
| author | nekineki <nekineki@nekineki.net> | 2022-12-22 09:38:51 +0100 |
|---|---|---|
| committer | nekineki <nekineki@nekineki.net> | 2022-12-22 16:12:54 +0100 |
| commit | 4235cbd70404d895b753b15335d3968b21a0c27f (patch) | |
| tree | 203ebe9eb1c3965167c7e19fa1fef0cf78b9fdd8 /2022/day22.py | |
| parent | dcc2e3092ad021d05b25f9cdd514ba8767463905 (diff) | |
day22
Diffstat (limited to '2022/day22.py')
| -rwxr-xr-x | 2022/day22.py | 264 |
1 files changed, 264 insertions, 0 deletions
diff --git a/2022/day22.py b/2022/day22.py new file mode 100755 index 0000000..1aab294 --- /dev/null +++ b/2022/day22.py @@ -0,0 +1,264 @@ +#!/usr/bin/env python3 + +# import numpy as np +from functools import reduce +from re import findall +from copy import deepcopy +import sys + +# filename = "in/day22.ref" +filename = "in/day22.pzl" +data = open(filename).read() +lines = [line for line in data.rstrip('\n').split('\n')] +# print(lines) + +res2 = 0 + +y_len = len(lines) - 2 + 2 +x_len = max([len(lines[i]) for i in range(y_len-2)]) + 2 +# print(y_len) +# print(x_len) + +A = [ [' ' for x in range(x_len)] for y in range(y_len)] +for y,line in enumerate(lines[:-2]): + for x, val in enumerate(line): + A[y+1][x+1] = val + +# for line in A: +# for char in line: +# print(char, end='') +# print() + +ops_s = lines[-1] +i = 0 +ops = list() + +while i < len(ops_s): + find_r = ops_s.find('R',i) + find_l = ops_s.find('L',i) + + if find_r == -1 and find_l == -1: + ops.append(int(ops_s[i:])) + break + elif find_r == -1: + lr = 'L' + nxt = find_l + elif find_l == -1: + lr = 'R' + nxt = find_r + elif find_l < find_r: + lr = 'L' + nxt = find_l + else: + lr = 'R' + nxt = find_r + + ops.append(int(ops_s[i:nxt])) + ops.append(lr) + i = nxt+1 +# print(ops) + + +def wrap(A, z, d): + new_z = z + new_d = -d + while A[int(new_z.imag)][int(new_z.real)] != ' ': + new_z += new_d + new_z += d + + if A[int(new_z.imag)][int(new_z.real)] == '#': + # print('unsuccessful wrap from', z) + return z, d + else: + # print('wrapping to', new_z) + return new_z, d + +def solve(part): + d = 1 + z = 1 + 1j + while A[int(z.imag)][int(z.real)] != '.': + z += 1 + # print(z) + + for op in ops: + # print(op) + if type(op) == int: + for i in range(op): + new_z = z + d + val = A[int(new_z.imag)][int(new_z.real)] + if val == '.': + z = new_z + elif val == '#': + # print('hit #') + break + else: + if part == 1: + z, d = wrap(A, z, d) + elif part == 2: + z, d = wrap2(A, z, d) + # print('z', z) + + elif type(op) == str: + if op == 'R': + d *= 1j + elif op == 'L': + d *= -1j + # print(op, z, d, new_z) + # print() + + res = 0 + res += 1000*int(z.imag) + res += 4*int(z.real) + if d == 1: + res += 0 + elif d == 1j: + res += 1 + elif d == -1: + res += 2 + elif d == -1j: + res += 3 + return res + + +# len +# L = 4 + +# # tiles +# T = dict() +# T['a'] = 9 + 1j +# T['b'] = 1 + 5j +# T['c'] = 5 + 5j +# T['d'] = 9 + 5j +# T['e'] = 9 + 9j +# T['f'] = 13 + 9j + +# # edges +# E = dict() +# E[('a',1+0j)] = ('f',-1+0j) +# E[('a',-1+0j)] = ('c',1j) +# E[('a',-1j)] = ('b',1j) + +# E[('b', 1j)] = ('e',-1j) +# E[('b',-1+0j)] = ('f',-1j) +# E[('b',-1j)] = ('a',1j) + +# E[('c', 1j)] = ('e',1+0j) +# E[('c',-1j)] = ('a',1+0j) + +# E[('d',1+0j)] = ('f',1j) + +# E[('e', 1j)] = ('b',-1j) +# E[('e',-1+0j)] = ('c',-1j) + +# E[('f', 1+0j)] = ('a',-1) +# E[('f', 1j)] = ('b',1+0j) +# E[('f',-1j)] = ('d',-1) + + + +# len +L = 50 + +# tiles +T = dict() +T['a'] = 51 + 1j +T['b'] = 101 + 1j +T['c'] = 51 + 51j +T['d'] = 1 + 101j +T['e'] = 51 + 101j +T['f'] = 1 + 151j + +# edges +E = dict() +# E[(' ', 1+0j)] = (' ',1j) +# E[(' ', 0+1j)] = (' ',1j) +# E[(' ',-1+0j)] = (' ',1j) +# E[(' ', 0-1j)] = (' ',1j) + +E[('a',-1+0j)] = ('d',1) +E[('a', 0-1j)] = ('f',1) + +E[('b', 1+0j)] = ('e',-1) +E[('b', 0+1j)] = ('c',-1) +E[('b', 0-1j)] = ('f',-1j) + +E[('c', 1+0j)] = ('b',-1j) +E[('c',-1+0j)] = ('d',1j) + +E[('d',-1+0j)] = ('a',1) +E[('d', 0-1j)] = ('c',1) + +E[('e', 1+0j)] = ('b',-1) +E[('e', 0+1j)] = ('f',-1) + +E[('f', 1+0j)] = ('e',-1j) +E[('f', 0+1j)] = ('b',1j) +E[('f',-1+0j)] = ('a',1j) + + +def get_tile(z): + for tile in T: + if T[tile].real <= z.real < T[tile].real+L and \ + T[tile].imag <= z.imag < T[tile].imag+L: + return tile + return ' ' + +# for y in range(y_len): +# for x in range(x_len): +# # print(A[y][x], end='') +# z = x + 1j*y +# print(get_tile(z), end='') +# print() + +# print(get_tile(9+1j)) +# exit() + +def wrap2(A, z, d): + tile = get_tile(z) + + if (tile, d) not in E.keys(): + print(z, tile, d) + assert False + + # print('from tile', tile, d) + new_tile, new_d = E[(tile,d)] + # print('to tile', new_tile, new_d) + + rela_z = ( ((z.real - 1)%L) ) + rela_z += ( ((z.imag - 1)%L) ) *1j + + # rot = new_d / d + + if d == -1j: + w = rela_z.real + elif d == 1: + w = rela_z.imag + elif d == 1j: + w = L-1 - rela_z.real + elif d == -1: + w = L-1 - rela_z.imag + + if new_d == 1j: + new_rela_z = L-1 - w + 0j + elif new_d == -1: + new_rela_z = (L-1) + (L-1 - w)*1j + elif new_d == -1j: + new_rela_z = w + (L-1)*1j + elif new_d == 1: + new_rela_z = 0 + w*1j + + # print('w', w) + # print('new_rela_z', new_rela_z) + new_z = T[new_tile] + new_rela_z + + if A[int(new_z.imag)][int(new_z.real)] == '#': + # print('unsuccessful wrap from', z) + return z, d + else: + # print('wrapping to', new_z) + return new_z, new_d + + +print('res1:', solve(1)) +print('res2:', solve(2)) + |
