day 09.2024
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
import sys,time
|
import sys,time
|
||||||
from helpingFunctions import *
|
from helpingFunctions import *
|
||||||
|
from collections import defaultdict,deque
|
||||||
|
|
||||||
setSampleMode(False)
|
setSampleMode(False)
|
||||||
|
|
||||||
@@ -13,9 +14,61 @@ aocYear = identifyYear(sys.argv[0])
|
|||||||
|
|
||||||
path = getPath2Data(aocDay,aocYear)
|
path = getPath2Data(aocDay,aocYear)
|
||||||
filename = getFilename2Data(aocDay)
|
filename = getFilename2Data(aocDay)
|
||||||
|
|
||||||
|
def buildBlocks(data):
|
||||||
|
dataBlocks=[]
|
||||||
|
for inputData in range(0,len(data[0])-2,2):
|
||||||
|
dataBlocks.append((int(data[0][inputData]),int(data[0][inputData+1])))
|
||||||
|
dataBlocks.append((int(data[0][inputData+2]),0))
|
||||||
|
return dataBlocks
|
||||||
|
|
||||||
|
def expandDataBlock(inputData, dataBlock):
|
||||||
|
expDataBlock=[]
|
||||||
|
for i in range(0,dataBlock[0]):
|
||||||
|
expDataBlock.append(str(inputData))
|
||||||
|
for i in range(0,dataBlock[1]):
|
||||||
|
expDataBlock.append(None)
|
||||||
|
return expDataBlock
|
||||||
|
|
||||||
|
def cleanUpString(data):
|
||||||
|
data = deque(data)
|
||||||
|
res = []
|
||||||
|
while data:
|
||||||
|
value = data.popleft()
|
||||||
|
if value == None:
|
||||||
|
while data and value == None:
|
||||||
|
value = data.pop()
|
||||||
|
if value != None:
|
||||||
|
res.append(value)
|
||||||
|
return res
|
||||||
|
|
||||||
|
#def cleanUpString(data):
|
||||||
|
# for i in range(0,len(data)):
|
||||||
|
# if i >= len(data):
|
||||||
|
# break
|
||||||
|
# if data[i] == '.':
|
||||||
|
# for j in range(len(data)-1,0,-1):
|
||||||
|
# if i==j:
|
||||||
|
# return data
|
||||||
|
# if data[j] != '.':
|
||||||
|
# #data = data[:i]+data[j]+data[i+1:j]+"."+data[j+1:len(data)]
|
||||||
|
# data = data[:i]+data[j]+data[i+1:j]
|
||||||
|
# break
|
||||||
|
# return data
|
||||||
|
|
||||||
|
def checksumme(data):
|
||||||
|
ret = 0
|
||||||
|
for i in range(0,len(data)):
|
||||||
|
ret = ret+(i*int(data[i]))
|
||||||
|
return ret
|
||||||
|
|
||||||
def taskA (data) -> int:
|
def taskA (data) -> int:
|
||||||
|
expandedData=[]
|
||||||
gameScoreA = 0
|
gameScoreA = 0
|
||||||
|
dataBlocks=buildBlocks(data)
|
||||||
|
for i in range(len(dataBlocks)):
|
||||||
|
expandedData = expandedData + expandDataBlock(i,dataBlocks[i])
|
||||||
|
gameScoreA = checksumme(cleanUpString(expandedData))
|
||||||
return gameScoreA
|
return gameScoreA
|
||||||
|
|
||||||
def taskB (data) -> int:
|
def taskB (data) -> int:
|
||||||
|
|||||||
67
2024/day09/day09a.py
Normal file
67
2024/day09/day09a.py
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
import sys
|
||||||
|
from collections import deque
|
||||||
|
|
||||||
|
def Checksum(arr):
|
||||||
|
total = 0
|
||||||
|
for n, num in enumerate(arr):
|
||||||
|
if num != None:
|
||||||
|
total += n * num
|
||||||
|
return total
|
||||||
|
|
||||||
|
L = open('/home/bandeira/git.bandeira/aoc/2024/day09/input.day09').read().strip().split('\n')
|
||||||
|
SPACES = []
|
||||||
|
FILES = []
|
||||||
|
l = L[0]
|
||||||
|
pos = 0
|
||||||
|
line = []
|
||||||
|
file = True
|
||||||
|
fid = 0
|
||||||
|
for ch in list(l):
|
||||||
|
if file == True:
|
||||||
|
line.extend([fid for _ in range(int(ch))])
|
||||||
|
FILES.append((fid, int(ch), pos))
|
||||||
|
fid += 1
|
||||||
|
else:
|
||||||
|
line.extend([None for _ in range(int(ch))])
|
||||||
|
SPACES.append((pos, int(ch)))
|
||||||
|
pos += int(ch)
|
||||||
|
file ^= True
|
||||||
|
LEN = pos
|
||||||
|
|
||||||
|
dq = deque(line)
|
||||||
|
nl = []
|
||||||
|
while dq:
|
||||||
|
h = dq.popleft()
|
||||||
|
if h == None:
|
||||||
|
while dq and h == None:
|
||||||
|
h = dq.pop()
|
||||||
|
if h != None:
|
||||||
|
nl.append(h)
|
||||||
|
print(Checksum(nl))
|
||||||
|
|
||||||
|
p2 = [None] * LEN
|
||||||
|
|
||||||
|
for item in reversed(FILES):
|
||||||
|
num, req, atpos = item
|
||||||
|
moved = False
|
||||||
|
for s in range(len(SPACES)):
|
||||||
|
spos, slen = SPACES[s]
|
||||||
|
if spos > atpos:
|
||||||
|
break
|
||||||
|
if slen >= req:
|
||||||
|
pos = spos
|
||||||
|
SPACES[s] = (spos + req, slen - req)
|
||||||
|
for _ in range(req):
|
||||||
|
p2[pos] = num
|
||||||
|
pos += 1
|
||||||
|
moved = True
|
||||||
|
if SPACES[s][1] == 0:
|
||||||
|
del SPACES[s]
|
||||||
|
break
|
||||||
|
|
||||||
|
if not moved:
|
||||||
|
pos = item[2]
|
||||||
|
for _ in range(req):
|
||||||
|
p2[pos] = item[0]
|
||||||
|
pos += 1
|
||||||
|
print(Checksum(p2))
|
||||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1 @@
|
|||||||
|
2333133121414131402
|
||||||
Reference in New Issue
Block a user