-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.py
147 lines (124 loc) · 4.86 KB
/
day9.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
141
142
143
144
145
146
147
import time
INPUT = './inputs/9.input'
def part1():
with open(INPUT) as f:
diskmap = [int(x) for x in list(f.read())]
memMap = []
nextFilePos = len(diskmap) - 1
i = 0
# Use while loop for better control over indices
while i < len(diskmap):
if diskmap[i] == None:
i += 1
continue
# File location, add immediately
if i % 2 == 0:
file_ID = i // 2
memMap.append(f'{diskmap[i]}:{file_ID}')
diskmap[i] = None
i += 1
# Space location, pull in files from the back
else:
space = diskmap[i]
file_ID = nextFilePos // 2
fileSize = diskmap[nextFilePos]
if fileSize == None:
nextFilePos -= 2
break
if space < fileSize:
memMap.append(f'{space}:{file_ID}')
diskmap[nextFilePos] = fileSize - space
diskmap[i] = None # space is filled
i += 1
elif space > fileSize:
memMap.append(f'{fileSize}:{file_ID}')
diskmap[nextFilePos] = None # file is moved
diskmap[i] = space - fileSize # update remaining space
nextFilePos -= 2
else:
memMap.append(f'{space}:{file_ID}')
diskmap[i] = None # space is filled
diskmap[nextFilePos] = None
nextFilePos -= 2
i += 1
# Calculate checksum
blockPos = 0
checksum = 0
for block in memMap:
length, ID = block.split(":")
length, ID = int(length), int(ID)
for i in range(length):
checksum += ID * blockPos
blockPos += 1
print(f'Part 1: {checksum}')
def part2_bad():
with open(INPUT) as f:
diskmap = [int(x) for x in list(f.read())]
def insertSort(x, lst):
for i in range(len(lst)):
if x < lst[i]:
lst.insert(i, x)
return lst
lst.append(x)
return lst
# Track positions of spaces of all sizes and create memory map
files = [] # Stores tuples of (ID, length)
spaces = {} # size: [list of positions]
memMap = []
posList = []
for i in range(len(diskmap)):
if posList == []:
posList.append(0)
elif i % 2 == 0:
posList.append(posList[-1] + diskmap[i-1] + diskmap[i-2])
if i%2 == 0:
memMap.extend([i // 2] * diskmap[i])
files.append((i // 2, diskmap[i]))
else:
memMap.extend([None] * diskmap[i])
spacePos = posList[-1] + diskmap[i-1]
if diskmap[i] in spaces:
spaces[diskmap[i]].append(spacePos)
else:
spaces[diskmap[i]] = [spacePos]
# Work backwards through the list of files
for i in range(len(posList) - 1, -1, -1):
# Analyze next file in line
fileID, fileSize = files.pop()
filePos = posList[i]
bestSpace = fileSize
for size in range(fileSize, 10):
if size in spaces and filePos >= spaces[size][0]:
if bestSpace not in spaces or spaces[size][0] < spaces[bestSpace][0]:
bestSpace = size
if bestSpace in spaces:
if filePos >= spaces[bestSpace][0]:
newPos = spaces[bestSpace].pop(0)
# Clean up
if spaces[bestSpace] == []:
del spaces[bestSpace]
for j in range(fileSize):
memMap[newPos + j] = fileID
memMap[filePos + j] = None
# Track position of leftover space
if bestSpace > fileSize:
leftoverSpace = bestSpace - fileSize
if leftoverSpace in spaces:
insertSort(newPos + fileSize, spaces[leftoverSpace])
else:
spaces[leftoverSpace] = [newPos + fileSize]
checksum = 0
for i in range(len(memMap)):
if memMap[i] != None:
checksum += memMap[i] * i
print(f'Part 2: {checksum}')
def part2():
with open(INPUT) as f:
diskmap = [int(x) for x in list(f.read())]
def clock(f):
start = time.time()
f()
end = time.time()
return round((end - start) * 1000, 3)
print(f'Part 1 took {clock(part1)} ms to run')
print(f'Part 2 took {clock(part2)} ms to run')