solved 2024 day9 part 1
This commit is contained in:
1 parent
f232a551b1
commit
21421d265e
3 files changed
+22
-12
No files matched your search
@@ -0,0 +1 @@
|
|||||||
|
2289937361462537565743654392191430243285795130435159294739821058371166572674439596984748944650114550102248505688364873372229621371122362606540477174397649912143956032765821236284479161796563703242896715613146225670757710431154401514218972769918955738355993247885853881641978164641953653673668835495411322313741486778343282298738876089766426742824494953715570396539315223643588405940756935794320308889618240773637962696225656991480607857941359647941361225474391964736562926841550728655952932856386598374142671751359412290899788354356219791102389492024564929732032669143862653312931485737164557755893818290839219543923461177327811756229235867751432339548828389256157664161627348703453629615707627382882849552449529305784316758404535105691422878697316562022191577918411422432546669231744899267699111734979896641335866854421506478733348108071964050898615677374471633544196842839885973161696776741115574499153959315286416228774931343908420721246761773496896361292549998721372826658242371868531212040981731253016339149698294501775519794176352868021286874541618161132729449353228384936983779321412763248823316113626875070813928656842499825645438136882367795242152394162167983855861745671295120305780358184734234861389148856118444307847658938976441418894619654681497833261503220885845311854739027754656574561815277356646834354138368445755518364125561524084262264802188396725861850416147479481219348317448914777708636661185508795672190279884882840545855557637996791535092648434431226984780525211849019807545467146238198183843598225439425258565905019993394893642916589276018863482907210556777705746694681181660291159639978686370599475613316543088544011375796297720635246824752203921576194757549702061653679521114967342955561197554125928194289817320757424431394238493912880298991983466759541212349378030428158229655507465621863546780601292871765769770951238725195449036609998941899855930489424649624729695244617963619553053328590833549142443423524264263813528627168845985614752185195956718218097683792375433203980142176275070717611416695636038532171747227283951391266325868965567966993975016391191849548471312231944551826662173439911149618573966869491657392962598492712554558737190436834663238224554995467837467431072727448195691192191398654144892778091367242663078187934378020464558433541744955982333203062905280828710535661366781935458167071435788398210733167403821767776892661117980448414675999723458888783183352837347257063373199933059168987324288963634807112766260919564245139716988817770113277313042762695708476394152349752892168115333641297782537812354166186192092709743673514993467311365747824137422227094231393782640601415635715251193113032724592974469882413569682244396872693682623351233915472501077856081413144209167187335929588551372327122105581712799992360324222957917109647856433908452761881588524921159583736823585666125766174554754517421449363318590918016821050439646261535438568771234464716341333923383569668726315247799168846497489906051177651869552579798329132465014878134939572618595979290681350159436637331821082704555502857693555123263607648976714753271165059927762859918152885676550812319801211882623432256819088559168709622918029937999609260282014716642413670941322317130545892303652458798404597137589237157382642204440982949192625575235785492762853243743248822392542915728368158707228892629626864545355318741501819102233385615422585311036809578775830589187682439899111784626444064656168545284696527795057969037396242808465499372716546528834746459375410883852902237585273445497417862928425762181946451164941684226597748929437659446914473279291111817729930287565673722637035526034949353929346743348141257357435903199651330526660293927498891169972384350344980116315796899983898233220146030393112961542542835743020605333214722494931582568357722652922222092607469677789824770895185968781558348412586319395951034448230337633935640181282295241586763189981982930477534233621136734131667181559209093261659771015419033338996589878157087352014293020141810312138744010645179462650766692596828799361936893647684114074705393729522139099406440811076578289813690297686224825579582581053489864807786781682886414844870479420233982573624299716422255104166965350176122974642765677226095294015869522564956671065555167216118473222507974608264498683457379768586993996914973808167723517487765448389233286458170868095479694252355749051639597185347375349357184211121169020415221887293308016684791428262448438811595142980674689679127445675449144831128797112269187215915673760676094372297898565806541405834171885536561191395658339944969554434117061924739213488238198419986588799366895787436288642748775124938971930692550315458828824186079125488732165856964794333532147863515805555468798581979168388341534547349378569983172437786373211724138259789961175868488972682582082259919182150584675782421532459901690856794378946661577757647252772581614567067497471453720433473709099845247619565435475313612667453585168742193383643497793821065654961871651326234929980335359462799771974803848106327877490975011666441638146967Line truncated
|
||||||
+20
-11
@@ -1,4 +1,4 @@
|
|||||||
with open('test.text', 'r') as file:
|
with open('input.text', 'r') as file:
|
||||||
data: str = file.read().strip()
|
data: str = file.read().strip()
|
||||||
|
|
||||||
# If block has no ID then it is free
|
# If block has no ID then it is free
|
||||||
@@ -77,8 +77,8 @@ class DiskMap:
|
|||||||
|
|
||||||
if remaining_free_size != 0:
|
if remaining_free_size != 0:
|
||||||
self.blocks[free_index] = FileBlock(free_size - remaining_free_size, file_id)
|
self.blocks[free_index] = FileBlock(free_size - remaining_free_size, file_id)
|
||||||
self.blocks.insert(free_index + 1, FreeBlock(remaining_file_size))
|
self.blocks.insert(free_index + 1, FreeBlock(remaining_free_size))
|
||||||
self.blocks[file_id] = FreeBlock(free_size)
|
self.blocks[file_index + 1] = FreeBlock(file_size)
|
||||||
else:
|
else:
|
||||||
self.blocks[free_index] = FileBlock(free_size, file_id)
|
self.blocks[free_index] = FileBlock(free_size, file_id)
|
||||||
self.blocks[file_index] = FreeBlock(free_size)
|
self.blocks[file_index] = FreeBlock(free_size)
|
||||||
@@ -86,17 +86,26 @@ class DiskMap:
|
|||||||
if remaining_file_size:
|
if remaining_file_size:
|
||||||
self.blocks.insert(file_index, FileBlock(remaining_file_size, file_id))
|
self.blocks.insert(file_index, FileBlock(remaining_file_size, file_id))
|
||||||
|
|
||||||
print(free_index)
|
def __str__(self) -> str:
|
||||||
self.print()
|
s = ''
|
||||||
|
|
||||||
def print(self) -> None:
|
|
||||||
for block in self.blocks:
|
for block in self.blocks:
|
||||||
print(block, end='')
|
s += str(block)
|
||||||
print()
|
return s
|
||||||
|
|
||||||
# Parse Data
|
# Parse Data
|
||||||
diskmap = DiskMap(data)
|
diskmap = DiskMap(data)
|
||||||
|
|
||||||
diskmap.print()
|
# Part 1
|
||||||
|
print(f'Input: {diskmap}')
|
||||||
diskmap.compact()
|
diskmap.compact()
|
||||||
diskmap.print()
|
print(f'Compacted: {diskmap}')
|
||||||
|
|
||||||
|
total = 0
|
||||||
|
index = 0
|
||||||
|
for block in diskmap.blocks:
|
||||||
|
if type(block) == FileBlock:
|
||||||
|
for i in range(block.size):
|
||||||
|
total += block.id * index
|
||||||
|
index += 1
|
||||||
|
|
||||||
|
print(f'Part 1: {total}')
|
||||||
+1
-1
@@ -1 +1 @@
|
|||||||
12345
|
2333133121414131402
|
||||||
Reference in new issue
Block a user