Disk Fragmenter

Advent of Code 2024 [Day 9]

09-12-2024

Another push of the button leaves you in the familiar hallways of some friendly amphipods! Good thing you each somehow got your own personal mini submarine. The Historians jet away in search of the Chief, mostly by driving directly into walls.

While The Historians quickly figure out how to pilot these things, you notice an amphipod in the corner struggling with his computer. He’s trying to make more contiguous free space by compacting all of the files, but his program isn’t working; you offer to help.

He shows you the disk map (your puzzle input) he’s already generated. For example:

2333133121414131402

The disk map uses a dense format to represent the layout of files and free space on the disk. The digits alternate between indicating the length of a file and the length of free space.

So, a disk map like 12345 would represent a one-block file, two blocks of free space, a three-block file, four blocks of free space, and then a five-block file. A disk map like 90909 would represent three nine-block files in a row (with no free space between them).

Each file on disk also has an ID number based on the order of the files as they appear before they are rearranged, starting with ID 0. So, the disk map 12345 has three files: a one-block file with ID 0, a three-block file with ID 1, and a five-block file with ID 2. Using one character for each block where digits are the file ID and . is free space, the disk map 12345 represents these individual blocks:

0..111....22222

The first example above, 2333133121414131402, represents these individual blocks:

00...111...2...333.44.5555.6666.777.888899

The amphipod would like to move file blocks one at a time from the end of the disk to the leftmost free space block (until there are no gaps remaining between file blocks). For the disk map 12345, the process looks like this:

0..111....22222
02.111....2222.
022111....222..
0221112...22...
02211122..2....
022111222......

The first example requires a few more steps:

00...111...2...333.44.5555.6666.777.888899
009..111...2...333.44.5555.6666.777.88889.
0099.111...2...333.44.5555.6666.777.8888..
00998111...2...333.44.5555.6666.777.888...
009981118..2...333.44.5555.6666.777.88....
0099811188.2...333.44.5555.6666.777.8.....
009981118882...333.44.5555.6666.777.......
0099811188827..333.44.5555.6666.77........
00998111888277.333.44.5555.6666.7.........
009981118882777333.44.5555.6666...........
009981118882777333644.5555.666............
00998111888277733364465555.66.............
0099811188827773336446555566..............

The final step of this file-compacting process is to update the filesystem checksum. To calculate the checksum, add up the result of multiplying each of these blocks’ position with the file ID number it contains. The leftmost block is in position 0. If a block contains free space, skip it instead.

Continuing the first example, the first few blocks’ position multiplied by its file ID number are 0 * 0 = 0, 1 * 0 = 0, 2 * 9 = 18, 3 * 9 = 27, 4 * 8 = 32, and so on. In this example, the checksum is the sum of these, 1928.

Compact the amphipod’s hard drive using the process he requested. What is the resulting filesystem checksum? (Be careful copy/pasting the input for this puzzle; it is a single, very long line.)

function createBlocks(lengths: number[]): string[] {
  const blocks: string[] = [];
  let fileId = 0;
  
  for (let i = 0; i < lengths.length; i++) {
    if (i % 2 === 0) {
      for (let j = 0; j < lengths[i]; j++) {
        blocks.push(fileId.toString());
      }
      fileId++;
    } else {
      for (let j = 0; j < lengths[i]; j++) {
        blocks.push('.');
      }
    }
  }
  return blocks;
}

function compactFiles(blocks: string[]): string[] {
  const result = [...blocks];
  
  while (true) {
    let rightmostFilePos = result.length - 1;
    while (rightmostFilePos >= 0 && result[rightmostFilePos] === '.') {
      rightmostFilePos--;
    }
    if (rightmostFilePos < 0) break;

    let leftmostSpacePos = 0;
    while (leftmostSpacePos < result.length && result[leftmostSpacePos] !== '.') {
      leftmostSpacePos++;
    }
    if (leftmostSpacePos >= rightmostFilePos) break;

    result[leftmostSpacePos] = result[rightmostFilePos];
    result[rightmostFilePos] = '.';
  }
  
  return result;
}

function calculateChecksum(blocks: string[]): number {
  return blocks.reduce((sum, block, pos) => {
    if (block === '.') return sum;
    return sum + (pos * parseInt(block));
  }, 0);
}

async function main() {
  const input = await Deno.readTextFile("input.txt");
  const initialBlocks = createBlocks(input.trim().split('').map(Number));
  const compactedBlocks = compactFiles(initialBlocks);
  const checksum = calculateChecksum(compactedBlocks);
  console.log(`Filesystem checksum: ${checksum}`);
}

main().catch((err) => console.error(err));

Upon completion, two things immediately become clear. First, the disk definitely has a lot more contiguous free space, just like the amphipod hoped. Second, the computer is running much more slowly! Maybe introducing all of that file system fragmentation was a bad idea?

The eager amphipod already has a new plan: rather than move individual blocks, he’d like to try compacting the files on his disk by moving whole files instead.

This time, attempt to move whole files to the leftmost span of free space blocks that could fit the file. Attempt to move each file exactly once in order of decreasing file ID number starting with the file with the highest file ID number. If there is no span of free space to the left of a file that is large enough to fit the file, the file does not move.

The first example from above now proceeds differently:

00...111...2...333.44.5555.6666.777.888899
0099.111...2...333.44.5555.6666.777.8888..
0099.1117772...333.44.5555.6666.....8888..
0099.111777244.333....5555.6666.....8888..
00992111777.44.333....5555.6666.....8888..

The process of updating the filesystem checksum is the same; now, this example’s checksum would be 2858.

Start over, now compacting the amphipod’s hard drive using this new method instead. What is the resulting filesystem checksum?

class CustomFile {
  constructor(
    public id: number,
    public length: number,
    public startPos: number
  ) {}
}

function parseInputToFiles(input: string): CustomFile[] {
  const lengths = input.trim().split('').map(Number);
  const files: CustomFile[] = [];
  let currentPos = 0;
  let fileId = 0;

  for (let i = 0; i < lengths.length; i++) {
    if (i % 2 === 0) {
      files.push(new CustomFile(fileId, lengths[i], currentPos));
      fileId++;
    }
    currentPos += lengths[i];
  }
  return files;
}

function findFreeSpaces(blocks: string[]): Array<[number, number]> {
  const spaces: Array<[number, number]> = [];
  let start = -1;
  
  for (let i = 0; i < blocks.length; i++) {
    if (blocks[i] === '.' && start === -1) {
      start = i;
    } else if (blocks[i] !== '.' && start !== -1) {
      spaces.push([start, i - start]);
      start = -1;
    }
  }
  if (start !== -1) {
    spaces.push([start, blocks.length - start]);
  }
  return spaces;
}

function moveFiles(blocks: string[]): string[] {
  const result = [...blocks];
  let files: CustomFile[] = [];
  let currentFile: string | null = null;
  let start = -1;

  for (let i = 0; i < blocks.length; i++) {
    if (blocks[i] !== '.') {
      if (currentFile !== blocks[i]) {
        if (currentFile !== null) {
          files.push(new CustomFile(parseInt(currentFile), i - start, start));
        }
        currentFile = blocks[i];
        start = i;
      }
    } else if (currentFile !== null) {
      files.push(new CustomFile(parseInt(currentFile), i - start, start));
      currentFile = null;
    }
  }
  if (currentFile !== null) {
    files.push(new CustomFile(parseInt(currentFile), blocks.length - start, start));
  }

  files.sort((a, b) => b.id - a.id);

  for (const file of files) {
    const spaces = findFreeSpaces(result);
    const targetSpace = spaces.find(([_, length]) => length >= file.length);
    if (targetSpace && targetSpace[0] < file.startPos) {
      for (let i = 0; i < file.length; i++) {
        result[targetSpace[0] + i] = file.id.toString();
        result[file.startPos + i] = '.';
      }
    }
  }

  return result;
}

function calculateChecksum(blocks: string[]): number {
  return blocks.reduce((sum, block, pos) => {
    if (block === '.') return sum;
    return sum + (pos * parseInt(block));
  }, 0);
}

async function main() {
  const input = await Deno.readTextFile("input.txt");
  const lengths = input.trim().split('').map(Number);
  const blocks: string[] = [];

  let fileId = 0;  
  for (let i = 0; i < lengths.length; i++) {
    if (i % 2 === 0) {
      for (let j = 0; j < lengths[i]; j++) {
        blocks.push(fileId.toString());
      }
      fileId++;
    } else {
      for (let j = 0; j < lengths[i]; j++) {
        blocks.push('.');
      }
    }
  }

  const compactedBlocks = moveFiles(blocks);
  const checksum = calculateChecksum(compactedBlocks);
  console.log(`Filesystem checksum: ${checksum}`);
}

main().catch((err) => console.error(err));
Click to show the input
5470796627976884269274912312534478497740857229903867252027701560453168811177359825165712921772414633734975365834978818338483457568344530317648212052611174481374376817985956168867953397151223328148382513134467482659871211605373538899532380897266151644381033667237314871401718428886885599467741414150471427591019626182366535429822877899197382849644363073162561982715223999137232739499236059288630316212911017487494969778672830857392388635632365462513302831946826989288582984685888312489978354947441371388617389863430694466664018809723222630932540907975564140257481587488551563255912868187763868795276792068597830736238645198147327227716219443652628649199201438775573383618717721859779529292467632403913728280783018504874339618752191975768837977626020764664236592483462735715322994651272612086474390269796513338983775402270232286963133527274887431882610428881523272223533928764868195325748699274603328168493549563204177368960853759189844523052671143722955508668506847296078613380913888126582479718908223735849206361882460506422646732245828882774465386642586951255755277326828179843495376203128211868259385262954258358106746629269566135568297678463142678677559791211493345292219926558504334664615292043624189225531257848996957292897285584647871582597699413285782549520108569859332659059608355236267295228908499175580147374249316233323672891198630616248917129652528259867323858668515387464628961281783861271191916163512646438849629798347226998123896667530597751461822267198839542179746171549787755276131681358462112515665773139608576768633345745445886998746467070235096606769764047541748451665775010334691914642598257305655662032106636412087208262745127106172457546761853479290289461429873942938875962815113176898507362918963516074697229504968348649104489177132256553453596342744252235156746786035719649123585834522842829661524156360956451698785237883447353478412212690592874893055197960982710528272522767413789763557692851275245548945572945813120664790827837238256921633619582226834837838911853495591157880388012399637448348187425461368501481907336792342455377193384409726755073283989109060338826848299837555525873534212221843334762294462772582943512404617906569102774291936912784759315763420661073227757892117625660328521761278618813334810627846577843102945655267963989208849188817654957127357462828354770613258396688911744957963968339501296852336894598245530182819613791204059683780281158373753519127743113926910359927143072695795784763248663103470518766187850488526996818164041845335282361429961276964133922437930424656746072117450675611435436515136139793262885181759438494761292729230558435526683275533258714333913234479196156636712602148729870765072897148935492945831483397181353372720916382213854144120559418822639827686678858525850169334624128751678394883556063145290726131464843427812981718347524924217325572665128619323609229858730639936539222321879186936369134966312251017619055571653186211774770375348235721773883112242412015377950471676237078589173417348972070253693119958643847954253322122917414299541346877518060616556816353239730666340942099149633187371847379473482589638215281972453303765523549918056511388738326806287679615346231706292473199889455863246744592488533406748381597166112459078238936758851288610966066986773659546323233646297956699963728332478531292536135189461655484579257952848399393317813975318654022128911119433761578934185373178675070292263619266707069257653513857833229374277919675519435582789647099724012594810934515444429478378288185635647894646283130416575625514136476365514957137731164253052755636167363658522557336732027368473215576876098783248775982976841737621912350816674633359479985936256271799181273125654627530542278537722634752678198499699533295548114526484801845209018876922117648391944919147736521281635508772139698733112234270739016415251389766741351196735838770413412785925427719792838752094795545226942139917581544711945669372699589182896164355257766863692698092633433893156433061149875651369183951625724967339208992729223239549987738531812606017612658675422595796173087898039386426217328173628885645143731166866611984144655773747316882205462394958279996728975913294644028123492353829299493593498952690986616448654891190735564727388901166594851839743921458666859763632342756512827819771124964472731745934863146993872465465513416223247661267503067627850964633656224352340581898305472107198209913223334608377601958735534605380168849852474359474637123703137123412373117587571575459518179922921551695227656368373483435505616348426787977498917116591126275157186388483627042909053653682417892225018228777885995166569835928322426345462339357931477997734639797348444405795812664881243242163555369925083258846385651179581599953397886277840556961606646406736227315649213587386212226938863312354529820573746526625572637976914212222745653191076123846791312461811301997833699301255715354236027745191402729794790899012251697984090126669591467231049519676546411981521372735443362701143143791414148702792949638476313112332251279124469502263517448472184727774878739762577961117522415866817391098973083151895855650591063713793451458188573643960909254578068619565844216231926148061971887989492207921552863687393131919936755384730817140697297342042979256499526477287401054997133809498499740474918284080621528554012868987793522269125519465798816141671338433248284327927887026348432225383322873977831913015826443601582434752733016851011352629309997156780658711593379265267977968966113852368119448321941675752176470859122564752932447724726637290605266851541182521165428536145942348359894322590198465418568734998314387174395849720359892258320269443514399906140542521368863647120695222291535962139917330768968548681351525228621524662763370609815617051721896626228327795101719208287267479452486564476199026765693368023519070815164603186209938193235521272609936575534441761794455201138578047709074424035378575392370254167537097265322197041929180806738261259739326342821321666542525351572994385668817622242936049153368615348273932502475108667106155372197646619514852428423819174526372359087711410836715907878243710153534668831912013135326516435468057725397213657581090605999815834579224634695814386925012575351754860463274638018815054991184917952398094766193694087572491575393157762734210539760875079905226232619694498456216407185896178628336526382749930919884195292589242693660437370511031558474285187393632366258821126544798861227337745761668436981242627755014465268882044331354361095316466774493535521816566737945725287891614393855169395734426149577873246621896564097295161403145927681784448769991992068818764348789775634215887805983816019367432406668117012829370203854171390997697875433869814371887922464224132625377918392392770337178873286938697165113191525705829162693157495688315643825874412522552614044357815309583109354347541597785563331623127687336351368639853203693657311331840777680857550195722984390956032753772525514919894609872487977427319947463791931682776654559182525993668577628595289725026477323573521395521954684968893178217138080656150849664325015147520819389795386626482156259876611399923187642323799162871769742211868512870604589353252783810469289864298181491841150936135438665843122688796992566877563962243382245354782813266584470492686326188149847727162522113985154943225574147181521248665199015586569714626777045553952393018735471994498764316103014511282758329695738339613551579369377342773451032692376318341144472997727972524846191419984802266908854171115262969186334744257811669728427145174801661109872178794508532714124471595449061945266946452498675809837315924653290212128525143174517529062734152778392393914798078969469487048715091649620423663623043147794661555398829498750914235506596505732415798992724706096866968134629917680534457149118228425962837741973216675558352604774221146294527905860563289757248762245186766293683494589791277958522964159639512684793146865342743167561178270505330316580104316206223712044933740602389763634453957256669243018251376196036575946622445288415911686357738464244288035931313339869584024631619581013307285118226634686683067515115703651382535928133276859976852805695286292721225492070441878757497313098794323155396263799134133565879267269109115721163513591846727285811161022976637102732623281543929643713554853379380128235405792961581929041295856789973276040543581531047789243334161548669847265666771563642681139601268322394936987889521479422506847673956517789246634784764669226126719699862665250549993731460918667252228122367129024818392237930451145547293459746906695243234639255387538834866642197327614205814269463424364593045722070524346791149383268933253233055982757635624849598351567943857931759651591795724966180858563174813582561771736347658935329479614385068541394713188862336608773419461731079701961822013848788724947238586635548812422948114167625558717245160407653895735541122226448807024962829997749484198238644747880791358248357416016151414746325743534307677894993117674229092488995548958566916502296318065435227293966828238493641203731399361878574507917522928466657389247676917231393842086424385491675103753927223496284411574955415673233392635515955836112749816578599666233768614727671855862282464639349422750593166894719958971931943418372271373328921708789669574378574244041164464534398498868708895688124693859606362449590438937106035801022471965587330598982583720205184956714856018274936213214648677387211533628872572499620431645638846934085541781605341771883877144676462928671675062726362973032436685822468954043193645695462814188415097247699708121278018916139502153913769676617565732843852391666429726867054286233459659884148906185638791178597423691614264924189243844109628581381729894813312459662635683866333754393469483792181942870381323792325698366145788959782291372439665666693901456169085285689735173169923636098428538321464558228705437422591381464657744905054863413369434783890552959817993714744816367652642295383191389634579152899809255718413323812338164332250464857544928315112746265264011722676918218853217576877833356625013404539654413788131653417227267938015903284979786214470263659185359386534487860634862615949394398998288603849234577721952912947405745992564976593146685943248932332164383406260634023155342725089551517408497764757139366224363947796536916451098557071993680386066388179435610402088544236978752931321159395439363161993638899733357877579779376344728423274974234741684474065833245524794842726268650125483868550266513242027608982903321794431415113702768878943702192683888123377255213216054567063886448339571113975965611775841464240751329722517371013779996797961643240256824379684227954928865844046279340843126507218413182996487888687795062536659563965988269471116662465556290353856969887718062736685461886557768368929337129733386257377881893219478643175741723248687747797374168779725932286814344811135574422475383561826338416626214179660432083649810389187919894445968647561479765601956618516728383816833995228364110828918703195335859156840447834323160654198511596683939776132114194865884953655195953541238953317391497486392612766761866802383943660878272411864341417907339989863894965119526689767106960173641445829407549831758762751251958704836678368581768563183664066665486732260179520321666918623263982967590513323576091787097321725405578317989424466434057911537538666487542193576662545796736217456996387959656756746345523359023797081102843423112244769835534776060397714279536798092848694138028163138471350785096159884122092756049709113658815236123356034406075411177593842811541729817818585606474673964859848534962455248516145796799986175785415114020294390531691922883336454899951468032584693921451519910511081655225602747606057874943807691201227502729195466154848957284398552667113538379147533172193947053245537713978915477498534545998267174538747394783917750129679764341882061424234564557373152779551527824934242986744156450331635275477391945204424235419863696617065344495136371491568605682333632389680157413552441299234707110462113248491953236116946549227178336111187794221281249353729405354975158812292543017238853129391204422625518635615844382423677411780855573303944446359483784706387698090445758806715499060444177211867122639705718463738212030656687558862254345402079767494629325391676504260357128696910839241359884175886812688527394576537258287669161872045586250807322789696374717687233391780501145697579317595503243557980196424331277791946277914733778306290767888374767387573735850372589292226212790814297953538784034624234957715611960609167901488378819216372606932187993989252978228154778567080577135204994965046174653743793453541874957115264253034441964213733606610751628229497314748103612144545651511157573562469421324914376997371922831522948791935862295471919911212504367294414406312555817482690402667837758327264971683974910902512122440137111921884612592647648836147509137994437895928716674485211292557583773906588588681667696771025782399793835713326765067818447489734225460459385435769214161234319939420493584596284968147986761544043325639694082947694383170861153782292534850778941568371592654228345196614842553382466273235888823556327633018491127417697765219913754261497117845553919585466395413461611609765387195988540395044994443189599903467232186904813423566883071639069144712122693574322321949703712422559529463204533572976537216625985376423384617845535717942543928252877363367546346565747555557499335692151595766173076664269638231634197616949474812911324634686543578843530967583596950484811495541229483937936595645762644693421784516497164309524509896826620368650758020457367358569843187782919653273684642866737128984661460565928203220127520978335354394585182526039872478541721259715954826251114991580437677691454283325125060464511351098477629657523561436716143428512106139631676161864292124898596269910543551991117729154213736186357536129865352505854723962393885608628773982873441244063653657779461335724654811799128657532928092774852233120805641328428813625839975997147114918721589368674149943836957503932435041124317907615451038917895921931242497174926715341157957271945661499786346131083769835516523498065645671739610819328514559904315419212267664194573315863756031753180217818642976185315385720811296884311262354757836295789877362637697827273455615156424415727613998895565268879876518689981916127597092181781369950929257962489503665133628826195857642535253349537171321816239202764295496909972868186728537804436322551884511522319271846684474419557296899813055902163238385977656452246803021749615115249968821731077479272977198958617412861358482479952363827301718668312541578901171461441919982243379397821965122441658607639566424951751398746188613158199689537969378102863651590487556265871468510883934953314869740455991668053804735841128684563801868313135309767138622644218427698488673726815721167372814112634235322832750463071636745782551255467858642304788646862102650106860232955493858672781119729728355897522674770974865126771826454213223614296746920789270276323899230616652267011739841973594231456111581796975936394962611997856638542386833911753196784319145824059841145942568528855794254379326316332166434638237791778732812379087154430219059588037886492886531804727908918299335409587606874286098834938415678603486426872626293713855203795531211372828478354417939677646348083205285984161506470207247233722385428616120893469158426757924615178952838124536181618911453628124514960533667577983733826844254852045569071423961623698691936787199119792798856372463951788254696854963585142237262297799726562892115284138273925115436942297528519111779717394423655955167839340842951846465784251949021148043708750562550634460111478655495266674352431718432811719273282597345606127966447888011335295166674405361263678242488823564627486727271441039788854711826143216178747498285802170684387277440245697478671674317372781806030725866915384309088615330992654294538952298434933477580931719522446722561176262341691123975692669961983813383752229837446163148595918214551383586314276963098769221872766732440713739558165984838241526441529943566147041355916198620967649522184458441223177277696163268938339315471636127109738733793432451113338493082758645562093507127494647172630353342761327499445306435362214901864255714493354377024724877564322618748979150644178953630175667706598149851194842312824724871408589262821252442327760111311204747614249671764702391909928529920828144526576323819808348213518419253919251471455918029358328325522794356336499684981794857351824533941141153274613284752402155562884277872486977212157707432524011962115461663602373375253883254767734348231883473657013372813501335774398378756105065977288796195249950752655327711973760914328784423287669835322369077112890519295425138105768771999458555242093484252885393267898274066481954735485766263332119688592656587618426163384846890109877948164133748772177633819532212617595959426893626822572163557469932319893504435768823215740511958412290197114849543698354303336428540423829536894546642123751707917707677314770246233491122887753932389597567692178875196254359761088165315122942809028592710759531362954586441513037824063338341225391657922812228288827295726505062837373816599381028179513745628499477562936714525637539611399867289372465417673296635929323517387166827365559136139314058416317812523981122533219507554514949547337319049935149667185607379599321543394453976592949812987594097137031798316586969276762214211877185941132603356288332316047897182743923426618245383426939778728475357212480382435433729736484778074882898651233835726161311794322844727643715258649815671547343355720833589478312175352497131142749102671654843636689101788629346849755773316754350694253244276199956687274619165212088945572489140538875852569267772179140856157761159143066106979619291945025754062301336572849255191393089756130274061227583271580675426486682548051454317976847132315757416195677167769466118832437511445448647436464312949165083715051454247439165776120988332448148363572258314745145637374906273266373346383989427194672316531295553974581457288992337712532258951942465472873811246488188843823435169722676691393573735259490172652162648874398224730276987664070363291786757718965885987456913894871547510257364325756147061617644165183181766287956143062777490479931878455622714704579128340325384185684108474834425552339293519959262791784232557851320209970771072453035872015227451105489342615483064315827791593361860779081296380514348392985341821804659373397493156225155695998315341527459484256243881661869833975342033774313452190106153638611231988972748346980788477427951927551882632553030989226346739179450832195474340701651145731801766811330666662462083824753563698587734504276157877534650216117123991939588726734647511355866952625405265602441202951978227599736619273188290284388632767497277906489886948637017804879784692336148891275983792175932997855816841267969429356272621783649455199336381658129924685266259215177136648738023864882968883236877364635231220963992663158872757369727724655266112196522671917937277859722757525354889913854455128604567767197778387158181623291439843819980989359312066407575536930843617503210903912426777102748488197915033347363117855426018206176937093876699743433551276472173291635912428657457835450615722536047905554733288279529616616681475405230639853406532678165605540321769485137581472924450722113593574601734781747145935904170628781825250689589946923201074967783932941372579723058232197472613474461281779519097925692787942324149867494398287946077787665347855684247298938977051525357588994364452744424128743433033678282122399604043151991183679684850422379904231958298527539726449276575234056387151772765381711919923592754386218308147851056474274393015642456503312292299184432124866591218278491526733679076778872423625233254497146721618276022905838253637142053262966707741917298494427344830643160939117949730121791222139458865507855446942879876356568775639867688157164942135149683348498141257729476937543169169985974375816424318679969178829682776224940507875977833624283701290606527751538566795174457752069202396534023627188672871166543441838971643672294433218902821367850565751268214328090106514978876151457775369619955394311389079687366394138959340129296383959522830932922724371425461433