From db8befc81fbb50ff67bd268db5bb661953f12a4b Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Fri, 5 Dec 2025 07:05:13 +0000 Subject: [PATCH] day 5 --- day05/input | 1182 ++++++++++++++++++++++++++++++++++++++++++++++++++ day05/sample | 11 + day05/sol.py | 57 +++ 3 files changed, 1250 insertions(+) create mode 100644 day05/input create mode 100644 day05/sample create mode 100644 day05/sol.py diff --git a/day05/input b/day05/input new file mode 100644 index 0000000..6e68709 --- /dev/null +++ b/day05/input @@ -0,0 +1,1182 @@ +293759114674909-294160161397913 +14118421020776-14930744118859 +212804101443789-218058035077685 +50854767297312-56411315641900 +263280706509777-265737025858052 +189685609890784-190192477294478 +188279085433277-189034967272023 +162607730698043-162607730698043 +300167827559797-300727517063443 +184915268785446-185017856482826 +383228600683302-387924839012363 +182129955449970-182374969732510 +90825530041695-96657677707484 +252948646840636-256726912576032 +296126217332660-296492183593019 +15113027119766-15788058659949 +317165330346483-319756785199474 +453875053055321-456016262408936 +1588359778224-1588359778224 +181522312061574-181792063478453 +516410761892282-522082097435568 +436983958939555-439748083024520 +334419699778862-339504477848945 +526093075174129-526901177450651 +15113027119766-15788058659949 +287156030432969-288910312232076 +528334907392464-528798980588538 +84347805502177-89233281153734 +187075368471782-187350410204881 +106386518140218-107538347738158 +495038028072904-497048807173181 +18308543486000-19063936849347 +312120793963018-317165330346481 +172034451158954-177902709394922 +162607730698044-168270486393478 +73515279482252-79444537000532 +41738954880549-49586059980007 +460171791414850-461982994249845 +1588359778224-9459487872680 +530747292433667-531082378231243 +50854767297312-59476027625316 +393447796688222-394730449008337 +103565607092779-104726895944442 +184018991298523-184248683371571 +378761634062214-378761634062214 +291941556223230-292423931469140 +484779107323165-488247664445995 +271965070601709-278955876456877 +452690040336858-454465178858441 +43245307834664-46019742026916 +12192026095067-12697120065197 +294754922981671-294837704098837 +475425166553581-479405117687162 +184248683371571-185017856482826 +293177473607841-293759114674909 +347911763367090-347911763367090 +13665677940873-14510402670977 +557321388585594-562124674730076 +152643352040446-160414795110922 +405724043573124-405724043573124 +395366451082717-396976456909177 +455284926705277-457302426796893 +524085893015885-524338726977985 +298228384916056-299041711087702 +399400922161252-400841717561840 +96657677707485-97977908663839 +531312334442228-531664153318532 +400254767362049-401797661086579 +299041711087702-299670438379451 +445010952788549-449481444380244 +541476938686352-541476938686352 +385726945887023-391226017441436 +186312435671497-187075368471782 +524828099845482-525459292712313 +364735716581903-371082928073708 +131783390820128-137288090698942 +548476342774845-551854597908597 +182809267410179-183245453418564 +528616239376180-528798980588538 +495038028072904-501174446363513 +73515279482252-79444537000532 +261598364303901-264399428601722 +183561727961215-183776463482965 +181408548710764-182129955449970 +145447723285709-150191998900783 +137288090698942-137288090698942 +398396130377440-399708460199615 +33541618650319-39315748549400 +299041711087702-299670438379451 +293177473607841-293334605216320 +104229415027763-105659252838080 +526901177450651-527105920879115 +10588810106828-10932938853759 +184563649203045-185017856482826 +282632020713215-287156030432968 +422637303023819-427909023660406 +223473021642281-223473021642281 +123021619329474-123021619329474 +223473021642281-229122155773085 +415430548176613-417849682615697 +516410761892282-522082097435568 +186562526415499-187350410204881 +202818421875168-207402174964858 +185600546964874-186312435671497 +375356583990252-378761634062214 +188552986145652-189373672577354 +300524128878436-300920798200495 +61723695330659-66934453033837 +267123379236393-269249556304596 +299670438379451-300524128878436 +445010952788549-445010952788549 +302593067897152-310496177367161 +294754922981671-295236654500346 +218058035077686-218058035077686 +296735095605171-297012203005960 +343050441747877-347911763367089 +479405117687162-479405117687162 +10149932412318-10588810106828 +394480286195125-395850414302244 +294009221321396-294340832333267 +435215601268184-436983958939554 +464547986954364-470637736546566 +334419699778861-334419699778861 +175554187342038-179270502629476 +527965210006873-528145779370247 +123021619329475-129230936844385 +294837704098837-295767616891645 +101592424674492-103018549421745 +426361402094889-431440354582071 +100714281030322-102117261418225 +207402174964859-208211961202753 +102457008397682-103780492898948 +186078194098514-186312435671497 +238359444554329-238359444554329 +356405949729591-357737354363171 +235015156808104-238359444554329 +252948646840636-260549412380522 +466106529871705-468310725178504 +397453020905445-398831051728302 +109016002316995-110195134063754 +529442999570838-529932783536100 +193043627044590-200204448735378 +324523729958481-330646265501421 +415430548176613-417849682615697 +142111478873018-145447723285707 +264853103803001-267985882847542 +524085893015885-524611698002829 +405724043573124-410787169998427 +181522312061574-182129955449970 +155006964181989-156118694522562 +112584652043027-117876129858968 +354013653195002-361320852308742 +504430399131984-510779333924290 +457681593075051-459609397537345 +39315748549401-39315748549401 +185600546964874-185810119923803 +23320912501001-26172096646748 +105407774407677-106729431722210 +302593067897152-306616636635475 +12842813391779-13285892056583 +186078194098514-186885175304232 +17171967083685-17506712345380 +543455463541816-548476342774843 +392439165379516-393795501064246 +366171479259870-371082928073708 +533541452190647-541476938686351 +193043627044590-200204448735378 +529442999570838-529932783536100 +107190586650780-108601673982848 +63478381557019-68201587704626 +242194001614782-250127963677070 +115592795688862-117876129858968 +83355673195332-84347805502175 +108160573837042-109545425345178 +458757635995980-460858048046654 +268814741295220-270884175562991 +396159396078746-397702385578543 +557321388585593-557321388585593 +456561659226023-458493007240010 +506967373408071-510779333924290 +247261438155904-250127963677070 + +292348212959867 +211866833001227 +518048347643485 +370185418389737 +127553873515726 +382338979947465 +109442100970605 +468527573994343 +248305304423060 +325288821344100 +537753303181089 +225051274802366 +134312639140414 +331293263652886 +354706158214777 +485321218747435 +2715747365358 +21598284193483 +369800476428327 +245283620340653 +365954784124270 +237186665926783 +175403200856367 +436648712687443 +388255076442741 +108545102139828 +309478805097149 +365675842918113 +504946041032938 +207713907707547 +541350661903598 +385938176702520 +331484367938496 +404989248763182 +197538204937447 +312723799175073 +15558536341907 +389819596524997 +29121218285396 +43815369441104 +127322471520907 +470456007138469 +389727296248080 +204436641090434 +89966885887884 +529232857127790 +560375821417470 +53025419297287 +145915036603255 +395316213044891 +454446480438710 +302825682036196 +448394746273904 +244437428962401 +505479784437969 +380239366155828 +191316264499519 +350383990383032 +370935795237206 +143246407427157 +336698519025305 +435983813461910 +374459079583544 +28059973396572 +562945561237465 +375778171752044 +247151108987707 +57121149585145 +67769883883776 +316800376922835 +423761262713753 +418798954789441 +168108899384133 +454433067791986 +46266206699194 +45522951849044 +10451892409805 +51873559741003 +278404272376204 +454997802893657 +116652802781254 +197886773072879 +557733443811495 +237445070931978 +496426888632174 +328709265366987 +177660121325822 +152727083702428 +135257903299358 +425633924895487 +207975638504485 +153146518177794 +334287736065077 +457079068895103 +487966161855848 +220024312014085 +344726889731300 +431085530284166 +51960174649550 +470257499499373 +237196110126185 +210935332491013 +14344386590104 +339437038949532 +145376229414435 +106914118553571 +203182920403875 +304825344079921 +337240949729647 +68493406575765 +365944508157876 +445380439935497 +259724596827826 +437397997537488 +501027502687375 +102844375136503 +253448236139578 +126011078125903 +203170745943766 +325758155147879 +378764693490525 +249192923830456 +221556902691202 +213690661473126 +160173506069276 +327049649287881 +229024174949274 +175348605427755 +319132850523492 +333096886821416 +485648683457650 +511618629451253 +315477899080452 +2222186982966 +497617087233496 +347827092544351 +255052284046970 +263121831674651 +3801967276421 +258411289234708 +547341633772511 +133619394758926 +512390516913983 +123961743048784 +264340333407220 +272235062994599 +376271994177759 +227568842526844 +509411891822737 +508450183745134 +544699492672495 +306153273543308 +411608927569108 +387875854538333 +362735971716752 +280864215534174 +458520238001774 +475475231745016 +396877728009648 +14534923300072 +167489518903388 +442560450606673 +61796770629569 +67505070964360 +223922817916432 +525581141084974 +226018622813296 +198361127750098 +144274868985806 +246800062043091 +438968298559494 +27962296290213 +453933783867753 +246452433702557 +391553047909968 +195484671744971 +423086397114599 +243677892840927 +107140611802377 +497981569989877 +76532816948092 +467741096761101 +39301053013871 +326744728390129 +251557650620160 +1376983837755 +329894164540738 +125405515684845 +247293581966939 +44090195522888 +470186857906672 +178001946026175 +198511291950393 +179221752836338 +176502565931820 +248551767465498 +316770848590779 +113912761595639 +546384264170975 +191528297215534 +38908000697891 +520007586790517 +534338986138130 +400179007450967 +498851057103868 +43604776920136 +548007273379317 +246798745424840 +365069849709309 +524114183579543 +189685684343124 +427396095495176 +383813804241142 +205572797001671 +534177098463912 +172762753248307 +10186332065452 +358452429681417 +507997689593144 +535718922648624 +31912466889224 +113830842111543 +250546764630851 +206181686836751 +107086959673760 +336758132146382 +200805572419434 +287716917861062 +424874768823066 +35028724053727 +495890896756862 +535421052370730 +176961366691672 +291158700421765 +31274529725978 +106361532328725 +156416350565892 +462918948616311 +476030012927689 +75029139359735 +376405144113799 +130133187202369 +156263128715488 +425866279121328 +154116116272582 +196646220922608 +51401822233164 +453526445940573 +90879198704775 +386694350276907 +365788801405797 +389657922559981 +394782430871383 +347009210485040 +28200712894641 +154771115004811 +214267292660843 +28119739994076 +173924932421123 +17172842103239 +159075957163549 +88660567367585 +508664037781619 +417452168320742 +213453900971864 +469294911047976 +42031409439728 +51435301665055 +101017450888661 +331276947041476 +438456041645456 +524867781963842 +60789866601449 +516449929354225 +533798169524679 +345281653562749 +461473002347931 +544938903443288 +148165463551493 +343460388508261 +5696855665706 +242494734731512 +429992577969911 +470933409153135 +282119615467013 +386488180275507 +75081256163178 +242408081646422 +474377942425104 +445558481995853 +80792640834510 +233355729689680 +419468167893963 +454087828491065 +495549066486349 +338719762703225 +38880485366091 +146394517810430 +335831217019250 +83365432611301 +366091916812997 +25764497321756 +18704872140729 +289982761358761 +74038902077027 +482126338123832 +375384929990006 +311075608967557 +260650977568424 +109376906906052 +255230204774417 +310754042933616 +446513457172856 +307303441260902 +47932419707373 +3891516430998 +270194464255922 +529581316449593 +33996362314133 +425299168263363 +549249720700943 +528552970213014 +243236178449783 +500364718396404 +532770450620952 +184430499982703 +531022437300061 +176693651040497 +195185169690329 +10367982650506 +61830085319874 +549402105568052 +560699730135052 +172427699557804 +25887643979250 +12582741460595 +283122014030621 +370763445067255 +182552289132010 +561407072772130 +156920557425643 +183891073839231 +561309441836460 +499598020406847 +419435362187075 +393288328145151 +42336225139216 +308429192249968 +250055122233707 +273987946605341 +248492202656162 +287673668768139 +377177008209318 +205184117377567 +458735066317998 +13077827596418 +244127978533830 +300369740732996 +394525796744311 +427009844453263 +88598164651207 +172851237601402 +90429465434422 +473536478636530 +266749073555428 +229414451109601 +257918570402304 +549830228154115 +141843441146988 +407391556298990 +55621892179608 +105673530063763 +280762361833438 +558524552447570 +53835368047907 +164895221221410 +52253384181126 +272575774692363 +267052228168652 +466725539246433 +317219731487910 +446272210327122 +21082354865719 +324386478503190 +519569461942776 +47504393392215 +295330469590488 +559202962015408 +309911680433470 +529792797760951 +56334602671350 +383385712390382 +33292204318553 +228365854569831 +135387937883309 +523140629102873 +183153557299117 +176018669012765 +106972775617671 +326839985009796 +500417600382770 +541260359475853 +252394403312969 +87344003908630 +27723568048577 +148609004303461 +178399471355394 +337427983544729 +305401267071079 +230643886417103 +539621734687358 +20939796879999 +203239520522042 +41873521912014 +264322690123270 +427863581405024 +253477518819980 +261356654448715 +212435406372750 +157428702605875 +9528659725006 +18766194307116 +372050915450275 +217535149285258 +25089310996332 +134242640076740 +321847792113999 +515305070489437 +121161486618098 +443182671448846 +91521024799527 +104792045882966 +285986002637743 +445446661671481 +428961992280439 +267384641216329 +277128710786491 +144556368535421 +193091137305642 +386240001053031 +113914763896276 +486698250812930 +193156000892745 +52787628419648 +6273793645588 +64485117909792 +54200296689015 +357334371776962 +545208018169975 +539450165342747 +331942316796668 +274741947457442 +419801743901863 +440659356847496 +10451644146667 +185164414554649 +462205814902944 +387531945873656 +399429221597834 +225945270949972 +55283029182487 +166636007118532 +334631526166390 +88822879114722 +455998486584969 +287615861028192 +385039028958877 +147508475329187 +197177794055856 +143358176775206 +562172301328788 +67962526761363 +464309316254209 +226585964585302 +261920926771008 +450701760600073 +436393448020520 +100860167429787 +59022834386115 +196314952199177 +62712552464826 +496158685473493 +190039788617975 +415984770298656 +277539282885663 +115521541516954 +332395457381390 +175284200114216 +275255665020432 +317174275648288 +397056194436680 +113694539464347 +42456725585483 +329233073287114 +198499868061551 +344093498873663 +173840567428412 +363392875992461 +207691377795757 +334041187418937 +528471291177057 +549421435409974 +561361176535476 +534252987449797 +560181330626392 +113446470916340 +364156828823473 +221398073910585 +133648240322596 +400637497977531 +187327351310484 +157846472927127 +357764964721716 +478170585020379 +504975075085498 +216456270320985 +268873793294799 +8520212327438 +229373392412106 +117694781097464 +282174702134389 +48189545675586 +356339400340721 +560096328542779 +268878996774170 +501910690500058 +53870199639323 +326898868378737 +394819986054768 +509974227378960 +423008523856722 +87897461653450 +411795955970843 +397924184278590 +269504090109694 +52100091898208 +58190881463599 +4951173443392 +536073833097295 +538577147278607 +248297461022991 +68365681907362 +346897290702765 +270590313308545 +507691569500401 +329321433666737 +356651551969910 +36189733228994 +165078221606962 +377525120249367 +157889884918397 +268054239291005 +507245304681168 +149929834187811 +446993690809636 +312507357874905 +368825146302992 +409417862732681 +32072507408281 +510291701985419 +386611901597174 +88329582048847 +87602534546127 +307143559305269 +318881803209660 +189320336409528 +243097140006749 +467481490309630 +357313520371000 +132287839351167 +190054460580204 +447465648534579 +533029910620049 +280289354368647 +411269228653476 +446310885013626 +456149338879863 +54940237216695 +258721189469904 +537699150189555 +104091872111061 +109262294979604 +44849571939987 +7476186086038 +115926500924771 +115466559274068 +14142441965788 +303334466294807 +126234350677163 +237634888384909 +203084395756588 +416679418872522 +55066675030603 +336402494710581 +194313260919928 +47018221112936 +296894998596652 +122091249624803 +132716028123446 +265570885926913 +97757832803082 +87761382218097 +196953986943416 +97460108198371 +368679925587128 +291786169080709 +361147502276578 +487889945672339 +539825679394728 +369811770045459 +3674060736938 +44367844818993 +83347112735055 +292360423693781 +391682808243069 +561565663204896 +337626251497198 +452863160559875 +310045093031290 +220372960682812 +276498749628479 +199889927526532 +34039501515066 +487355275099701 +24834654991378 +548171738486430 +482501276328938 +365508584163798 +160141963457734 +309381440143819 +67548102028230 +305714692787322 +260792935140193 +312914263132185 +430220556403869 +75714856758030 +125790549512296 +103259195077046 +136973808805137 +343839721029044 +119338459409611 +391984204498955 +257007988967655 +163260980432010 +362843788666247 +560961777344273 +165240547514882 +62023534566635 +501023076872885 +400193703588017 +357903778919965 +544772272822966 +61233159901868 +341469032762198 +263909942022025 +225307104259207 +423325757593096 +394276069524262 +175152958726589 +294860206884795 +381023273756574 +540795736757132 +79011508233352 +248037637916955 +333510082683449 +44313864071248 +49617273821756 +103469387796782 +386550304864114 +457816939703418 +261739111959743 +182039053218404 +177893506870327 +34652233545025 +437276607924206 +407980288983546 +408529188144860 +197827029468131 +54641068612777 +273658339178424 +305507571576037 +74343560391789 +472586541026518 +59178972052721 +158748733990263 +28841058583694 +543466482796593 +10637164515020 +477334983346379 +521420626457405 +94960463355062 +268229523557262 +287482402060277 +549460390549557 +167601374300750 +235767055665994 +399187297481270 +318945770804688 +273556206764670 +143784845034764 +549796231114714 +16007954454102 +386388551267192 +449085127255355 +46304075109025 +231077363133396 +357809201094011 +204074891991698 +455081063207002 +201621798135210 +127780406829005 +13426353125876 +560942828301898 +549474222892483 +24326625564534 +446551951480493 +105086273425113 +178824706848369 +487090090386941 +479126355904686 +165185309896970 +198603620968289 +498824847595232 +79719915478747 +141760989407273 +75415733110624 +419016062586929 +506117649708613 +364094179938252 +76165264425404 +147461978425669 +344119541002021 +436402298104792 +306317479072904 +159425070673137 +14623894687578 +503230211694031 +321461561149591 +236697533870318 +440663757034283 +527801405157547 +86467767379262 +265317402114931 +374814276287662 +404316149955274 +180786812372665 +548214790890649 +516317280508292 +362701970248439 +32582941711129 +385012430284208 +194797356931327 +506007027999602 +115305272719269 +431435477245645 +401792544622729 +373168080390029 +335272812960482 +521992905917082 +338260415211278 +522047459459508 +286704476627243 +274404323969468 +237157219568754 +198701250157395 +217801429433186 +498080100240239 +76455009640171 +244563453771087 +272227624175769 +548708420039481 +318661155906266 +335506356124860 +384558466199093 +334705444522703 +78468958918409 +303454231724065 +409932522527785 +156403200666084 +410224377882521 +309693283075207 +478377755458008 +399836085748966 +400291100460674 +186351276291577 +391127842224260 +409993740841223 +75294491430507 +214129580849915 +45377302186727 +299907239666816 +515719522456983 +175531718784543 +47923069737777 +326991110898532 +309364432243680 +324740194016408 +124773925105055 +287292453031704 +267439900346942 +443301065907791 +249631922653271 +24125580174966 +335915344419607 +354224720666229 +406333111796270 +415000245007409 +268036395274779 +227463239681935 +263846402873817 +447904893201118 +44322876284881 +143047334760071 +186375147879466 +302298584269638 +31469150148718 +135034639374080 +397551398899013 +225275634766021 +60450084253541 +506025127588396 +80486609706100 +370290475636106 +58102726491654 +88314374948709 +25653434827248 +510148560300757 +497536343764740 +375586618875561 +358593662130550 +54348117982896 +334017896461843 +12447104026995 +429743732211623 +95733005930858 +265386607325089 +34463377239519 +208202042225972 +266579119336437 +186126208169113 +175853636041631 +442804976578138 +163136496483638 +125398235933663 +359216440293250 +247240373959327 +378449267720397 +308105102477295 +435174195268523 +261248296578748 +52782942551736 +383815454439318 +132988732017537 +148285743874297 +366663583917060 +213192784497428 +256838818157455 +41811150424157 +458049420392458 +42868786903085 +174791363422886 +179943533900237 +520876235889795 +186336416868342 +75541588727613 +97651207251311 +174943390740213 +461224712167617 +331110430818425 +285292980138971 +117580952594931 +123899388252389 +113666530471234 +382904708857589 +335582546501901 +545131229724184 +536331780256440 +95431522274151 +75522633866100 +396883063035429 +92642442678252 +274660122066969 +367381477596181 +549238949069900 +86698759331838 +226847745518731 +268476874669852 +131313335930262 +484164312372638 +144971574935859 +309654105085763 +499353341938865 +425110070133621 +442252625387155 +436930917300786 +160834755237081 +157905721116051 +548698039215017 +225250959822890 +93065150142100 +84637758287942 +282073623197989 +95169175978930 +306187539619969 +253780068056885 +470460763838933 +282128885795588 +381962092418825 +495215062908940 +33576707664300 +385391381995928 +36787093940989 +265161790125679 +246381496946047 +545434969174488 +247992151640700 +497851163256742 +321667388033467 +243641778317137 +391773552703945 +473272522669274 +109914245032956 +556469685062476 +466494056849848 +125302321845776 +172433579964491 +103405055780403 +378263573498281 +526851271554975 +428884407853327 +282760062935520 +446539766115500 +212972881275036 +163183481481729 +87887861544592 +283296730147376 +387744133095855 +537591620935458 +236153347629652 +526607090637832 +73940113830415 +277241633915456 +264868038188056 +81084483510432 +199539607696796 +67067930347296 +218471326795757 +445108600335434 +127991357526826 +181969739301987 +475543061976003 +144555867200498 +404753717481168 +267723419404685 +358687795082523 +505035145816546 +257446349207205 +266720290120806 +156426575931843 +354673947405921 +87437032301247 +47797306694241 +308712232502697 +178391587117475 +261337192437841 +466602172891334 +144664669774635 +91464377448009 +175227287494808 +109111932909221 +48328244079804 +261582078489825 +423879282470243 +344098315163091 +544031769499824 +273864393256645 +561849221950759 +559454024920307 +174016398550127 +467108306448750 +284776693042343 +3028280078870 +447276297158662 +466351065305572 +406672422038806 +397462627469921 +246491726299090 +394627127211226 +368605603950785 +327472943114158 +548641786869985 +544782684238937 +327818171504263 +520242930837901 +281754457288321 +343564175002392 +310456900346766 +142356584651725 +348513231251705 +242500121161169 diff --git a/day05/sample b/day05/sample new file mode 100644 index 0000000..2e9078d --- /dev/null +++ b/day05/sample @@ -0,0 +1,11 @@ +3-5 +10-14 +16-20 +12-18 + +1 +5 +8 +11 +17 +32 diff --git a/day05/sol.py b/day05/sol.py new file mode 100644 index 0000000..3d54e49 --- /dev/null +++ b/day05/sol.py @@ -0,0 +1,57 @@ +def parse(input): + ranges = [] + ids = [] + with open(input) as f: + for line in f: + line = line.strip() + if line == "": + break + lo, _, hi = line.partition("-") + ranges.append((int(lo), int(hi))) + for line in f: + ids.append(int(line.strip())) + return ranges, ids + +def solve(input): + # Part 1: count the number of given ids which are within one of the ranges + ranges, ids = parse(input) + t = 0 + for x in ids: + for lo,hi in ranges: + if lo <= x <= hi: + t += 1 + break + print(t) + + + # Part 2: count the number of possible ids which are within one of the ranges -- + # in other words, the size of the union of all the ranges. + # + # We start by flattening the ranges so that they don't overlap with each other + flattened = [] + queue = list(ranges) + while queue: + lo, hi = queue.pop() + found = False + for i,(x,y) in enumerate(flattened): + if not (hi < x or y < lo): + found = True + break + if found: + #print("collapsing ({:,} {:,}) and ({:,} {:,}) -> ({:,} {:,})".format(x,y,lo,hi,min(x,lo),max(y,hi))) + # if x <= lo <= hi < y then the range is unchanged and we don't need to update flattened[i]. + # otherwise we need to add the new range to the queue to see if it can be merged with any other entries. + if lo < x or hi > y: + del flattened[i] + queue.append((min(x,lo), max(y,hi))) + else: + flattened.append((lo,hi)) + + # Then we sum up their sizes. Ranges are inclusive, so +1. + t = 0 + for x,y in flattened: + t += y-x + 1 + print(t) + +solve("sample") +solve("input")