Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- with open("input") as file:
- data=file.read().splitlines()
- loc = []
- keys = ['seeds','seed','soil','fert','wate','ligh','temp','humi']
- maps = dict(zip(keys, [[] for _ in range(len(keys))]))
- def mappa(c, v):
- for x,y,z in maps[c]:
- if v>=y and v < y+z:
- return x + v - y
- return v
- #riempio dizionario
- for i,line in enumerate(data):
- if i==0:
- maps['seeds']=list(map(int, line.split(":")[1].split()))
- continue
- if re.search(r"\w+:",line):
- r=line[0:4]
- if re.search(r"\d+", line):
- x, y, z = map(int, line.split())
- maps[r].append((x, y, z))
- #faccio mappaggi
- for s in maps['seeds']:
- s = int(s)
- for x in maps:
- if x=='seeds':
- continue
- s = mappa(x, s)
- loc.append(s)
- print(f"Part1:{min(loc)}")
- loc=[]
- def mappa_range(c, ranges):
- p = []
- for (x, y, z) in maps[c]:
- e = y + z
- new_range = []
- while ranges:
- (a, b) = ranges.pop()
- if min(b, y) > a:
- new_range.append((a, min(b, y)))
- if min(e,b)>max(y,a):
- p.append((max(y,a) - y + x, min(e,b) - y + x))
- if b > max(e, a):
- new_range.append((max(e, a), b))
- ranges = new_range
- return p + ranges
- for i,j in zip(maps['seeds'][::2],maps['seeds'][1::2]):
- ranges = [(i, i + j)]
- for x in maps:
- if x=='seeds':
- continue
- ranges = mappa_range(x, ranges)
- loc.append(min(ranges)[0])
- print(f"Part2:{min(loc)}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement