commit 180a5ea19c0f72a6947bb6766c0cd5b0e9a82f21 Author: Zynh Ludwig Date: Sun Dec 1 11:12:21 2024 -0800 day1 diff --git a/day1/input b/day1/input new file mode 100644 index 0000000..ec0133e --- /dev/null +++ b/day1/input @@ -0,0 +1,1000 @@ +77442 88154 +71181 76363 +49755 69158 +38798 22488 +89857 49242 +96168 60034 +68503 43760 +52053 29173 +27070 63754 +78107 13305 +18422 95805 +34555 59209 +52260 83075 +35963 65961 +92308 56524 +25352 19586 +87921 76287 +21112 20450 +40251 20187 +66316 47227 +10872 77651 +78694 67481 +80223 15216 +69732 81168 +38939 38777 +34636 24551 +10066 49751 +21755 21498 +59907 37042 +40042 20430 +10479 30302 +92724 56653 +49261 18139 +14450 41684 +20138 54577 +49196 76287 +78226 77472 +55352 47752 +73181 76363 +88373 44539 +55224 13454 +78343 39913 +50215 26340 +93394 81365 +12421 17637 +30218 22488 +60899 27070 +53443 63193 +78769 76287 +34837 64955 +57703 90260 +84563 61551 +95968 61321 +18237 59051 +88136 22597 +75094 61551 +14017 58846 +18973 79651 +45202 81168 +67888 36454 +58743 65364 +65978 66336 +27877 47227 +23703 90563 +63594 76363 +19101 52928 +49192 16770 +78677 33277 +74303 76324 +56874 47227 +90161 64736 +39443 58067 +59419 76363 +16790 61551 +23505 93360 +70298 20187 +64391 61321 +62270 61551 +12823 57714 +83131 23208 +29372 81168 +21121 63648 +22012 74365 +42673 20187 +81026 63754 +96443 74839 +30527 16770 +88227 80574 +43550 81327 +83170 87067 +97480 59239 +57306 33168 +12562 19586 +70051 69573 +23409 59209 +98618 36335 +55824 77218 +26417 29173 +47973 28702 +88458 23040 +78792 26612 +49946 81708 +80977 82477 +64506 76324 +51584 19586 +74305 17617 +29886 13430 +66682 81168 +25165 48749 +58380 53467 +69518 61551 +13253 54169 +47096 90256 +30095 92342 +53753 74679 +18402 68884 +96146 54906 +78431 82348 +58498 30348 +48373 28719 +84718 49196 +60072 54268 +82045 81168 +11287 73806 +37090 34498 +38432 59686 +13148 69158 +29704 68950 +45645 76324 +68457 60034 +66349 78117 +72212 54196 +16845 53315 +49997 16770 +56793 64874 +27128 43619 +92355 71300 +76013 43778 +20620 81868 +23056 37042 +82567 61327 +13147 79444 +70591 58074 +68310 63193 +36484 52913 +97410 78373 +31951 44983 +65334 30896 +14656 59209 +91768 61551 +56457 27850 +57141 42263 +18561 31760 +75233 81868 +73250 95805 +14002 12467 +42157 47227 +34516 26033 +55035 81168 +13086 73119 +43301 41775 +38864 65779 +95169 31728 +73367 69158 +53500 63193 +81688 63754 +93934 95142 +54841 30896 +66614 76324 +52867 34526 +92062 65386 +74176 79531 +75046 76363 +64084 28284 +17516 91401 +58314 14259 +30184 48803 +82359 33277 +83956 16770 +87246 96540 +66231 34404 +42526 87428 +96228 96903 +70702 63193 +72108 20187 +62377 59209 +72553 10468 +57600 56860 +16563 57459 +35342 33277 +29419 27070 +22242 37042 +81264 45944 +27706 18763 +11777 17623 +96931 30714 +27765 80535 +65779 66518 +82921 19432 +87744 43619 +85543 40341 +56196 81132 +34774 97741 +93073 61787 +37699 62712 +45701 81380 +60801 76363 +66024 25646 +38321 50162 +32244 10468 +95866 29173 +91365 37520 +96047 11889 +60690 63193 +40819 47227 +19229 63551 +56978 27070 +84539 43393 +50724 59209 +90639 67420 +70374 28146 +83465 27150 +67309 61321 +56678 42545 +91876 78117 +11246 81868 +66054 83337 +40614 69158 +56899 37042 +65744 20551 +80749 37042 +74619 10468 +60988 76287 +84866 60034 +45318 49196 +14541 60910 +79263 35253 +48847 58067 +73082 24937 +79835 48742 +62446 39600 +62036 62719 +13498 71733 +18172 81168 +35258 59209 +60068 38731 +43622 33109 +23059 12770 +61060 43807 +45296 76363 +31310 72036 +69945 10468 +89043 62946 +44661 96601 +32843 10468 +83953 61321 +44774 43897 +47310 48153 +96064 35936 +79098 27177 +11333 76324 +31875 27736 +92000 60034 +60969 96903 +37597 73631 +97194 20187 +90624 70413 +15881 59845 +29211 22488 +19865 95805 +37093 27070 +85746 17347 +88886 46767 +94576 81868 +59784 19770 +98263 75790 +47612 62157 +27380 91120 +33552 16758 +72012 80611 +18549 85133 +23250 70963 +43410 85770 +55986 33277 +62202 20187 +86252 81868 +76363 81168 +39103 67989 +94626 14656 +11792 43619 +62872 29173 +66617 16770 +76923 37042 +54667 22488 +80651 76324 +24898 98262 +78913 43619 +86226 39913 +41730 29173 +62228 39913 +20570 14090 +48070 61321 +89609 73535 +21233 69158 +67520 27006 +69207 66474 +80773 76363 +98652 82728 +77753 65779 +61668 40001 +49504 65779 +90868 87612 +50836 27501 +34661 98114 +36590 31091 +29788 95805 +36309 46751 +45305 33147 +77866 89262 +26091 95805 +73725 75617 +89578 71004 +62919 78373 +18631 61551 +65733 91526 +63976 31808 +57363 54799 +63193 35481 +93512 11640 +22455 32469 +12710 33277 +80568 61321 +82907 31518 +70300 76363 +65593 58067 +66452 68846 +81168 19586 +81230 95805 +15461 78373 +99354 27070 +97780 16770 +11597 36795 +86142 60034 +22929 58067 +72577 16770 +87562 11734 +15882 17423 +18782 78117 +21443 53759 +97662 66320 +26467 10468 +12181 60034 +99202 76363 +92349 72225 +20986 25419 +85821 58067 +70172 92060 +54212 59209 +42712 76324 +90995 90226 +62165 20187 +70665 81168 +23358 63754 +82477 84396 +71987 23192 +95504 51191 +40799 41278 +35361 95805 +10730 51728 +43173 61551 +28181 33277 +23939 35592 +38685 60717 +36349 88861 +38913 47227 +36681 54470 +34231 80208 +84150 32348 +53592 31390 +90048 13692 +56334 66336 +92955 73054 +26500 20187 +11840 75286 +23430 42085 +79148 63193 +60469 19586 +15750 66336 +51639 16201 +66378 76287 +33011 19586 +74383 18079 +13342 71848 +43984 27950 +78415 45783 +37553 58067 +86775 68642 +34408 60406 +53067 58239 +83096 30991 +59779 47641 +92173 31090 +93789 84819 +34804 35005 +86853 98556 +19023 69269 +24014 78373 +88189 76324 +96897 80598 +71969 16770 +57758 60034 +84236 19912 +39543 74542 +45629 61518 +46405 23011 +88155 56357 +39913 69880 +58055 19586 +67449 33277 +97296 41994 +11979 74945 +97896 11294 +16406 47227 +33210 16770 +22366 19586 +14047 99638 +62524 35305 +85153 11167 +35374 52130 +76324 97224 +50715 66545 +30446 95805 +75098 54539 +60654 14555 +92593 10468 +37954 23752 +67845 49196 +18665 95058 +46953 20187 +22488 13898 +76756 95805 +52973 20187 +88681 61321 +67431 37042 +58093 59209 +61522 58067 +19692 22314 +55686 78373 +75663 88193 +75619 70326 +83139 34779 +96583 66996 +76437 13367 +19832 35599 +32727 43276 +64900 37042 +99403 70388 +20217 83512 +68879 39957 +55020 39913 +22732 61321 +70441 48560 +35671 30896 +87206 77391 +15719 65627 +70843 68981 +80802 77969 +60385 54937 +68163 27874 +51564 60034 +14231 59293 +10468 69158 +58531 33277 +89365 96591 +65598 49196 +30226 67781 +13423 29173 +43242 87650 +45581 39913 +55879 81168 +28959 84631 +67482 78373 +21358 13041 +40107 60034 +56957 66336 +68981 42504 +45865 67267 +83968 32642 +36082 29173 +51895 92287 +97489 19909 +13164 23658 +43619 32643 +80054 68277 +43543 10468 +21603 54219 +29392 81168 +79097 91391 +91813 76324 +27744 43619 +66902 41779 +84610 27070 +63644 23335 +63408 68981 +71224 33277 +48293 47227 +57365 48719 +13590 33277 +34822 59209 +90168 71625 +88698 88553 +59044 71011 +64826 60877 +46286 85678 +49565 52138 +89303 29173 +57427 76287 +84546 75928 +68856 99211 +44748 59209 +18252 70479 +32823 91526 +36238 56724 +34436 69158 +25905 83377 +11310 49196 +71865 29173 +31257 41046 +82139 76324 +66945 47714 +23390 33277 +95805 81168 +53931 19586 +46434 95805 +75144 41705 +88419 10648 +99702 63193 +38898 53110 +83475 59209 +57700 37042 +42255 16770 +68559 69158 +48622 30896 +48063 60034 +36896 58465 +61761 38881 +45534 23509 +84197 40976 +10741 60540 +57029 78300 +61058 76363 +16955 76324 +95154 29173 +20941 43072 +76420 10654 +57350 57933 +37339 33049 +11022 33630 +11086 39913 +64945 97536 +95125 27070 +70646 78373 +70553 20187 +52129 76287 +35583 76287 +52407 32158 +21537 45488 +49682 24598 +20290 47227 +66261 44764 +90484 31261 +25358 29173 +59209 88778 +21520 49196 +46692 41896 +83918 13692 +68641 78373 +93012 68295 +10186 63414 +84383 19586 +33576 99070 +42089 59209 +76287 61321 +63821 55986 +17944 38193 +57972 81168 +51596 61321 +22418 95805 +68055 76287 +19554 29173 +59117 14656 +17210 65375 +61968 34453 +22768 44020 +22419 92236 +33546 83847 +34755 58136 +17956 31634 +19543 59209 +85916 34705 +66761 93059 +30911 50479 +12158 32771 +91771 29981 +87708 60034 +70837 67717 +27584 78373 +67642 61551 +62273 19018 +16352 61551 +41901 29173 +25732 81298 +75816 29212 +25681 63754 +86396 12643 +44190 56820 +58070 19742 +44532 83582 +47227 35357 +36638 44159 +15632 76363 +72120 47309 +32764 47227 +51180 78117 +49739 14656 +66274 78739 +61723 43619 +59015 19586 +21891 85648 +18747 61551 +86144 58938 +39681 69158 +38150 68516 +61321 10468 +38754 30896 +70627 83718 +91724 74971 +91330 29173 +38159 73587 +78498 78373 +24641 61551 +12550 83211 +70711 83416 +17338 65779 +56076 39000 +21353 27070 +61551 85619 +42398 85167 +80835 63559 +69604 71196 +76284 81168 +73817 65125 +35170 23254 +52139 95805 +40826 81168 +85479 46622 +56430 33363 +67747 31755 +25739 13587 +17558 39762 +89499 93764 +78373 73981 +95268 46534 +33958 28798 +12195 66209 +63380 76287 +60034 27070 +80467 76324 +98506 95805 +58067 99459 +75075 60524 +43692 26965 +17039 37042 +97439 76324 +79297 29173 +68473 35985 +47630 17932 +61662 58771 +86027 39235 +62403 81835 +24100 54006 +87270 47227 +35793 39913 +91493 19398 +55623 19898 +82673 78117 +15863 91070 +57779 66587 +45405 49196 +93780 61551 +99320 65779 +40036 47227 +33370 83360 +82692 90756 +78117 19586 +68580 64705 +57478 69158 +15588 64663 +73700 39913 +81481 78593 +49442 28165 +61061 20187 +37624 78760 +90983 30919 +60057 61171 +80319 60632 +99544 63754 +14572 32678 +35131 83311 +90273 78373 +19621 19897 +88549 43398 +90170 60034 +35057 61321 +34283 61551 +24485 16250 +41063 95805 +47154 45332 +87986 16770 +36053 29173 +20687 60034 +66575 30896 +81371 95805 +91665 78117 +51369 51581 +19220 59209 +45158 39913 +61595 98095 +79096 75715 +72662 59209 +10875 44090 +99736 16480 +85897 65779 +83776 52163 +40755 19553 +80969 76363 +53053 76363 +58468 76324 +68895 30896 +10193 19586 +51578 42226 +59446 27070 +98969 73952 +15913 88063 +95191 69158 +27476 27070 +94851 58067 +47345 56070 +35791 59209 +56194 62759 +71120 31064 +42599 76860 +33848 20187 +68814 60034 +25584 51942 +85338 71636 +77668 99601 +82555 35041 +44609 16770 +73386 61321 +30553 29173 +35913 76363 +70145 95805 +99873 19236 +32644 24024 +15974 19406 +70045 47227 +37614 14925 +70738 66336 +96972 69158 +71948 76363 +18962 63193 +22983 27070 +50419 60034 +92422 18415 +29173 94253 +17365 35447 +76649 65307 +72431 40771 +46431 45281 +25817 42605 +83620 61152 +83434 76363 +25031 24195 +31968 42631 +31604 71439 +94886 72284 +88754 27196 +14709 25431 +40906 27070 +89846 44931 +29736 78373 +62109 76363 +41649 82052 +13672 35541 +58161 31191 +63752 55104 +22495 47227 +55699 81868 +69083 14656 +20079 36929 +34505 27070 +70644 33277 +18606 80975 +90720 65841 +33445 14656 +66398 76324 +22680 47227 +27166 43619 +94319 27070 +19586 62041 +51374 52608 +28183 59545 +49601 60267 +45765 51736 +57265 29173 +77954 77288 +81735 27070 +17145 10232 +62809 52565 +38935 53430 +26249 78373 +86870 42850 +49988 91893 +57756 60034 +69866 85817 +38276 95805 +65159 78373 +88269 78117 +18994 47227 +89894 88633 +19078 27070 +13692 58448 +34577 76324 +93672 39465 +66156 33277 +52761 75778 +71038 95805 +59876 23131 +95962 85623 +33277 13436 +22237 76435 +42882 27070 +42825 27070 +44396 19594 +89726 78373 +16770 65779 +44209 15242 +98471 40691 +63754 30896 +52305 55759 +21085 35037 +84268 33277 +23150 60228 +60140 62491 +96903 10468 +94329 75565 +26226 36931 +74515 63754 +68426 57490 +36722 76287 +91076 59209 +10149 82403 +49208 56685 +64396 93619 +35021 76363 +10780 30896 +86680 54460 +65532 35338 +35181 83328 +85405 10723 +91632 63754 +75023 72435 +37042 63193 +53779 19586 +37585 39913 +23603 90041 +88436 83914 +69158 68981 +70402 94227 +44083 65227 +82394 14656 +27746 16770 +19624 45804 +97957 71000 +30896 28091 +78374 26479 +66971 16770 +96748 49196 +47956 30896 +84663 80516 +89157 82477 +12228 65947 +42362 53557 +93195 51572 +75162 89269 +45009 61551 +39320 49305 +67273 41190 +65163 30289 +18734 74713 +73297 82175 +91431 13090 +14963 46180 +71185 16770 +42321 48403 +64599 67069 +53334 34468 +20187 57178 +83616 10468 +66914 20187 +81279 20187 +99419 37042 +79294 31121 +28220 76363 +80676 61551 +24797 25016 +35108 35246 +65643 66828 +36523 57465 +33040 46127 +81868 23412 +53306 90826 +47740 15732 +37843 22642 +98695 87173 +51321 38561 +95514 61551 +26419 35706 +20382 64457 +18337 76324 +24862 42585 +96800 57680 +88316 78955 +93747 40607 +89689 78373 +86418 13271 +37119 98421 +98276 61657 +37447 56593 +41319 41419 +43040 40781 +44573 69158 +93294 85174 +37495 99697 +33107 16770 +54548 62282 +54445 80876 +88038 88090 +66613 65779 +64729 18816 +81937 14656 +86560 49196 +90763 79218 +10930 80489 +52132 37042 +93840 96903 +14381 46862 +23038 59209 +12809 69134 +28706 53283 +35811 47227 +64368 16770 +14652 55002 +24290 95805 +35618 61879 +76669 65779 +65052 63193 +27533 84394 +37756 60034 +28054 78373 +56969 59374 +83129 27070 +81613 16770 +75625 78373 +17846 70557 +77897 20442 +93086 30896 +73969 96869 +87726 16313 +88858 78373 +65550 37042 +60065 61551 +91526 59209 +15647 20187 +40208 60034 +45614 81345 +56409 48982 +14733 73493 +50641 83354 +60500 30643 +66336 39913 +52887 80280 +72997 39913 +44985 61551 +79421 34868 +36332 36878 +69182 95805 +59092 67571 +29363 98020 diff --git a/day1/part1.lua b/day1/part1.lua new file mode 100644 index 0000000..b0867c5 --- /dev/null +++ b/day1/part1.lua @@ -0,0 +1,51 @@ +-- see if the file exists +local function file_exists(file) + local f = io.open(file, "rb") + if f then + f:close() + end + return f ~= nil +end + +-- get all lines from a file, returns an empty +-- list/table if the file does not exist +local function lines_from(file) + if not file_exists(file) then + return {} + end + local lines = {} + for line in io.lines(file) do + lines[#lines + 1] = line + end + return lines +end + +-- tests the functions above +local file = "input" +local lines = lines_from(file) + +local total = 0 + +local left_list = {} +local right_list = {} + +-- https://www.lua.org/manual/5.4/manual.html#pdf-ipairs +-- print all line numbers and their contents +for _, v in ipairs(lines) do + for n1, n2 in string.gmatch(v, "(%d+)%s+(%d+)") do + -- [](https://stackoverflow.com/a/27434198) + -- [Lua Length Operator](https://www.lua.org/manual/5.2/manual.html#3.4.6) + left_list[#left_list + 1] = n1 + right_list[#right_list + 1] = n2 + end +end + +-- https://www.lua.org/manual/5.2/manual.html#pdf-table.sort +table.sort(left_list) +table.sort(right_list) + +for i, v in ipairs(left_list) do + total = total + math.abs(v - right_list[i]) +end + +io.output():write(total) diff --git a/day1/part2.lua b/day1/part2.lua new file mode 100644 index 0000000..4ba1938 --- /dev/null +++ b/day1/part2.lua @@ -0,0 +1,63 @@ +-- see if the file exists +local function file_exists(file) + local f = io.open(file, "rb") + if f then + f:close() + end + return f ~= nil +end + +-- get all lines from a file, returns an empty +-- list/table if the file does not exist +local function lines_from(file) + if not file_exists(file) then + return {} + end + local lines = {} + for line in io.lines(file) do + lines[#lines + 1] = line + end + return lines +end + +-- tests the functions above +local file = "input" +local lines = lines_from(file) + +local left_list = {} +local right_list = {} + +-- https://www.lua.org/manual/5.4/manual.html#pdf-ipairs +-- print all line numbers and their contents +for _, v in ipairs(lines) do + for n1, n2 in string.gmatch(v, "(%d+)%s+(%d+)") do + -- [](https://stackoverflow.com/a/27434198) + -- [Lua Length Operator](https://www.lua.org/manual/5.2/manual.html#3.4.6) + left_list[#left_list + 1] = n1 + right_list[#right_list + 1] = n2 + end +end + +local right_list_count_map = {} + +for _, v in ipairs(right_list) do + if right_list_count_map[v] ~= nil then + right_list_count_map[v] = right_list_count_map[v] + 1 + else + right_list_count_map[v] = 1 + end +end + +local total = 0 + +for _, v in ipairs(left_list) do + local count + if right_list_count_map[v] ~= nil then + count = right_list_count_map[v] + else + count = 0 + end + total = total + (v * count) +end + +io.output():write(total) diff --git a/template.lua b/template.lua new file mode 100644 index 0000000..7a31d77 --- /dev/null +++ b/template.lua @@ -0,0 +1,30 @@ +-- see if the file exists +local function file_exists(file) + local f = io.open(file, "rb") + if f then + f:close() + end + return f ~= nil +end + +-- get all lines from a file, returns an empty +-- list/table if the file does not exist +local function lines_from(file) + if not file_exists(file) then + return {} + end + local lines = {} + for line in io.lines(file) do + lines[#lines + 1] = line + end + return lines +end + +-- tests the functions above +local file = "test.lua" +local lines = lines_from(file) + +-- print all line numbers and their contents +for k, v in pairs(lines) do + print("line[" .. k .. "]", v) +end