-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay05p2.py
92 lines (72 loc) · 2.27 KB
/
Day05p2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import sys
import datetime
input = open("Day05-input.txt", "r")
lines = input.read().splitlines()
class Mapping:
def __init__(self, sourceStart, destinationStart, range) -> None:
self.sourceStart = sourceStart
self.destinationStart = destinationStart
self.range = range
def __repr__(self) -> str:
return f"dest@{self.destinationStart} src@{self.sourceStart} rng@{self.range}"
class Map:
def __init__(self, source: int, destination: int) -> None:
self.source = source
self.destination = destination
self.mappings = []
def __repr__(self) -> str:
return f"{self.source}->{ self.destination}"
class Seed:
def transform(map: Map, value) -> None:
for mapping in map.mappings:
if mapping.sourceStart <= value <= mapping.sourceStart + mapping.range - 1:
difference = value - mapping.sourceStart
return mapping.destinationStart + difference
return value
maps = []
print("# creating seeds")
seedsLine = lines[0].replace("seeds: ", "")
seedsSplit = seedsLine.split(" ")
seedPairs = list(map(int, seedsSplit))
lines.pop(0)
lines.pop(0)
print("# creating maps")
map = None
for index, line in enumerate(lines):
if "map:" in line:
if map != None:
maps.append(map)
line = line.replace(" map:", "")
lineSplit = line.split("-to-")
map = Map(lineSplit[0], lineSplit[1])
continue
if line != "":
lineSplit = line.split(" ")
mapping = Mapping(int(lineSplit[1]), int(lineSplit[0]), int(lineSplit[2]))
map.mappings.append(mapping)
if index == len(lines) - 1 and map != None:
maps.append(map)
print("# transforming seeds")
# numSeeds = len(seeds)
# for index, seed in enumerate(seeds):
# if index % 1000 == 0:
# print(f"{index} of {numSeeds}")
# for map in maps:
# seed.transform(map)
processed = 0
minimum = sys.maxsize
print(f"### START {datetime.datetime.now()}")
for i in range(0, len(seedPairs), 2):
for seedNumber in range(seedPairs[i], seedPairs[i] + seedPairs[i+1]):
number = seedNumber
for map in maps:
number = Seed.transform(map, number)
if number < minimum:
minimum = number
print(f"New min: {minimum}")
processed += 1
if processed % 1000000 == 0:
print(f"{processed}")
print("####")
print(f"### END {datetime.datetime.now()}")
print(minimum)