Working on 2023-12-19 p1
This commit is contained in:
parent
48918e0077
commit
1d187ed320
191
2023/day19/part1.py
Normal file
191
2023/day19/part1.py
Normal file
@ -0,0 +1,191 @@
|
|||||||
|
import sys
|
||||||
|
import os
|
||||||
|
from pprint import pprint
|
||||||
|
import time
|
||||||
|
import operator
|
||||||
|
|
||||||
|
from shapely.geometry import Point
|
||||||
|
from shapely.geometry.polygon import Polygon
|
||||||
|
import shapely
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
from matplotlib.path import Path
|
||||||
|
import sympy
|
||||||
|
#colors
|
||||||
|
from termcolor import colored
|
||||||
|
|
||||||
|
start_time = time.time()
|
||||||
|
|
||||||
|
def p(x,inside):
|
||||||
|
for idx,i in enumerate(x):
|
||||||
|
for jdx,j in enumerate(i):
|
||||||
|
|
||||||
|
#if j == '#':
|
||||||
|
# print(colored(j,'red'),end='')
|
||||||
|
#if (idx,jdx) in steps:
|
||||||
|
# print(colored(j,'blue'),end='')
|
||||||
|
if (idx,jdx) in inside:
|
||||||
|
print(colored(j,'green'),end='')
|
||||||
|
else:
|
||||||
|
print(j,end='')
|
||||||
|
print()
|
||||||
|
|
||||||
|
input_f = ''
|
||||||
|
if len(sys.argv) == 1:
|
||||||
|
input_f = 'test'
|
||||||
|
else:
|
||||||
|
input_f = sys.argv[1]
|
||||||
|
|
||||||
|
grid = []
|
||||||
|
|
||||||
|
with open(input_f) as file:
|
||||||
|
for line in file:
|
||||||
|
grid.append(line.rstrip().split())
|
||||||
|
|
||||||
|
|
||||||
|
workflow = {}
|
||||||
|
ratings = []
|
||||||
|
tmp_workflow = []
|
||||||
|
tmp_ratings = []
|
||||||
|
|
||||||
|
for i in grid:
|
||||||
|
if i == []:
|
||||||
|
tmp_workflow = tmp_ratings
|
||||||
|
tmp_ratings = []
|
||||||
|
else:
|
||||||
|
tmp_ratings.append(i)
|
||||||
|
|
||||||
|
|
||||||
|
for i in tmp_workflow:
|
||||||
|
r = i[0].split("{")[0]
|
||||||
|
p = i[0].split("{")[1][:-1].split(',')
|
||||||
|
workflow[r] = p
|
||||||
|
|
||||||
|
for i in tmp_ratings:
|
||||||
|
tmp = []
|
||||||
|
for j in i[0][1:-1].split(','):
|
||||||
|
tmp.append(j[2:])
|
||||||
|
ratings.append(tmp)
|
||||||
|
|
||||||
|
current = 'in'
|
||||||
|
|
||||||
|
ops = {
|
||||||
|
'<': operator.lt,
|
||||||
|
'>': operator.gt
|
||||||
|
}
|
||||||
|
|
||||||
|
def follow_workflow(current,ratings):
|
||||||
|
#print(workflow[current])
|
||||||
|
print(current)
|
||||||
|
|
||||||
|
if current == 'A':
|
||||||
|
return 'A'
|
||||||
|
elif current == 'R':
|
||||||
|
return 'R'
|
||||||
|
else:
|
||||||
|
for inst in workflow[current]:
|
||||||
|
# print('Instructions ', inst)
|
||||||
|
# print(workflow[current])
|
||||||
|
if len(workflow[current]) == 2:
|
||||||
|
end_result = workflow[current][1]
|
||||||
|
|
||||||
|
if '<' in inst or '>' in inst:
|
||||||
|
number = inst[2:].split(':')[0]
|
||||||
|
result = inst.split(':')[1]
|
||||||
|
operator = inst[1]
|
||||||
|
#print(number,operator,end=' ')
|
||||||
|
match inst[0]:
|
||||||
|
case 'x':
|
||||||
|
#print(ratings[0],end=' ')
|
||||||
|
if ops[operator](ratings[0],number):
|
||||||
|
current = result
|
||||||
|
else:
|
||||||
|
current = end_result
|
||||||
|
|
||||||
|
case 'm':
|
||||||
|
#print(ratings[1],end=' ')
|
||||||
|
|
||||||
|
print(operator,ratings[1],number)
|
||||||
|
|
||||||
|
if ops[operator](ratings[1],number):
|
||||||
|
current = result
|
||||||
|
else:
|
||||||
|
current = end_result
|
||||||
|
|
||||||
|
case 'a':
|
||||||
|
#print(ratings[2],end=' ')
|
||||||
|
if ops[operator](ratings[2],number):
|
||||||
|
current = result
|
||||||
|
else:
|
||||||
|
current = end_result
|
||||||
|
|
||||||
|
case 's':
|
||||||
|
#print(ratings[3],end=' ')
|
||||||
|
if ops[operator](ratings[3],number):
|
||||||
|
current = result
|
||||||
|
else:
|
||||||
|
current = end_result
|
||||||
|
if current in ('R','A'):
|
||||||
|
return current
|
||||||
|
else:
|
||||||
|
follow_workflow(current,ratings)
|
||||||
|
|
||||||
|
|
||||||
|
# for i in ratings:
|
||||||
|
# print('Working on ', i)
|
||||||
|
# follow_workflow(current,i)
|
||||||
|
|
||||||
|
ratings = ratings[0]
|
||||||
|
|
||||||
|
found = False
|
||||||
|
|
||||||
|
while current not in ('A','R'):
|
||||||
|
print(current,end='')
|
||||||
|
print(' -> ',end='')
|
||||||
|
#print(workflow[current])
|
||||||
|
for idx, inst in enumerate(workflow[current]):
|
||||||
|
if any(not c.isalnum() for c in inst) and found == False:
|
||||||
|
if '<' in inst or '>' in inst:
|
||||||
|
number = inst[2:].split(':')[0]
|
||||||
|
result = inst.split(':')[1]
|
||||||
|
operator = inst[1]
|
||||||
|
match inst[0]:
|
||||||
|
case 'x':
|
||||||
|
print('Going X')
|
||||||
|
if ops[operator](ratings[0],number):
|
||||||
|
current = result
|
||||||
|
found = True
|
||||||
|
else:
|
||||||
|
current = workflow[current][1]
|
||||||
|
|
||||||
|
case 'm':
|
||||||
|
print('Going M')
|
||||||
|
|
||||||
|
if ops[operator](ratings[1],number):
|
||||||
|
current = result
|
||||||
|
found = True
|
||||||
|
else:
|
||||||
|
current = workflow[current][1]
|
||||||
|
|
||||||
|
case 'a':
|
||||||
|
print('Going A')
|
||||||
|
if ops[operator](ratings[2],number):
|
||||||
|
current = result
|
||||||
|
found = True
|
||||||
|
else:
|
||||||
|
current = workflow[current][1]
|
||||||
|
|
||||||
|
case 's':
|
||||||
|
print('Going S')
|
||||||
|
if ops[operator](ratings[3],number):
|
||||||
|
current = result
|
||||||
|
found = True
|
||||||
|
else:
|
||||||
|
current = workflow[current][1]
|
||||||
|
print(current)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
print("--- %s seconds ---" % (time.time() - start_time))
|
Loading…
Reference in New Issue
Block a user