-
Notifications
You must be signed in to change notification settings - Fork 0
/
day5part2.py
140 lines (112 loc) · 3.67 KB
/
day5part2.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import copy
import sys
import numpy as np
import pandas as pd
import matplotlib
import re
def getManAdjacentIndices(row, col, arr):
m = len(arr)
n = len(arr[0])
adjacent_indices = []
if row > 0:
adjacent_indices.append((row - 1, col))
if row+1 < m:
adjacent_indices.append((row + 1, col))
if col > 0:
adjacent_indices.append((row, col - 1))
if col+1 < n:
adjacent_indices.append((row, col + 1))
return adjacent_indices
def getAdjacentIndicies(x, y, arr):
position = (x, y)
adj = []
for dx in range(-1, 2):
for dy in range(-1, 2):
rangeX = range(0, arr.shape[0]) # X bounds
rangeY = range(0, arr.shape[1]) # Y bounds
(newX, newY) = (position[0] + dx, position[1] + dy) # adjacent cell
if (newX in rangeX) and (newY in rangeY) and (dx, dy) != (0, 0):
adj.append((newX, newY))
return adj
def extractKeys(string):
alphaRemoved = re.sub("[^0-9]", "", string)
num = int(alphaRemoved)
numRemoved = re.sub(r"\d+", "", string).replace(" ", "")
return [numRemoved, num]
def lazyAdd(dict, key, num):
if key in dict:
dict[key] += num
else:
dict[key] = num
def safeIndex(arr, row, col):
if row > -1 and row < len(arr):
slice = arr[row]
# print(slice)
if col > -1 and col < len(slice):
return slice[col]
else:
return "."
else:
return "."
def invDict(dict):
return {v: k for k, v in dict.items()}
def hashList(list):
return str(list)
def unHashListInt(string):
elements = string.replace('[', '').replace(']', '').split(',')
return [int(element) for element in elements]
def unHashListDouble(string):
elements = string.replace('[', '').replace(']', '').split(',')
return [float(element) for element in elements]
def intify(arr):
return [int(string) for string in arr]
def doubleify(arr):
return [float(string) for string in arr]
def echo(string):
print(string)
return string
if __name__ == '__main__':
dataFile = open("day5.txt", "r")
data = dataFile.read()
data = data.split('\n')
data = data[:-1]
runningSum = 0
seeds = data[0].split(' ')
seeds.pop(0)
seeds = [int(string) for string in seeds]
data.pop(0)
maps = [{}, {}, {}, {}, {}, {}, {}]
currentMap = -1
newRanges = []
for index, seed in enumerate(seeds): # knuth forgive me for what i'm about to do
if index % 2 == 0:
start = seed
end = seed + seeds[index+1] - 1
newRanges.append([start, end])
checkingRanges = []
keyNums = []
for index, dataPoint in enumerate(data):
print('--------')
if ':' in dataPoint:
currentMap += 1
checkingRanges = newRanges
newRanges = []
elif not(dataPoint == ''):
splits = dataPoint.split(" ")
splits = [int(string) for string in splits]
srcStart = splits[1]
dstStart = splits[0]
rangeLen = splits[2]
srcEnd = srcStart + rangeLen - 1
dstDiff = dstStart - srcStart
for numRange in checkingRanges:
start = numRange[0]
end = numRange[1]
low = max(srcStart, start)
high = min(srcEnd, end)
if low < high:
newRanges.append([low + dstDiff, high + dstDiff])
keyNums += [low, high]
# newRanges.append([min(srcStart, low), min(srcEnd, high)])
# newRanges.append([max(srcStart, low), max(srcEnd, high)])
print(np.array(newRanges).min())