From 2b7b974cf58e7ad7957ffb50e0dab40bb0bceb0c Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Thu, 10 Dec 2020 00:05:39 +0100 Subject: [PATCH] 9 --- solutions/9/README.md | 11 + solutions/9/data.txt | 1000 ++++++++++++++++++++++++++++++++++++ solutions/9/python/main.py | 50 ++ solutions/9/test.txt | 20 + 4 files changed, 1081 insertions(+) create mode 100644 solutions/9/README.md create mode 100644 solutions/9/data.txt create mode 100644 solutions/9/python/main.py create mode 100644 solutions/9/test.txt diff --git a/solutions/9/README.md b/solutions/9/README.md new file mode 100644 index 0000000..4f4468e --- /dev/null +++ b/solutions/9/README.md @@ -0,0 +1,11 @@ +# 9 + +Again the python std lib saved me some lines of code. `itertools` to the rescue and basically we are done. + +
+ Solutions +
    +
  1. 23278925
  2. +
  3. 4011064
  4. +
+
diff --git a/solutions/9/data.txt b/solutions/9/data.txt new file mode 100644 index 0000000..b71d454 --- /dev/null +++ b/solutions/9/data.txt @@ -0,0 +1,1000 @@ +13 +41 +12 +29 +40 +27 +22 +38 +4 +8 +33 +39 +16 +45 +47 +23 +37 +31 +1 +10 +24 +20 +9 +32 +46 +5 +11 +17 +6 +21 +7 +18 +22 +40 +44 +8 +15 +12 +25 +64 +13 +16 +14 +19 +29 +28 +26 +20 +23 +38 +24 +30 +27 +35 +31 +21 +32 +34 +39 +61 +22 +33 +36 +37 +40 +41 +42 +63 +55 +43 +44 +45 +50 +51 +53 +66 +78 +73 +52 +54 +56 +96 +58 +59 +65 +62 +75 +95 +80 +81 +83 +85 +101 +102 +87 +89 +97 +105 +103 +106 +108 +111 +170 +110 +193 +141 +117 +120 +121 +140 +294 +155 +197 +198 +186 +182 +172 +176 +184 +391 +211 +200 +218 +275 +214 +290 +221 +231 +227 +237 +238 +403 +241 +569 +398 +360 +407 +348 +383 +356 +801 +372 +376 +402 +411 +682 +414 +439 +435 +441 +452 +759 +597 +464 +817 +794 +589 +724 +736 +704 +720 +728 +731 +1134 +732 +748 +787 +778 +841 +1139 +849 +853 +1293 +887 +1200 +916 +1305 +1053 +1460 +1309 +1367 +1455 +1467 +2029 +2087 +1448 +1459 +1463 +2427 +1480 +1601 +1565 +1619 +1690 +2316 +1702 +3003 +2335 +2362 +3484 +2764 +2513 +2420 +4693 +3170 +3886 +4629 +2907 +2911 +2922 +4203 +2939 +5588 +3045 +5683 +3220 +7393 +3309 +3392 +5273 +4037 +4697 +4848 +8089 +4933 +5184 +5327 +5331 +6142 +5818 +6612 +5829 +5861 +5833 +5967 +5984 +10815 +6265 +6354 +6529 +6701 +11974 +7346 +11813 +10558 +8734 +10664 +10117 +10260 +12094 +10511 +10658 +11192 +12445 +11647 +23460 +11662 +11800 +12098 +16080 +12249 +17457 +12619 +12883 +13230 +14047 +17463 +17606 +20377 +20771 +18851 +20628 +20775 +21452 +21169 +21703 +21850 +33686 +23309 +23447 +32428 +30836 +25847 +24981 +24868 +25132 +25502 +44075 +34335 +30693 +31510 +35069 +36457 +59188 +43937 +40554 +60916 +65527 +42621 +61589 +47550 +56378 +58554 +46756 +48315 +49849 +56540 +84690 +50370 +50000 +110192 +94257 +84184 +71247 +83213 +66579 +75623 +77011 +83175 +97920 +87310 +132836 +116949 +90171 +94306 +95071 +158798 +98164 +122379 +146870 +137159 +100370 +121247 +116579 +125623 +161195 +143590 +153889 +148258 +152634 +286725 +264837 +252053 +222749 +182381 +181616 +184477 +188335 +185242 +189377 +193235 +198534 +214743 +247240 +318858 +216949 +237826 +242202 +269213 +273881 +291848 +296224 +333500 +378477 +453172 +363997 +367623 +396359 +387911 +366093 +386869 +369719 +382612 +374619 +391769 +581146 +413277 +452569 +459151 +480028 +454775 +507039 +516083 +543094 +565729 +769481 +750866 +898808 +730090 +748705 +731620 +754492 +735812 +740712 +744338 +752331 +1127581 +1144100 +1059177 +1493043 +865846 +920316 +975234 +934803 +1683508 +961814 +1072768 +1291799 +1108823 +1295819 +1461710 +1669021 +1467432 +1472332 +3145218 +1475958 +1476524 +1672647 +2020991 +1496669 +1618177 +2435899 +2168000 +2396274 +1786162 +1800649 +1910037 +2368587 +1896617 +3455183 +3145545 +2181591 +2400622 +2404642 +2757529 +5891082 +2939764 +2943390 +2948290 +2952482 +2972627 +2973193 +3114846 +3393286 +3406706 +5807328 +5730156 +5120482 +8755618 +12162324 +3697266 +6098027 +7626773 +5705819 +4582213 +4586233 +4805264 +5352932 +10599400 +8068772 +11828183 +5883154 +9212873 +5900772 +8198550 +7920110 +6088039 +10389541 +20231096 +7103972 +11435975 +8279479 +12865712 +11617376 +8283499 +8502530 +13551482 +9168446 +9387477 +10688418 +9391497 +32059279 +19938505 +14081704 +11783926 +15024082 +11971193 +11988811 +13004744 +13192011 +14008149 +15606502 +21825516 +15383451 +22472344 +25792075 +21171403 +17890007 +24809387 +22360457 +27273715 +18555923 +18559943 +33160762 +20079915 +32914089 +23755119 +23772737 +23960004 +24788670 +29391600 +30989953 +36480493 +30894751 +23278925 +36554854 +33943394 +33273458 +33939374 +38635838 +63904042 +36445930 +42699394 +37115866 +38639858 +47238929 +59759418 +48543789 +69534609 +48748674 +47034044 +47051662 +47732741 +54180270 +48067595 +64168209 +69753951 +57218299 +118084312 +56552383 +67882768 +95188221 +67212832 +81672115 +101214314 +93664229 +79815260 +75755724 +103604045 +95595451 +123927627 +104619978 +115280427 +96816269 +94766785 +104269961 +94784403 +101913011 +102247865 +105285894 +113770682 +123765215 +220332177 +199808199 +124435151 +135095600 +142968556 +174582045 +155570984 +172571993 +170522509 +199404381 +170540127 +189551188 +190362236 +191600672 +191583054 +218532000 +293830919 +229862385 +196697414 +207198905 +301652246 +366182717 +219056576 +237535897 +248200366 +290666584 +418340199 +367219923 +345933220 +298539540 +326093493 +326111111 +388280468 +362123181 +528401925 +360091315 +379913424 +381945290 +383183726 +403896319 +425730905 +579881140 +426255481 +415753990 +908315349 +538866950 +686202426 +467256942 +563629390 +636480834 +693331034 +624633033 +708056401 +989360295 +1006578323 +944155915 +814011373 +795667414 +722214496 +808914631 +1005612045 +1075276324 +1147945401 +787080045 +819650309 +893512423 +883010932 +1430270897 +1030886332 +1006123892 +1091889975 +1642604726 +1188262423 +1200110224 +1814526676 +1655519365 +1702661241 +1503723815 +1509294541 +2649183049 +1697022305 +1517881910 +1582747459 +2386734747 +1888622977 +2522311550 +1606730354 +2019760533 +2590534728 +1713162732 +2230996556 +1889134824 +2673491058 +2709404765 +2509847707 +2280152398 +2691986238 +3323821217 +2703834039 +3086471274 +3013018356 +3279769764 +3021605725 +3027176451 +3214904215 +3886882752 +3100629369 +5603973615 +4099897479 +3319893086 +4707359723 +4303697460 +6332911442 +8818877830 +4398982531 +4581121062 +6987512121 +4953643456 +4790000105 +5213681746 +4972138636 +7613886746 +5716852395 +5725439764 +6034624081 +9661003179 +6048782176 +7404326829 +6127805820 +6315533584 +9288480785 +10338321541 +7419790565 +8889897584 +11546593188 +8702679991 +12201193867 +9371121167 +8980103593 +11853245584 +9534764518 +9743643561 +9762138741 +18178378369 +10185820382 +10688991031 +11442292159 +15013920549 +19195767697 +12162429901 +15709785355 +18905885685 +12443339404 +15017703404 +15018213575 +16122470556 +16309688149 +16399894158 +18446323552 +17682783584 +18073801158 +22131283190 +18351224760 +18723747154 +19947959123 +19296903259 +20451129772 +35305779843 +20874811413 +28259621540 +22851420932 +31327391553 +24605769305 +33741450558 +27180133305 +37149496765 +35468833176 +27461042808 +51424234142 +53657004603 +32432158705 +32709582307 +46983368694 +35756584742 +36034008344 +36425025918 +56631396155 +37074971914 +38020650413 +54641176113 +45480580718 +41325941185 +53584393720 +50031554237 +47457190237 +50312463740 +89063554860 +58347219863 +60921583863 +74054658757 +60170625115 +87849260060 +63217627550 +106486817462 +103615947957 +129175201245 +68743590651 +91605044133 +71790593086 +77359949529 +79346591598 +98191275528 +154822671683 +83501231131 +94910334905 +86806521903 +105804410100 +97488744474 +97769653977 +116200780888 +110483088855 +118517844978 +119268803726 +129665174514 +156706541127 +139517216713 +131961218201 +135008220636 +234066490656 +180975897486 +189796319661 +223018447844 +189305641231 +149150542615 +162847822729 +177537867126 +170307753034 +266969438837 +199702012019 +203007302791 +184295266377 +195258398451 +207971833329 +378279586363 +226683869743 +237786648704 +248183019492 +261626392715 +264673395150 +288667759328 +271478434914 +315984118122 +365566151485 +434326257643 +330126440101 +311998365344 +319458295649 +347845620160 +604634010677 +361833133503 +354603019411 +387302569168 +464375407169 +503753562026 +398265701242 +550294152043 +403230231780 +753784553292 +464470518447 +526299787865 +576671760494 +780454636569 +666601384755 +553341154478 +706760864817 +583476800258 +627982483466 +677972060261 +649584735750 +966467144180 +674061315060 +709678753663 +702448639571 +716436152914 +741905588579 +752868720653 +785568270410 +1126965912537 +801495933022 +1041142278941 +867700750227 +929530019645 +1175884523615 +990770306312 +1079640942343 +1130012914972 +1277567219216 +1502004423324 +1136817954736 +1211459283724 +1233061536008 +1776338576722 +1323646050810 +1352033375321 +1376509954631 +1843402065451 +1782089581914 +1418884792485 +1458341741493 +2287572693734 +1538436991063 +1587064203432 +1842638211963 +1669196683249 +2252601562665 +1858471056539 +1920300325957 +2862083041873 +3166284262773 +2209653857315 +2348277238460 +3363402780154 +3991743439229 +2488851330057 +4720554098412 +3512598748700 +4638421618595 +5350934371930 +3005948995917 +2795394747116 +2877226533978 +2957321783548 +2996778732556 +4464290737410 +3125501194495 +3207633674312 +3256260886681 +4653865803655 +3527667739788 +6848075475910 +5345055971016 +4129954183272 +4557931095775 +4698505187372 +4837128568517 +5604538125141 +8347713104486 +5284246077173 +5366077864035 +6524446472344 +5672621281094 +5752716530664 +7657621923060 +5792173479672 +5834548317526 +9844854208466 +8409747271668 +6253039619237 +7824006381867 +14182261422012 +11200626181561 +7954766074053 +10162469220916 +10654400655616 +8687885279047 +8828459370644 +8967082751789 +9256436283147 +9535633755889 +10970615989176 +10650323941208 +10956867358267 +14077046001104 +11038699145129 +24027115630478 +15517475489560 +11544890010336 +13910661542297 +11626721797198 +12087587936763 +14207805693290 +20460845312527 +14940924898284 +19923950110056 +16642651353100 +16783225444697 +16921848825842 +17516344649691 +17944321562194 +17654968030836 +17795542122433 +18223519034936 +18792070039036 +20185957697097 +27681350498229 +21607191299475 +22583589155465 +22665420942327 +23126287081892 +28548570623040 +23171611807534 +50852962305763 +33705074270539 +33564500178942 +26295393630053 +29148730591574 +31583576251384 +31724150342981 +40642631731583 +33425876797797 +34438193475533 +39123535949166 +45791708024219 +35450510153269 +45476892620662 +36019061157369 +38409476732033 +38978027736133 +41793148996572 +44190780454940 +57057701452744 +58602650312834 +54709863333276 +62104314818025 +49467005437587 +52320342399108 +55444124221627 +60000467900592 +58019543973034 +57878969881437 +60732306842958 +69888703628802 +65150027140778 +75080825207116 +68876386951066 +93687891069409 +128491353941636 +73859986885302 diff --git a/solutions/9/python/main.py b/solutions/9/python/main.py new file mode 100644 index 0000000..f863b47 --- /dev/null +++ b/solutions/9/python/main.py @@ -0,0 +1,50 @@ +from os.path import join, dirname +from typing import List, Optional, Set, Tuple +from itertools import combinations + + +class XMAS: + + def __init__(self, data: str, size: int) -> None: + self.size: int = size + self.position: int = size + self.buffer: List[int] = [ + int(x) + for x in data.strip().split('\n') + ] + + def check_next(self) -> bool: + possible = [ + a + b + for a, b in combinations(self.buffer[self.position - self.size: self.position], 2) + ] + return self.buffer[self.position] in possible + + def find_first_invalid(self) -> int: + l = len(self.buffer) + while self.position < l: + valid = self.check_next() + if not valid: + return self.buffer[self.position] + self.position += 1 + raise Exception + + def find_slice(self, target: int): + l = len(self.buffer) + for n in range(l): + for m in range(n, l): + slice = self.buffer[n: m] + if sum(slice) == target: + return min(slice) + max(slice) + raise Exception + + +data = join(dirname(__file__), '../data.txt') +with open(data) as f: + xmas = XMAS(f.read(), 25) + + first_invalid = xmas.find_first_invalid() + print(first_invalid) + + solution = xmas.find_slice(first_invalid) + print(solution) diff --git a/solutions/9/test.txt b/solutions/9/test.txt new file mode 100644 index 0000000..28d66e4 --- /dev/null +++ b/solutions/9/test.txt @@ -0,0 +1,20 @@ +35 +20 +15 +25 +47 +40 +62 +55 +65 +95 +102 +117 +150 +182 +127 +219 +299 +277 +309 +576