diff --git a/mark/0_dayn/example.txt b/mark/0_dayn/example.txt new file mode 100644 index 0000000..e69de29 diff --git a/mark/0_dayn/input.txt b/mark/0_dayn/input.txt new file mode 100644 index 0000000..e69de29 diff --git a/mark/0_dayn/part1.rb b/mark/0_dayn/part1.rb new file mode 100644 index 0000000..ff3a55e --- /dev/null +++ b/mark/0_dayn/part1.rb @@ -0,0 +1,61 @@ +require 'debug' + +# Generic FileHandler +class FileHandler + def initialize(file_path) + @file_path = file_path + end + + # Usage: + # FileHandler.new('/path_to_file').read_lines do |line| + # # process line + # end + def read_lines + File.foreach(@file_path, chomp: true).map do |line| + yield(line) + end + end + + # Usage: + # file_content = FileHandler.new('/path_to_file').read_file( + def read_file + File.read(@file_path) + end +end + +class PuzzleSolver + def initialize(file_handler, debug: false) + @handler = file_handler + @debug = debug + end + + def debug(message) + puts message if @debug + end + + def print_debug(message) + print message if @debug + end + + def solve + raise NotImplementedError, 'Please implement this method in subclasses.' + end +end + +# TODO: Puzzle-specific + + + +if ARGV[0].nil? || ARGV[0].empty? + puts "Usage: ruby #{__FILE__} [debug]" + exit 1 +end +file_path = ARGV[0] +debug = (ARGV[1] == "debug") +file_handler = FileHandler.new(file_path) + +## Puzzle-specific +cls = PuzzleSolver +## + +puts "The answer is: #{cls.new(file_handler, debug:).solve}" diff --git a/mark/day5/example.txt b/mark/day5/example.txt new file mode 100644 index 0000000..ca4fb6b --- /dev/null +++ b/mark/day5/example.txt @@ -0,0 +1,11 @@ +3-5 +10-14 +16-20 +12-18 + +1 +5 +8 +11 +17 +32 \ No newline at end of file diff --git a/mark/day5/input.txt b/mark/day5/input.txt new file mode 100644 index 0000000..dcbdb0f --- /dev/null +++ b/mark/day5/input.txt @@ -0,0 +1,1175 @@ +123733999511819-129097742451553 +72457259933919-73006486209179 +449751833329432-450568741457015 +448947300617550-449751833329432 +316890832692667-318375609475339 +131019804696732-133504194291127 +395398847461944-396102934283973 +468376118242991-471943110871380 +333238489644882-340784194571579 +145963688800831-146145871957057 +537960941273275-540259922750046 +426202095635127-431349654040376 +312828450083787-314305496993884 +543936086278284-548968886939809 +78116148736612-78776205767469 +375440416264834-379737809430310 +523959872606445-523959872606445 +202177936141278-203498793678689 +537077393994144-538544747255375 +144587556727895-144859937290891 +484305130559820-488966224558090 +398364695750914-399128875480997 +318072307317644-319255683053141 +168891995188741-168891995188741 +163090520676375-168891995188741 +365562695731946-368393312968878 +70971915434748-71824285847299 +254114180397478-254114180397478 +148990027966738-149108492715891 +514820395926815-520494825773196 +22756767160351-22756767160351 +225901307405140-228251163257320 +398769071207357-399411191135966 +22756767160352-27807037307628 +50772368198525-58822218726806 +272764005357263-279452204102319 +540434864117418-542603411850875 +213865942274473-213865942274473 +535514631750141-537631240137378 +114540090145831-118145014645056 +97402799394292-97402799394292 +191485186966715-199069720799337 +533237666288192-534793402534845 +179089627875761-179089627875761 +201454906030829-202747562281209 +437971290047841-440939365462702 +152477144442229-158303798019034 +103993765352676-103993765352676 +426202095635127-431349654040376 +400509640504551-400956320724574 +450285967857330-450713973644024 +348493430575081-348493430575081 +209547485503448-210880045174633 +450713973644024-450915605345301 +149597383337345-150487596666673 +205702087897137-207260881959745 +206639283022764-208040019436720 +400509640504551-400956320724574 +42837681618333-45038259219829 +122034590777905-127236687887286 +395258762373838-396026141420454 +42837681618333-49157815364864 +326611040738619-330481152508039 +141592085809887-142341356942707 +444616482618339-445175130948821 +202935099007068-204441264065218 +146699153642080-147158619682012 +434255273547528-440939365462702 +213865942274474-218664945453134 +402654169333020-406434569103853 +231949155646869-237086199261782 +383103533229844-389988436987826 +145206819977168-145963688800831 +142787599347688-143557171206626 +324437266434316-326611040738617 +553825067470460-562009316501320 +15768900832850-15768900832850 +254114180397478-258010591291039 +486346732468311-491257246683015 +78776205767469-79258861946720 +207608866702410-209000903596796 +194773819445986-199069720799337 +64140594340860-68554026650285 +112431945208907-118145014645056 +392930358950372-393469543412806 +77708018530136-78303007481531 +72794065855459-73331859754342 +75812866444581-76582297163100 +141082935241657-141400174620601 +53899982970514-56162558416759 +494962663361751-499924452897506 +152477144442229-158303798019034 +394764025279638-395398847461944 +396513669711427-397030357185118 +344374444362491-348493430575080 +375440416264834-375440416264834 +305497956964067-310819747532438 +247282776586758-249433370001354 +148182555999792-148712833562777 +304586138639328-310819747532438 +231949155646869-240361286655633 +517247388598561-519391364691403 +311872285027385-313439342279269 +1498220551639-6952719563388 +314024115559271-315256926607111 +455090421238550-458362334280272 +185291270397823-190310581333748 +32607483985306-38795978084301 +205096452733529-206458590524066 +134127793532006-136868645700672 +147397374804130-147641905030945 +447751758628882-448426894178436 +447751758628882-447890076574460 +76066049817733-76582297163100 +474763431631341-481635540296155 +356116129221008-359636761675883 +181701409335748-185291270397821 +392266187429988-392930358950372 +73930963939517-74785342454198 +319028042503038-320448279038767 +146145871957057-146361933776836 +352960724896393-358343148565632 +143205059394258-143557171206626 +132956966890879-135585887923730 +103993765352676-109602964033165 +315828331743366-317253286294513 +494962663361751-498291628175739 +523959872606445-531486218692760 +140817009132136-141400174620601 +241729215349632-247282776586757 +314991460694776-316335025854229 +464706095601557-468376118242989 +208679684033567-210052810547714 +143205059394258-143361965215351 +415377694783979-420328423222987 +449460995906643-449751833329432 +171680444344246-179089627875760 +137611104805792-140279553528871 +73006486209179-73331859754342 +363766299808742-370194869964745 +148990027966738-149597383337345 +319986026835966-321422271206633 +203991523121897-205546834849258 +292529454752280-298458727505487 +406434569103854-411448632603079 +146699153642080-147158619682012 +283808279464425-289958181152715 +223025997361587-230324469502334 +333238489644882-337647340379613 +397170692407151-397684418670106 +383103533229843-383103533229843 +508695025798639-511012266908114 +148990027966738-149427147902759 +14038091919253-15768900832849 +283808279464425-289958181152715 +82180997164891-85040800874679 +539193029794061-541036206954435 +534397022700522-536150413315905 +135979964732750-138450706404735 +503901145593421-508695025798638 +395398847461944-396102934283973 +447890076574460-448426894178436 +445960671807395-446434795466429 +481635540296156-481635540296156 +298458727505487-298458727505487 +93779765965657-97402799394292 +85040800874681-87622909142158 +265089854453580-269240241118013 +548968886939809-548968886939809 +1498220551639-6952719563388 +79258861946720-79450046880469 +262400291271055-268503795588810 +553825067470459-553825067470459 +61847066453483-64140594340859 + +522268425811830 +58341928066253 +255014109831764 +315843162847563 +232938072218405 +19238194231473 +266392673472622 +405603051584155 +122087955120424 +163498875135204 +316521298866315 +216386416919494 +244462636143708 +285238009910535 +232770351114751 +395098050723371 +133177507902191 +287256470908438 +32937479800945 +188321939132043 +428374575414591 +222588409351927 +211483387571430 +420352785783313 +364606620872250 +443433046654032 +134440829394773 +409234262909541 +195431555768032 +244941870406086 +351693863657088 +160579331943327 +335278976276672 +187584576177970 +365304047867811 +453939786893495 +145338557625740 +156156443823469 +178167660118496 +31704188932634 +385750661257533 +238590598966474 +479103035176667 +385983385727179 +109102802895817 +416108956305569 +556512417133498 +74445764218069 +401550605187678 +223901416887738 +466323646857857 +396306234993106 +356240729264499 +381863504277317 +294450332161713 +145288624574100 +431757992022260 +501234842002248 +172625840794735 +146922860850506 +353389540408415 +46324443761482 +54607324791467 +336838869669662 +24862029077778 +380246060390313 +429567326467230 +93482011059279 +503522309214910 +214688163106348 +490693281484231 +421210464643474 +117493101193319 +316810636752782 +344065030918857 +526123006217119 +155288157972082 +388647865632927 +376780231285276 +156323268844200 +439437777431944 +518823794594753 +208806734279641 +266752416192087 +177880434410666 +480712749127986 +516921532107868 +225673778279752 +122909268585638 +503624753931605 +5332807310234 +268957787292002 +157865767176152 +542881331469066 +226299602428643 +455029288257451 +562521502590053 +174088697409185 +3869014646122 +313232427509295 +486720516390751 +259873321715480 +88168530621585 +537869748151225 +106164354130800 +371068025927523 +161334365399053 +61095137645381 +108499466473812 +210217902138701 +335424857636711 +354945905754350 +287449528588491 +310235350850966 +374567554573765 +534026067782047 +329427939127247 +3788937071307 +551811336212883 +485602983607056 +249560823332236 +506885482746450 +533792335036195 +278690852579380 +63225072122982 +554896687355375 +241936114653718 +269672347689056 +62558430115002 +328844745814765 +165689097579304 +421101696882810 +104742139988287 +70289948676540 +100914706595381 +506035708154945 +6346536291804 +19525618065147 +475814754454076 +201772379304521 +155091362597495 +165205181408778 +106149532673050 +293444237227651 +10752088753699 +102742333792540 +88529209257082 +156269706699304 +254065526193458 +447702736208059 +142155699071326 +315770576619610 +20077399278893 +201445079264015 +115683569402529 +76447836471839 +325095475798116 +24585508243375 +255707575848517 +246554114912555 +54572741682952 +320932529639558 +550393567902400 +156614834339836 +254214985127865 +356820402288504 +261634022877099 +307377016738681 +307754068645110 +186625528679656 +526492558851810 +201390995577201 +541475681670522 +448142136550328 +238170735580889 +278138903332437 +268159671439729 +163509820281244 +146616846722080 +430090926260930 +209832057065104 +151374002315947 +537516327838442 +547070201835886 +143427834979038 +547817729991804 +100700162698105 +442960456247426 +70880017032405 +450754608786102 +265710598033549 +134971889861648 +319828881455113 +133677853873520 +71423988413197 +289044946954798 +265012479381519 +307082938857726 +449246104891399 +436232084474015 +202656701349398 +21772457467346 +354135642366064 +528828527650724 +378922096990324 +7003386817481 +466613650917734 +411315686256310 +141103121126049 +331027393331265 +315624949775212 +58792020663883 +534706246779483 +390479268927927 +465599625407918 +275666732945071 +538722303129339 +486762527359651 +503833819887241 +323605025017615 +293641802251829 +467539049211907 +439475077901076 +215555015150566 +353204136528043 +448562997127445 +116184430135658 +257579502882425 +176705241754156 +442633813097981 +176899509703934 +144620397795826 +387583371084238 +377950533212149 +412698313911330 +251853841281635 +487824266816512 +496507201659661 +230100209737010 +255434202422560 +503002918221459 +479486088917587 +11876463905084 +486696315919546 +84063663812618 +479974179115957 +354981606995700 +284153624763422 +232445202234137 +138338049453543 +170943888291789 +437901045034036 +354234021765386 +82418433121773 +45406797149516 +470665842164815 +464293471076587 +71113575223668 +289548048040222 +76678209678259 +448962568885499 +219736441363714 +46089374138870 +181429997499787 +215792539063393 +519501951676038 +554185940142208 +350707432219348 +113382408819701 +407719259801762 +217631549461165 +546661725054690 +135538004710123 +355678002063914 +235138052482578 +376508427736755 +88836071188395 +44049450208314 +462368681292444 +380593745459031 +11265659759789 +289500529041586 +317181028103663 +484392335609616 +6471743833805 +160516003082486 +115141678060367 +34394456361293 +428506343214664 +67834825552691 +371704968378355 +71461424354682 +220902080424883 +464265278386287 +447931208293242 +378908807990054 +273500615495985 +158725928736531 +240119229189138 +283684730220286 +277342500339172 +107028834947392 +5133529553047 +105296006886471 +185516143549306 +146303107743922 +124279892113901 +152911267053671 +198932101357516 +313672800572274 +33767737088861 +512390427504898 +294326717669767 +291138260097070 +198020080198704 +469425336414435 +162107030423088 +146860436315904 +359387503053820 +435327832730714 +82932629824843 +265480731831228 +450290255702277 +388722160757197 +139901224037611 +552122750331812 +94815669097041 +14555512984964 +218885867571857 +291379613084005 +430956274353209 +379403395922618 +413510335028658 +386106690791664 +434612154318173 +262511832536836 +155725992682330 +401910159343806 +396394490356238 +57443678242625 +543961501698890 +235459207604743 +530978885804003 +196136757872609 +367849484857813 +159382132610458 +316420429148601 +79136461697580 +98099872195640 +35144594855549 +557257995921798 +274678520935764 +367871094311129 +455508093113179 +415678845138229 +398719791886039 +409663969244563 +170417349432431 +132296081995422 +468458850259073 +405786366476048 +204527335955581 +89519771645577 +186793113488710 +197172404172231 +8123757636577 +33859155781227 +526414053813419 +550108894907272 +427758316754097 +430439193348795 +152985347863083 +407357534838100 +380630010834074 +378499710786526 +391683263784339 +235927635383829 +172392085484758 +378409154262197 +322277473045239 +515832965272084 +69573211257879 +429438737637217 +461591109047897 +538916142777584 +211277182572659 +518029039226072 +307265750365549 +422625831942065 +170122214705943 +73159439284963 +140486950301977 +535271525179306 +1706714418292 +203284656002007 +163008748377037 +514237600082406 +35928140999086 +278642505003002 +194306346751764 +472785559196719 +141216101366622 +459838732066572 +301271761992836 +260435287671722 +294895291899364 +550531387850489 +458869567800346 +338551470401642 +142193778382383 +203445897567592 +440556858669593 +236329512643400 +187483114530502 +65598598657363 +428284415637590 +265725791071353 +551249686244338 +335498314049613 +335517695016382 +156715978643154 +280333011097921 +239932029810785 +546082204952011 +319998027885304 +525003667511858 +82870977921682 +165665780099625 +97049938869069 +280179665568642 +63124932086434 +420115109028748 +178571005850235 +159238772780721 +406500104374341 +37828303242635 +360102094141870 +540715661470620 +164638481969105 +153876795364166 +475405772167422 +127104170807503 +456127484408450 +524779418576973 +519461408279416 +287703204350577 +104158535107420 +326321393590219 +518030496027283 +553821878119290 +438748017597125 +290103152753321 +504924719780971 +69113296811475 +66177208865764 +157818869139341 +85151130484317 +277345377753708 +234584767854645 +444485939931951 +172274489515952 +26696437765364 +71300837075030 +529067116633242 +347345950204210 +524441935927717 +345098509305857 +531161451799306 +442273594579844 +472509756779632 +508827321196329 +243908057655704 +185541381140448 +408826968429599 +407534509311192 +151910125977744 +488789932273075 +437849554259777 +483713463730730 +297955715583440 +63706115520600 +245778383504500 +64330506210084 +173199378689250 +433552772160777 +364190902156416 +454960668246602 +101380098505418 +551818616659312 +148356893868424 +119053869195222 +521833506055334 +490779577188217 +203288001766342 +517561653307775 +113976209371970 +202860556007143 +153740824459975 +163852107010557 +94558632576245 +551555013171094 +256605707168210 +85658202102839 +449726433502070 +365443636888027 +188751352423476 +342850875431614 +266977160178029 +475489731287876 +306499575864069 +384672358156173 +480772613122228 +967936979743 +262616589706552 +174081653835715 +514210408341256 +555296312944773 +557019812239581 +108947869801 +480979773452864 +122106165798653 +342503677839643 +406311789445311 +268425895098237 +226868633308318 +95614524774601 +157647423601681 +101589309041885 +464236913664714 +226234005926657 +219658385980986 +467833965810646 +267475626844713 +396598453912391 +528374073759164 +48399986267436 +401115689121541 +96749374728296 +97663702216747 +539348445567378 +476162396663489 +446755105051181 +240030392724329 +81959632696319 +198034320107976 +368904682200622 +366697162016555 +301537075198997 +510639173248153 +478399551501607 +458001581498818 +371215957989005 +419415397032680 +557042197977017 +81640722102855 +101687529734138 +361161139092044 +315348743421537 +367919366653635 +156923358603588 +174173885018200 +13340794622753 +152443763456078 +118333674185046 +545016087146089 +165867517035497 +329419488047867 +128785676927126 +83340840223053 +164218083371866 +85372463248980 +81840449728525 +161454003862752 +296020743572507 +74520882040063 +292100708348731 +418433665660335 +194550295909312 +205700380717345 +140081816103205 +506811246928702 +84058042018421 +254671031109686 +48322341671590 +443341518290168 +258720147608721 +247778720764879 +490210044045597 +15977000452498 +491235592987621 +388970213731515 +318611086305850 +540978570370659 +381568124743210 +354972815550324 +194253598399274 +217124257445345 +296981040420710 +104213634268578 +554704731366517 +449572575610744 +454458345780882 +207588189730252 +258275595415696 +396445678529436 +280432846459584 +74022361977186 +32678015528549 +265815426531967 +326956940032012 +175803276896058 +339550985311204 +368330539704141 +183118126312787 +28993841207264 +361426960770594 +166308412272869 +393563241351710 +382893421100575 +254386236363854 +495887766607651 +386060415139629 +533023396501415 +351589541104171 +100105062081903 +497029001339824 +277350233058173 +395058538211874 +448629949680092 +136981165485649 +510396040555591 +98919111505347 +265234547253967 +174199186136998 +46864239082385 +131413239128726 +354300504536400 +272865960383179 +552969309748123 +174273235052923 +503566043199917 +59172133860765 +450119619566344 +343622473648391 +430901740896048 +359329830608041 +427341306283631 +91367360423183 +81645922818788 +279798975522659 +321573413529428 +510283703574127 +126752331161988 +238224244150221 +314146170809421 +406370214848390 +494202304589110 +117262010501066 +190008018878234 +460904243404300 +468085851685569 +51789164569805 +345469313980792 +331490702080982 +208084893466741 +215453748884070 +446171759494858 +195308555016048 +59509168033425 +234883528181320 +306345152401339 +233019601717403 +479832400199373 +484702075723597 +261407101950338 +209892979622840 +166284424921136 +264172956731557 +267631421371696 +75135059126929 +183112512741000 +169288618847820 +22702628194649 +51112155243707 +331732079061532 +110421717315904 +399394401209644 +442188789146202 +189455695993232 +54861098847948 +295015293902505 +185098521369464 +457589563053693 +560917315345802 +545040837185642 +383327100035763 +149666980790995 +283105556029967 +268874017206446 +306870979789828 +5950357206942 +500822651116976 +268105922570827 +433224356606500 +356751589226626 +212015504960892 +19382874066773 +24219927731584 +443849867654428 +485299282437603 +528874164034342 +502826823259923 +50908664742642 +187030781272209 +194072946650380 +504290357987181 +180146491098756 +230553647048412 +113917036308954 +375788942110053 +490605812478790 +519330025431418 +137419963624814 +201569761011069 +9480592099536 +472090660911006 +206753069028027 +458259848582221 +248826009280049 +184607829805908 +250494301390092 +275342595561645 +122242252065649 +156196503156114 +92296579808870 +74217856715696 +195244646938556 +341472309590662 +239064736911306 +446392270870172 +191521545598773 +82995387072667 +550235961070923 +355902572749111 +275438130107556 +326627446910088 +248535148289402 +93822887018734 +258481107589613 +551784197179282 +143809378475943 +367094827822666 +66252187165041 +189823598658316 +365510286034575 +240234908018922 +35315136081683 +18081730568484 +439400406439844 +378258419862605 +407510559390305 +345338411124702 +458248624678447 +107130797316410 +465794387213716 +82517039229613 +317732119923775 +202234491082735 +133277265216356 +339428102361697 +504715018551943 +149564589157420 +333897402557700 +166387765267602 +394734429868594 +84235193891914 +12808242938273 +539428472166051 +148491206973675 +108290830454838 +69325948731628 +58075041515849 +194094139256646 +361134280920318 +230058201598134 +155470143683817 +75797983096252 +293904345711224 +65925813953618 +78147659102664 +278613888712541 +277589279868399 +447736918254698 +273749537976682 +41702245130580 +470083016794883 +375307061922216 +455979509709128 +6208109673922 +48127833053387 +199643796855355 +79530147874685 +368375848231329 +26032135379364 +125852681465990 +345272840712858 +12959796000640 +369154965439655 +6226291608820 +356213610933783 +326501044333283 +528042020579136 +364874510888959 +525030914131956 +491751469681677 +103413821989208 +104464123832285 +442273011774060 +228883129318903 +142207288256713 +321644335387344 +263834808764912 +65046354682216 +56798318595030 +191333927611985 +150829625837253 +126232380779498 +72622592596260 +412559410160292 +301449671026116 +559459172918876 +398513600128804 +528427551705542 +312139395783111 +193822855748043 +491629639271943 +463656937290223 +467021910079109 +181195660505841 +257977427417482 +388057462249765 +225725126746217 +86268413977309 +491213280262732 +458614915371195 +22273818951004 +468780424702625 +536933077001014 +496120838621027 +335151107483505 +418848922785654 +546114456979767 +467050722143514 +419016096879970 +208950118068705 +46239176985547 +62154608924248 +107168569888720 +58980362520432 +122047834171265 +478733783356228 +136950947540420 +165023278448909 +544744214940896 +529048550722243 +53230344341406 +200078416853115 +17104887836511 +19912237186819 +344046181070903 +269239927198105 +354704064722889 +353702393034456 +457625329073557 +57829421026343 +161748202644160 +364995130015300 +366152936025197 +259881085355924 +218012857543645 +404511261279477 +147525680200970 +549400248914135 +295520434089937 +535148558876549 +347144353410250 +227655478464968 +201943593222412 +198422412095281 +536555204204103 +110969899184009 +386163644928839 +417734498918300 +62263277079372 +398966953404239 +254059235829297 +486981976533329 +417315203085410 +24527645171959 +544640788595782 +157687153115875 +302599428782975 +37202598794670 +24250339443146 +70112251339720 +491056738812447 +22141714311902 +167023996819965 +354884346055617 +362635101945206 +356609281688422 +499086885108526 +179903109525110 +126801889293083 +113385780783605 +548901779729641 +264667938986079 +262126453251697 +534237204595353 +143802723317071 +388321951909634 +297709851944253 +120702256719193 +535481108529532 +490920924006183 +423859539322624 +289878037043922 +7283092373101 +82644252942341 +365343808709762 +254583212513165 +185776440739207 +906575294370 +368795017014698 +145679755079585 +299574654126826 +48121232683634 +213273036185622 +385359463768521 +154242899511592 +126564275894839 +446471589054538 +357485080609641 +348952407077346 +465023919243636 +139632747453290 +20601639946113 +132495824403437 +538886340307624 +64210869638075 +10295730576351 +507329877456014 +426902002766141 +370922358084867 +207931651824892 +135704653994954 +525219092816682 +435503071299719 +502924120970810 +33327304328811 +187354799573736 +397356124169193 +403904198319633 +150011076702236 +247973610195657 +54677732861479 +374628123962656 +265806552296980 +98580886664027 +392561271404753 +557841296401612 +430645543566707 +550140505203572 +516191528100466 +84527986514542 +513070923820895 +432819046856184 +177839615092019 +266043399855097 +194801159145767 +353510684747755 +540586542239904 +380220223728163 +448900463683128 +317199941073069 +360474465987966 +264480511951897 +119655846555726 +67917305960231 +296007444718361 +395792875364672 +164362101285074 +538343539400921 +167689689862956 +51298031167366 +1612927824087 +92073285006472 +151989719510572 +404906777217450 +527820283544138 \ No newline at end of file diff --git a/mark/day5/part1.rb b/mark/day5/part1.rb new file mode 100644 index 0000000..87b8b10 --- /dev/null +++ b/mark/day5/part1.rb @@ -0,0 +1,102 @@ +require 'debug' + +# Generic FileHandler +class FileHandler + def initialize(file_path) + @file_path = file_path + end + + # Usage: + # FileHandler.new('/path_to_file').read_lines do |line| + # # process line + # end + def read_lines + File.foreach(@file_path, chomp: true).map do |line| + yield(line) + end + end + + # Usage: + # file_content = FileHandler.new('/path_to_file').read_file( + def read_file + File.read(@file_path, chomp: true) + end +end + +class PuzzleSolver + def initialize(file_handler, debug: false) + @handler = file_handler + @debug = debug + end + + def debug(message) + puts message if @debug + end + + def print_debug(message) + print message if @debug + end + + def solve + raise NotImplementedError, 'Please implement this method in subclasses.' + end +end + +class FreshnessDatabase + # @param [] list of ranges of fresh IDs + def initialize(fresh_ranges) + @fresh_ranges = fresh_ranges + end + + def fresh_id?(id) + @fresh_ranges.any? {|range| range.include?(id)} + end +end + +class Solver < PuzzleSolver + # @param [String] e.g., "11-22" + # @return [Range] e.g., 11..22 (inclusive) + def to_range(string) + boundaries = string.split('-').map(&:to_i) + raise "Invalid boundaries for #{string}, got #{boundaries.inspect}" if boundaries.length != 2 + Range.new(boundaries[0], boundaries[1]) + end + + # @param [String] full input e.g., "11-22\n33-604\n..." + # @return [] e.g., [11..22, 33..604, ...] + def parse_ranges(input) + input.split("\n").map {|range| to_range(range)} + end + + # @param [String] ids_str e.g., "1\n5\n8\n11\n17\n32" + # @return [] e.g., [1, 5, 8, 11, 17, 32] + def parse_ids(ids_str) + ids_str.split("\n").map(&:to_i) + end + + def solve + two_parts = @handler.read_file.split("\n\n") + raise "Invalid file format: #{two_parts.inpsect}" if two_parts.length != 2 + + db = FreshnessDatabase.new(parse_ranges(two_parts[0])) + ids = parse_ids(two_parts[1]) + + fresh_ids = ids.select {|id| db.fresh_id?(id)} + fresh_ids.length + end +end + + +if ARGV[0].nil? || ARGV[0].empty? + puts "Usage: ruby #{__FILE__} [debug]" + exit 1 +end +file_path = ARGV[0] +debug = (ARGV[1] == "debug") +file_handler = FileHandler.new(file_path) + +## Puzzle-specific +cls = Solver +## + +puts "The answer is: #{cls.new(file_handler, debug:).solve}" diff --git a/mark/day5/part2.rb b/mark/day5/part2.rb new file mode 100644 index 0000000..b006202 --- /dev/null +++ b/mark/day5/part2.rb @@ -0,0 +1,198 @@ +require 'debug' + +# Generic FileHandler +class FileHandler + def initialize(file_path) + @file_path = file_path + end + + # Usage: + # FileHandler.new('/path_to_file').read_lines do |line| + # # process line + # end + def read_lines + File.foreach(@file_path, chomp: true).map do |line| + yield(line) + end + end + + # Usage: + # file_content = FileHandler.new('/path_to_file').read_file( + def read_file + File.read(@file_path, chomp: true) + end +end + +class PuzzleSolver + def initialize(file_handler, debug: false) + @handler = file_handler + @debug = debug + end + + def debug(message) + puts message if @debug + end + + def print_debug(message) + print message if @debug + end + + def solve + raise NotImplementedError, 'Please implement this method in subclasses.' + end +end + +class FreshnessDatabase + # @param [] list of ranges of fresh IDs + def initialize(fresh_ranges) + @fresh_ranges = fresh_ranges + end + + def fresh_id?(id) + @fresh_ranges.any? {|range| range.include?(id)} + end + + def parsed_ranges + all_ranges = [] + current_min = Float::INFINITY + current_max = -Float::INFINITY + @fresh_ranges.each_with_index do |range, index| + if range.max < current_min + all_ranges.insert(0, range) + current_min = range.min + current_max = range.max if range.max > current_max + next + end + + if range.min > current_max + all_ranges << range + current_max = range.max + current_min = range.min if range.min < current_min + next + end + + overlapping_range_indices, closest_range_index = FreshnessDatabase.find_all_overlapping_range_indices(all_ranges, range) + + if overlapping_range_indices.empty? + all_ranges.insert(closest_range_index + 1, range) + elsif overlapping_range_indices.length == 1 + overlap_index = overlapping_range_indices[0] + new_range = FreshnessDatabase.max_range([all_ranges[overlap_index], range]) + all_ranges[overlap_index] = new_range + range = new_range + else + min_index = overlapping_range_indices[0] + max_index = overlapping_range_indices[overlapping_range_indices.length - 1] + min_range = all_ranges[min_index] + max_range = all_ranges[max_index] + new_range = FreshnessDatabase.max_range([min_range, range, max_range]) + + new_ranges = [] + all_ranges.each_index do |i| + if i < min_index + new_ranges << all_ranges[i] + elsif i == min_index + new_ranges << new_range + elsif min_index < i && i <= max_index + # do nothing + else + new_ranges << all_ranges[i] + end + end + all_ranges = new_ranges + range = new_range + end + + current_min = range.min if range.min < current_min + current_max = range.max if range.max > current_max + end + + all_ranges + end + + def num_fresh_ids + ranges = parsed_ranges + puts ranges.inspect + num_ids = ranges.map {|range| range.size}.sum + + num_ids + end + + private + + def self.overlapping_ranges?(range1, range2) + (range1.min <= range2.min && range2.min <= range1.max) || + (range1.min <= range2.max && range2.min <= range1.max) + end + + def self.max_range(ranges_array) + (0...ranges_array.length - 1).each do |index| + raise "Non overlapping ranges found on #{index}: #{ranges_array}" if !self.overlapping_ranges?(ranges_array[index], ranges_array[index+1]) + end + + bounds = ranges_array.flat_map {|range| [range.min, range.max]} + Range.new(bounds.min, bounds.max) + end + + def self.find_all_overlapping_range_indices(all_ranges, range) + # NOTE: we assume `range` is between smallest and largest range, as those cases are handles separately in main loop + raise "Error unmet assumption: #{all_ranges}" if all_ranges.length < 2 + + indices = [] + closest_range_index = 0 # used in case there is no overlap + all_ranges.each_with_index do |possible_range, index| + if self.overlapping_ranges?(possible_range, range) + indices << index + else + closest_range_index = index if range.min > possible_range.max + end + end + + [indices, closest_range_index] + end +end + +class Solver < PuzzleSolver + # @param [String] e.g., "11-22" + # @return [Range] e.g., 11..22 (inclusive) + def to_range(string) + boundaries = string.split('-').map(&:to_i) + raise "Invalid boundaries for #{string}, got #{boundaries.inspect}" if boundaries.length != 2 + Range.new(boundaries[0], boundaries[1]) + end + + # @param [String] full input e.g., "11-22\n33-604\n..." + # @return [] e.g., [11..22, 33..604, ...] + def parse_ranges(input) + input.split("\n").map {|range| to_range(range)} + end + + # @param [String] ids_str e.g., "1\n5\n8\n11\n17\n32" + # @return [] e.g., [1, 5, 8, 11, 17, 32] + def parse_ids(ids_str) + ids_str.split("\n").map(&:to_i) + end + + def solve + two_parts = @handler.read_file.split("\n\n") + raise "Invalid file format: #{two_parts.inpsect}" if two_parts.length != 2 + + db = FreshnessDatabase.new(parse_ranges(two_parts[0])) + db.num_fresh_ids + end +end + + +if ARGV[0].nil? || ARGV[0].empty? + puts "Usage: ruby #{__FILE__} [debug]" + exit 1 +end +file_path = ARGV[0] +debug = (ARGV[1] == "debug") +file_handler = FileHandler.new(file_path) + +## Puzzle-specific +cls = Solver +## + +puts "The answer is: #{cls.new(file_handler, debug:).solve}"