From 15c6d8d79d9ec00a7abb14f90bd65547893c76f2 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Fri, 25 Dec 2020 15:25:06 +0100 Subject: Add day 9 --- 09/a.rb | 32 ++ 09/b.rb | 19 ++ 09/input.txt | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 1051 insertions(+) create mode 100644 09/a.rb create mode 100644 09/b.rb create mode 100644 09/input.txt diff --git a/09/a.rb b/09/a.rb new file mode 100644 index 0000000..ea90532 --- /dev/null +++ b/09/a.rb @@ -0,0 +1,32 @@ +preamble = 25 + +def has_sum hash, sum + hash.keys.any? do |n| + hash[sum - n] > 0 + end +end + +def update array, hash, n + hash[n] += 1 + hash[array.first] -= 1 + array.shift + array.push n +end + +lines = File.readlines('input.txt').map {|l| l.to_i} +current25 = lines.slice(0, preamble) +current25hash = Hash.new 0 +current25.each do |n| + current25hash[n] += 1 +end + +invalid = 0 + +lines.slice(preamble, lines.size).each do |n| + if invalid == 0 && (!has_sum current25hash, n) + invalid = n + end + update current25, current25hash, n +end + +puts invalid diff --git a/09/b.rb b/09/b.rb new file mode 100644 index 0000000..c95c1c9 --- /dev/null +++ b/09/b.rb @@ -0,0 +1,19 @@ +sum = 400480901 +lines = File.readlines('input.txt').map {|l| l.to_i} + +current_list = [] +current_sum = 0 + +lines.each do |n| + current_sum += n + current_list.push n + + while current_sum > sum && current_list.size > 1 + current_sum -= current_list.first + current_list.shift + end + + if current_sum == sum && current_list.size > 1 + puts current_list.min + current_list.max + end +end diff --git a/09/input.txt b/09/input.txt new file mode 100644 index 0000000..c27e1a4 --- /dev/null +++ b/09/input.txt @@ -0,0 +1,1000 @@ +30 +17 +44 +5 +10 +33 +19 +31 +22 +25 +39 +49 +1 +26 +38 +36 +14 +27 +15 +50 +28 +41 +20 +34 +18 +6 +24 +16 +11 +81 +7 +8 +63 +65 +58 +9 +109 +10 +13 +26 +86 +17 +57 +32 +14 +15 +42 +22 +21 +30 +40 +39 +28 +37 +20 +16 +18 +24 +19 +23 +35 +33 +25 +56 +29 +31 +34 +92 +75 +64 +61 +36 +38 +41 +42 +78 +43 +39 +44 +48 +37 +97 +59 +55 +62 +58 +106 +105 +79 +60 +87 +93 +73 +114 +74 +75 +149 +116 +99 +108 +82 +76 +162 +81 +110 +118 +150 +113 +220 +120 +131 +133 +213 +134 +142 +147 +148 +196 +221 +156 +151 +157 +181 +175 +242 +158 +186 +214 +384 +233 +231 +244 +251 +253 +254 +352 +467 +276 +281 +347 +304 +299 +308 +313 +307 +309 +315 +475 +783 +389 +575 +400 +445 +464 +477 +482 +624 +504 +851 +622 +1079 +923 +1251 +704 +603 +606 +795 +620 +1158 +616 +773 +715 +789 +1477 +834 +1794 +845 +909 +941 +986 +1318 +1695 +1981 +1209 +1219 +1222 +1223 +1226 +1307 +2304 +2012 +1236 +1549 +1331 +1786 +1488 +1504 +2649 +2272 +1679 +3183 +2727 +2131 +2177 +2195 +2428 +2431 +3667 +2432 +2445 +2905 +3874 +2543 +2567 +2724 +2785 +4862 +3932 +3635 +2992 +3167 +4584 +4563 +5423 +5159 +4308 +4326 +4859 +6521 +10285 +6080 +5110 +4877 +4975 +4988 +5267 +5291 +7895 +10153 +7773 +5777 +7730 +6627 +13196 +7751 +10887 +8892 +8634 +9167 +9185 +9314 +9203 +10126 +9963 +9865 +10255 +9852 +10144 +10242 +10279 +16492 +11068 +14378 +12404 +14944 +13507 +16622 +15261 +16385 +16954 +17526 +17801 +17819 +25695 +18388 +18517 +28796 +20534 +19717 +19996 +20131 +20094 +26734 +20521 +24657 +23472 +26012 +41995 +35392 +28451 +33339 +43119 +34186 +47184 +34773 +38611 +35620 +45729 +40251 +62126 +39811 +39713 +58328 +39848 +44653 +43993 +40615 +56141 +55294 +48129 +51923 +61404 +89480 +83706 +61790 +102321 +95989 +68959 +70393 +73384 +74231 +75333 +79524 +79561 +114846 +79659 +80328 +80463 +83841 +84608 +88744 +92538 +100052 +107217 +109533 +113327 +131797 +130749 +179711 +136021 +144292 +172099 +139352 +257619 +272151 +149564 +154857 +159085 +352479 +164304 +159987 +207269 +176379 +168449 +231890 +181282 +244344 +340548 +291784 +276089 +268184 +262546 +275041 +275373 +280313 +283644 +429877 +318013 +304421 +495405 +308649 +366354 +323389 +328436 +324291 +336366 +344828 +491838 +460233 +544273 +425626 +519385 +607033 +543225 +537587 +551828 +642304 +550414 +555686 +641402 +847646 +613070 +805061 +627810 +632038 +632940 +659755 +647680 +972638 +660657 +681194 +895242 +1099959 +1073303 +1230687 +1479684 +1056972 +1080812 +1089415 +1168756 +1811060 +1210169 +1293597 +1183496 +2458740 +1246010 +1508312 +1259848 +2597727 +1620318 +1737095 +1905765 +1328874 +1341851 +1555899 +1576436 +1984657 +2146387 +2130275 +2137784 +2900475 +2170227 +2352252 +2258171 +3356556 +2393665 +2836284 +2983105 +4428398 +2884773 +2505858 +3518019 +3430075 +2670725 +2897750 +3890422 +3461664 +4758949 +5335357 +3132335 +4122441 +5967522 +4268059 +4300502 +6558673 +4522479 +9429186 +8319774 +4651836 +5176583 +4899523 +7251164 +5390631 +5568475 +5403608 +5935933 +8012863 +5803060 +7549586 +6030085 +6593999 +7254776 +8919895 +7400394 +9477085 +8390500 +14832794 +9422002 +8822981 +9174315 +9551359 +9828419 +13496357 +11770582 +14598585 +10290154 +10794239 +11738993 +10972083 +17674926 +11833145 +12397059 +12624084 +13284861 +13430479 +14984499 +15645276 +15790894 +16574709 +28263273 +40660332 +22046086 +54156689 +17997296 +22564821 +19379778 +21567412 +21084393 +33306405 +21262237 +21766322 +22533232 +54390798 +22805228 +43028559 +24230204 +26715340 +25908945 +28269360 +28414978 +41425864 +31436170 +50461064 +39259533 +42227500 +37377074 +39081689 +39564708 +45492441 +40464171 +40642015 +44100644 +42346630 +59910306 +61025855 +44299554 +50948210 +63447243 +86447274 +54323923 +50139149 +52624285 +59851148 +56684338 +72515622 +70517859 +83360177 +83381243 +78019089 +76458763 +77841245 +78646397 +130465530 +84763725 +95247764 +84742659 +86646184 +93294840 +109308623 +111165004 +136586423 +101087359 +102763434 +123142144 +104463072 +106823487 +112475433 +116535486 +133143101 +148359104 +155105160 +155860334 +154300008 +154477852 +197239158 +156487642 +187506093 +197218092 +191587212 +219298920 +171388843 +199121617 +223358973 +220473627 +203850793 +205550431 +207226506 +323761992 +235617577 +211286559 +344429660 +229010919 +374951479 +404672048 +302659112 +353078426 +312347976 +401089951 +310965494 +353726800 +360338435 +370510460 +382675402 +362976055 +423149713 +375239636 +434739194 +409401224 +673941549 +518192000 +412776937 +783765353 +440297478 +446904136 +646025753 +400480901 +687587612 +783156303 +613624606 +615007088 +623313470 +975345523 +664692294 +720366718 +714065235 +1036774319 +733486515 +772377279 +875036672 +775720537 +840778379 +809882125 +847385037 +813257838 +1616498916 +853074415 +887201614 +1014105507 +1310718047 +1023794371 +1015487989 +1310901082 +1228631694 +1236938076 +1238320558 +1378757529 +1942696929 +1385059012 +1453853233 +1700459452 +2510341577 +1505863794 +1548097816 +1588978375 +1623105574 +1623139963 +2198102696 +1876868786 +2429756754 +2125522172 +1740276029 +2253808547 +2029593496 +3094842169 +3408351025 +2244119683 +2465569770 +2466952252 +2475258634 +2617078087 +2763816541 +2933156828 +3171237779 +2959717027 +5426669279 +5338961852 +3053961610 +4815180783 +3329254404 +3246245537 +3363415992 +3769869525 +5046834841 +3865798201 +4357354116 +6013678637 +4273713179 +5203836710 +4709689453 +4711071935 +4719378317 +4932522022 +6288971431 +5092336721 +6296572820 +5696973369 +5892873855 +6130954806 +12189446675 +7133285517 +6300207147 +6383216014 +6575499941 +6609661529 +7016115062 +7635667726 +8043582704 +12193081002 +10849213120 +8631067295 +8983402632 +8984785114 +10024858743 +9429067770 +9430450252 +12431161953 +15558902573 +10789310090 +10985210576 +11589847224 +11827928175 +12023828661 +22379157314 +12683423161 +12875707088 +12909868676 +16040111781 +14211167667 +21614705967 +23860917664 +34650227754 +16674649999 +39901029445 +20814168833 +17614469927 +17968187746 +24273270385 +29558034970 +18859518022 +33527090319 +22617238265 +23417775399 +24703635263 +22575057800 +23613675885 +23851756836 +24899535749 +54341259152 +25559130249 +41887740312 +27121036343 +81788769757 +30885817666 +34289119926 +42318105190 +35534168021 +34642837745 +42711274858 +41466226763 +35582657673 +83784331953 +41434575822 +97052534010 +52386608341 +47031451284 +82614108957 +45992833199 +52020572092 +65501416197 +81674289029 +48751292585 +50458665998 +52680166592 +84177501621 +79507644684 +76968743843 +77017233495 +68931957671 +113796764610 +70177005766 +70225495418 +76109064508 +77048884436 +81575490872 +84333950258 +149684650450 +88466027106 +93024284483 +130170334820 +116169838965 +94744125784 +104700738684 +102479238090 +149733140102 +128789231100 +99209958583 +164921131550 +122905662010 +169993028326 +169386964349 +139157453089 +139108963437 +223442913695 +164575091614 +140402501184 +195503522573 +153157948944 +197223363874 +238199167208 +183210152890 +181490311589 +187675985689 +292315402033 +193954084367 +201689196673 +199444864468 +207179976774 +279511464621 +322743315467 +222115620593 +238318922020 +262014625447 +363947112693 +278266416526 +500213792655 +304977592798 +321892812773 +293560450128 +352251077303 +323612654074 +380935176057 +347112033311 +460434542613 +369166297278 +364700464479 +387120850157 +381630070056 +393398948835 +423804817266 +566389661152 +477711280994 +445498898794 +484130246040 +500333547467 +500382037119 +531879372148 +540281041973 +921216218030 +615453262901 +598538042926 +770916850577 +617173104202 +784047196687 +913501694463 +704547830131 +711812497790 +1000715584586 +751821314636 +901516098260 +746330534535 +1093442567846 +775029018891 +817203766101 +924186854385 +923210179788 +929629144834 +945880935913 +1445381066611 +1157454146175 +1032261409267 +1130417415074 +1401220300889 +1215711147128 +1213991305827 +1388089954779 +1321720934333 +1363503638737 +1416360327921 +1450878364666 +1456369144767 +1698239198679 +1592232784992 +2722941235222 +1741390620486 +2672080291895 +1699215873276 +2138532657628 +1763084702014 +1847397034173 +1852839324622 +4060170246674 +1978142345180 +2162678824341 +2189715555442 +2664869670493 +2344408720901 +3303717689288 +2429702452955 +3403706861269 +2685224573070 +2738081262254 +3104894259223 +2872729472688 +2907247509433 +3594901802395 +3887954754121 +5652403031666 +4384440446346 +4568235110583 +3462300575290 +3546612907449 +7979342248741 +3610481736187 +4015518148963 +6237279770968 +4140821169521 +5610810734942 +6295706309257 +4534124276343 +5790118832293 +5977623731911 +5167783715209 +6795202263554 +6147525148360 +7291687955779 +6419342380137 +8757297290889 +8129026078738 +6369548084723 +9699580346258 +7625999885150 +7008913482739 +14084020792604 +11363437374137 +7072782311477 +7157094643636 +7751302905708 +9183301864172 +11400929567235 +8674945445864 +10511748008254 +9701907991552 +11537331799932 +10903672361066 +10957902547502 +17684856948292 +11315308863569 +13214544643691 +13773525033510 +13378461567462 +12788890464860 +13442330396200 +13526642728359 +14698782196627 +14824085217185 +14166008126375 +14229876955113 +14908397549344 +19695049872426 +16774690303029 +15832040089500 +16426248351572 +18376853437416 +19186693454118 +30530822286127 +20213655999806 +20605580352618 +22441004160998 +21861574908568 +22273211411071 +24529853507260 +35651672978533 +28038629860876 +26167352032322 +28620930554360 +26231220861060 +56762043147187 +28225424924986 +28395885081488 +30592256477947 +36121298223998 +29138274504457 +30740437638844 +35527089961926 +33200938654601 +40650064848487 +34803101788988 +39400349453924 +39792273806736 +58756247211113 +40819236352424 +46836801213678 +44134786319639 +46391428415828 +46803064918331 +75319363768662 +54205981893198 +52398572893382 +54392776957308 +59213187032307 -- cgit v1.2.3