-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday05.py
112 lines (78 loc) · 2.81 KB
/
day05.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import re, time
from common import *
startTime = time.time()
def part1():
lines = getDayInput(5, 1, False, True)
seeds = re.compile('\d+').findall(lines[0]) # first use of regex this year!
lines.pop(0) # we've now looked at our seeds
# mattie did this what the fuck is a map
seeds = list(map(lambda i: int(i), seeds))
lovelyLittleDatabase = {}
locations = []
maps = "\n".join(lines).split("\n\n")
for m in maps: # for each map section
thisMap = m.split("\n")
title = None
for line in thisMap:
if line.endswith(":"):
title = line
lovelyLittleDatabase[title] = []
continue
numbers = line.split(" ")
if title:
lovelyLittleDatabase[title].append(numbers)
for seed in seeds:
for m in lovelyLittleDatabase:
numberList = lovelyLittleDatabase[m]
for setOfNumbers in numberList:
dest, source, length = setOfNumbers[0], setOfNumbers[1], setOfNumbers[2]
dest, source, length = int(dest), int(source), int(length)
if seed >= source and seed <= source + length:
seed = dest + (seed - source)
# print(seed)
break
# print("new seed time")
locations.append(seed)
# find lowest location
return min(locations)
def part2():
lines = getDayInput(5, 2, False, True)
# first use of regex this year!
seedsRanges = re.compile('\d+').findall(lines[0])
lines.pop(0) # we've now looked at our seeds
# mattie did this what the fuck is a map
seedsRanges = list(map(lambda i: int(i), seedsRanges))
lovelyLittleDatabase = {}
locations = []
maps = "\n".join(lines).split("\n\n")
for m in maps: # for each map section
thisMap = m.split("\n")
title = None
for line in thisMap:
if line.endswith(":"):
title = line
lovelyLittleDatabase[title] = []
continue
numbers = line.split(" ")
if title:
lovelyLittleDatabase[title].append(numbers)
for i in range(0, len(seedsRanges), 2):
print(f"{round(time.time() - startTime, 2)} secs - starting {seedsRanges[i]} to {seedsRanges[i] + seedsRanges[i + 1]} ({(seedsRanges[i] + seedsRanges[i + 1]) - seedsRanges[i]} to do)")
for seed in range(seedsRanges[i], seedsRanges[i] + seedsRanges[i + 1]):
for m in lovelyLittleDatabase:
numberList = lovelyLittleDatabase[m]
for setOfNumbers in numberList:
dest, source, length = setOfNumbers[0], setOfNumbers[1], setOfNumbers[2]
dest, source, length = int(dest), int(source), int(length)
if seed >= source and seed <= source + length:
seed = dest + (seed - source)
# print(seed)
break
locations.append(seed)
print(f"{round(time.time() - startTime, 2)} secs - done {i} of {len(seedsRanges)}")
# find lowest location
return min(locations)
print(f"Part 1: {part1()}")
print(f"Time: {round(time.time() - startTime, 2)} seconds")
print(f"Part 2: {part2()}")
print(f"Time: {round(time.time() - startTime, 2)} seconds")