diff --git a/2024/day1/.gitignore b/2024/day1/.gitignore new file mode 100644 index 0000000..bdeeb3a --- /dev/null +++ b/2024/day1/.gitignore @@ -0,0 +1,4 @@ +*.out +*.cmi +*.cmx +*.o diff --git a/2024/day1/.ocamlformat b/2024/day1/.ocamlformat new file mode 100644 index 0000000..0619e80 --- /dev/null +++ b/2024/day1/.ocamlformat @@ -0,0 +1 @@ +profile = janestreet diff --git a/2024/day1/Makefile b/2024/day1/Makefile new file mode 100644 index 0000000..246c487 --- /dev/null +++ b/2024/day1/Makefile @@ -0,0 +1,19 @@ +CC = ocamlopt +RM = rm -rf + +NAME = main +EXE = a.out +LIB = unix.cmxa -I +unix + + +all: run + +compilation: + $(CC) -o $(EXE) $(LIB) $(NAME).ml + +run: compilation + ./$(EXE) + +EXTS = cmi cmx o +clean: + $(RM) $(EXE) $(foreach ext,$(EXTS),$(NAME).$(ext)) diff --git a/2024/day1/input.txt b/2024/day1/input.txt new file mode 100644 index 0000000..c059813 --- /dev/null +++ b/2024/day1/input.txt @@ -0,0 +1,1000 @@ +77710 11556 +22632 23674 +82229 77288 +35788 30924 +84000 63702 +28350 62605 +15185 47495 +59530 63702 +38142 60772 +53694 41728 +38645 53443 +14632 43457 +54935 60772 +80251 67650 +45254 80940 +92045 48739 +88570 62608 +36464 14863 +15072 50428 +47732 12515 +66594 38710 +49622 63960 +38988 83798 +80235 95974 +66138 48396 +24678 63702 +89750 21245 +49637 60225 +66174 33783 +31929 60355 +86217 38710 +63702 64237 +10029 40993 +18724 11247 +86376 33183 +38846 77319 +71807 81452 +95125 65995 +14313 48739 +43968 82639 +95579 67299 +34700 57990 +95295 86925 +77804 39861 +23393 68293 +16675 33218 +86282 15372 +23383 44772 +93238 77732 +48968 55633 +13865 82878 +94041 30924 +58961 57990 +95054 99680 +38556 11247 +49445 49423 +12517 17577 +16458 62967 +70240 11247 +70209 13865 +44469 37707 +44143 76913 +40530 67650 +77107 57609 +52160 33783 +21898 53452 +60672 67910 +78399 38710 +65701 18077 +44093 31621 +10717 89642 +61143 33783 +46421 36377 +25600 86925 +71610 67650 +84942 88880 +18709 63702 +35967 62840 +59430 33783 +99613 11247 +21285 53061 +45961 63960 +64708 61045 +18663 38142 +24076 81982 +65173 33218 +99985 38090 +54288 50656 +45599 57990 +71134 86925 +21756 47991 +61585 96229 +30625 28042 +20187 96905 +53557 36740 +97635 85201 +74537 26837 +47344 89993 +23434 81337 +22578 52652 +64334 95857 +31984 23357 +19285 91889 +89380 60225 +68178 55027 +60534 92951 +92947 36269 +19390 21611 +36751 61641 +46265 86925 +64682 62605 +82939 21611 +62893 68731 +80218 33783 +82578 47062 +38606 47991 +64066 32805 +73995 96279 +59980 81452 +35206 49067 +63735 22549 +69054 43457 +39028 33503 +47551 63960 +23082 33555 +46069 99239 +25862 21611 +29188 80530 +80159 72409 +89316 15281 +54325 96229 +20285 94652 +70669 60225 +38999 72702 +85965 38803 +29799 32822 +59668 67650 +51559 66111 +13066 40993 +83004 30924 +10791 93513 +49496 38142 +73531 13865 +69050 96905 +69139 86925 +38483 67650 +30305 30924 +81076 76125 +15160 86181 +16189 56515 +22900 81452 +73271 82195 +45974 47969 +36115 10740 +59422 43457 +85666 59898 +82750 60225 +40947 99239 +75465 61804 +54504 94607 +62259 57990 +61506 76849 +93497 89296 +86487 26837 +41882 90643 +88793 12950 +39763 99728 +93555 26837 +12393 94358 +74543 62605 +88378 13865 +50221 21611 +27299 21611 +11114 69673 +65791 40954 +83752 25848 +18745 66574 +74882 15777 +48692 92817 +44362 33783 +61242 33713 +75950 97325 +57627 78921 +27136 37764 +39092 75180 +57990 47495 +61045 38333 +92827 42676 +24438 68802 +47626 96905 +37175 21756 +73009 41330 +96229 91479 +11873 81197 +25332 28939 +93039 26837 +44499 54251 +19265 33218 +31294 48739 +21611 85477 +47571 35343 +41758 81197 +89804 57990 +48279 92182 +94594 66451 +57016 89158 +17266 86925 +81813 58727 +13336 38200 +94562 21611 +38748 27800 +43566 38115 +33641 16072 +32705 13865 +91130 23928 +25131 25225 +36909 58299 +42651 60225 +15671 86925 +55940 79543 +54192 93612 +35033 46154 +45535 93612 +67381 33783 +19212 43554 +61797 57334 +31432 63702 +67624 63702 +68653 21925 +68954 19906 +59967 19319 +92360 62605 +62287 81197 +45573 67650 +36328 86925 +61960 54935 +52269 30924 +29735 38710 +17036 86925 +41095 43262 +78536 69197 +66416 38710 +16855 26837 +34731 88069 +87905 57921 +94906 38142 +51691 60772 +23074 40993 +21975 30538 +98269 63247 +18510 11247 +75157 81452 +81502 96229 +42393 40392 +33218 59424 +27267 81197 +79787 64276 +93536 44879 +48011 86126 +31968 37764 +29136 93822 +31415 38142 +82576 57290 +52268 76837 +92810 86925 +72140 30880 +53488 77275 +92207 29581 +87280 30659 +37835 33783 +13844 33707 +36050 15281 +99005 47991 +28786 38142 +47468 63702 +25603 42097 +26727 78314 +49976 20189 +62921 45423 +52044 21611 +17790 89889 +88780 18585 +34331 38142 +67726 74641 +57916 47495 +63452 79115 +78967 39701 +94997 37764 +42030 96311 +60590 30659 +78384 86925 +92241 99239 +51851 98205 +35112 11955 +94174 93612 +56105 34772 +82931 83183 +85755 21756 +41762 11247 +29979 30538 +64165 50082 +70021 82923 +59604 50033 +13201 97846 +50277 79961 +50619 21580 +64368 62605 +83879 71346 +27839 21580 +26011 16779 +69345 93082 +41187 45082 +24601 57403 +89017 39275 +13283 63960 +31256 13865 +78623 40494 +39962 60772 +77663 75759 +26038 96905 +73316 33938 +92999 63310 +70166 81452 +76098 13228 +14891 73495 +28141 90952 +39299 28133 +15281 73967 +64842 69333 +95559 89761 +17538 13099 +76799 89172 +22138 21687 +49158 40993 +33693 62605 +55981 94587 +83142 79932 +21077 85487 +23175 30659 +80364 52407 +42472 57021 +29236 63702 +14255 81197 +38418 38658 +51859 99239 +45882 24944 +61324 62605 +61841 14654 +80388 49151 +29747 37942 +72476 85550 +16252 35104 +29781 40993 +44328 57990 +54660 26837 +59197 60225 +39215 26403 +29033 87810 +74751 13865 +11363 81452 +45067 13865 +69924 75263 +58362 14620 +99338 97975 +75898 43457 +96905 16625 +70983 76517 +14236 74084 +96360 24647 +86881 50855 +26274 21580 +40600 30924 +58564 40993 +99226 35194 +87750 47107 +97441 60772 +35567 84318 +21972 77712 +78537 60772 +67606 33783 +87472 54880 +40590 39337 +65012 40993 +34228 86925 +67523 60772 +59292 92825 +58678 51248 +84088 74861 +15637 67061 +84670 34083 +65027 35840 +36693 78427 +13907 63494 +21482 63960 +93441 20115 +52171 52940 +19489 81197 +15078 62605 +83077 30659 +63960 60225 +46050 80683 +38700 96905 +63198 38142 +41302 48739 +68920 57324 +45369 27878 +80613 35343 +42072 18174 +72266 67650 +33109 59561 +27044 94017 +58397 99239 +99287 26837 +75188 26837 +35603 96905 +72118 64189 +52217 89498 +22261 15916 +47554 64893 +37504 93612 +37585 81197 +57160 99788 +58979 86925 +86068 17478 +30924 67650 +91757 33783 +41612 62605 +49882 58770 +58123 91694 +19042 25712 +74025 54935 +72215 80755 +59251 94914 +51699 82641 +38983 81452 +52780 27519 +19446 23721 +44414 93461 +10848 63498 +13269 77958 +55135 86925 +94862 96905 +61608 71892 +32466 63702 +73641 77222 +88895 60772 +30659 43691 +79642 26837 +14338 62605 +64448 98623 +56553 26837 +40861 95000 +27892 38142 +66064 56112 +27121 67650 +47531 38710 +25848 38142 +14271 82868 +10228 60225 +87793 84307 +47175 11247 +17169 21756 +27721 32097 +92418 56108 +66355 95179 +44735 14992 +56507 47495 +28974 30659 +25283 47495 +49106 30659 +61397 20474 +57660 37764 +95424 82164 +10121 68532 +49037 21756 +16018 54935 +54171 21580 +80053 62605 +54412 86925 +43192 98063 +53374 30538 +65521 28042 +95061 23181 +94014 96905 +24774 45834 +28399 48739 +97698 26837 +46147 65487 +84884 13865 +52030 67650 +93282 25692 +79313 96229 +58667 57990 +47096 12206 +87391 13255 +99072 77986 +50703 38710 +87988 66679 +71641 20252 +59311 96905 +71220 26837 +67650 37764 +59553 50172 +31948 78353 +34761 62033 +87610 12483 +31841 10666 +94372 64616 +60219 93612 +22359 20993 +97102 38142 +77624 33783 +24951 60772 +37488 15301 +11723 48739 +39666 30924 +32108 63702 +83851 34362 +96210 93612 +86925 96905 +45896 30538 +65334 17294 +72470 70423 +30711 64283 +41405 14988 +43167 67650 +96945 74134 +86918 21648 +73197 99239 +12883 30924 +68421 66131 +52765 38142 +90834 21611 +45258 64964 +47579 75382 +32277 35346 +71673 46895 +37595 87784 +48519 41837 +29412 96905 +96414 21611 +61644 21815 +81808 62605 +30538 81530 +34537 63702 +93380 28460 +87906 60225 +10146 40993 +47991 40365 +67034 48739 +83743 12925 +10147 37764 +26378 87307 +40198 47495 +67130 54935 +83983 93612 +72931 59552 +95192 32462 +95378 15281 +96684 30924 +95810 94119 +84753 83149 +40769 68570 +17455 63702 +81452 30538 +10731 30614 +45236 60225 +86568 60225 +23497 40337 +51480 19838 +13760 33886 +45686 60296 +96295 75032 +76955 89344 +45913 68223 +87186 11247 +31427 71637 +55276 85526 +38894 67650 +21981 42123 +22349 96229 +57614 21611 +94900 30538 +66311 93612 +55885 89479 +50423 37764 +26215 21076 +48434 60772 +40955 26850 +39151 66713 +70875 94570 +27665 95982 +18538 40993 +13492 67650 +70499 47991 +77683 54750 +67747 30924 +71323 72807 +59706 24034 +38778 40993 +87807 25447 +70172 84749 +26837 38142 +23133 37764 +64159 37764 +52185 69425 +76687 41712 +63845 30659 +35561 13247 +43934 63702 +40181 85421 +32289 96905 +65398 67650 +71139 54935 +73790 47495 +45466 76229 +58332 67014 +61971 72358 +69121 63726 +39542 93612 +22484 68967 +55587 48739 +60432 18975 +94726 64078 +24284 14730 +14909 99239 +90684 11247 +32649 33783 +65137 47495 +77217 91427 +21116 47495 +83895 96905 +96601 27302 +55754 40024 +79944 85355 +16002 66193 +90129 11247 +30213 70372 +78641 83755 +63174 13530 +14543 10536 +90871 21580 +85852 79909 +34592 96905 +10211 33104 +97542 11247 +54594 33218 +63461 64351 +28007 25447 +99493 80756 +37563 33783 +81527 47495 +84654 81452 +18992 19077 +24263 25447 +69870 80090 +72716 33783 +97138 36996 +52068 63702 +29147 96229 +37530 64645 +33783 46150 +74846 60772 +16825 15281 +35345 62605 +69248 63960 +62605 40993 +48393 72178 +18517 63702 +83254 33783 +79495 30659 +66124 23376 +78999 80281 +96072 93612 +23506 12287 +28042 71962 +37764 29122 +27239 77167 +67379 96052 +50343 62639 +65800 37119 +73545 10601 +26147 48018 +11703 11247 +12981 63702 +10852 45862 +28206 37518 +98061 28757 +83811 58066 +23862 67650 +76612 22466 +10665 73928 +30081 85075 +63962 96905 +76010 84994 +27403 67650 +90077 63960 +74068 93612 +59069 67650 +48585 76889 +26308 62536 +94927 25428 +16522 88617 +42895 67650 +23882 25848 +88312 53998 +93856 55795 +37116 62605 +78120 42683 +79015 12949 +60074 54593 +41372 30538 +92369 25447 +46454 10552 +90499 13865 +95683 47495 +26207 15402 +41663 38142 +45778 96087 +82536 26837 +73671 62605 +90830 70767 +98755 11247 +40667 21580 +68269 68306 +27166 38710 +52502 36604 +90650 39860 +60073 67650 +82795 80557 +93494 67650 +61210 75404 +65316 65293 +31264 86574 +77373 98352 +30739 81452 +85346 62605 +66664 21611 +99662 37764 +29578 98074 +57246 60772 +29093 22422 +59393 33582 +69790 62469 +33276 30114 +80189 96666 +66119 26837 +19784 62605 +81184 41088 +35076 33783 +67704 61250 +48739 33218 +55762 13865 +89013 26837 +37137 21611 +88909 61336 +32196 30538 +86724 29523 +17981 54398 +56448 60256 +80823 54221 +83300 27328 +88026 38142 +58336 93612 +65810 77527 +24714 13865 +17929 86925 +11327 77128 +55034 58912 +44769 86797 +13322 55659 +64372 93494 +36477 21611 +23642 60772 +55724 60772 +26667 36225 +45210 96905 +11247 46836 +78918 90137 +13992 47495 +41326 60225 +47903 26837 +35926 77384 +27221 95442 +98695 63702 +93775 43732 +97744 78882 +81628 30659 +60225 63960 +36543 23250 +98904 30924 +79272 76007 +91916 45599 +21068 14578 +44525 49606 +26951 60225 +85562 81197 +79077 93612 +91418 74576 +64125 48739 +25949 65857 +36366 30045 +51836 21611 +59493 30924 +14600 31019 +51609 40993 +56673 93612 +85240 33218 +89574 15281 +30058 15281 +29464 46074 +94282 15086 +28521 53216 +85293 55577 +71182 37764 +14518 19328 +60810 64463 +44155 71652 +50352 30924 +81271 37520 +56005 73595 +93426 99239 +48196 49136 +80048 21611 +99240 42592 +20530 33783 +60772 53594 +43174 36790 +69363 14739 +77407 35483 +37644 60772 +86726 87246 +98832 40993 +26660 79340 +67933 26574 +74474 48739 +62414 49360 +20524 21580 +57665 50944 +49993 62605 +39330 74779 +95735 60225 +54490 60772 +42736 94550 +18812 33783 +83965 71738 +96638 78706 +75468 93612 +23757 37754 +69579 44683 +98297 75616 +49322 34521 +34698 29727 +99972 40993 +52254 74431 +85235 30538 +65740 30778 +52565 47991 +33052 78908 +60013 96229 +83067 99496 +85558 60225 +27244 96905 +72402 26837 +91813 99239 +65555 60225 +35968 25069 +53737 61662 +70451 46554 +78329 73260 +55609 74063 +12781 60772 +66272 49941 +16118 86925 +26033 49617 +48390 63702 +30003 23436 +41542 91300 +76570 93612 +67639 63960 +63930 11333 +22066 36920 +83376 74684 +16104 21611 +35185 56286 +95216 44366 +22605 62605 +80979 30538 +14635 94665 +83619 30924 +92781 98532 +78861 38764 +62770 19341 +39746 25447 +93612 38710 +44092 62605 +77918 96905 +80257 30580 +17590 99272 +68229 76224 +54001 85765 +97469 99239 +37205 30538 +69048 57990 +14872 60739 +48571 99239 +23974 91218 +38710 14364 +31301 26090 +68179 99207 +67300 99239 +33247 34833 +21580 99865 +74274 97554 +20411 47495 +40542 30538 +67917 89131 +99902 40838 +40993 21580 +97436 35819 +23800 60566 +86595 88748 +78666 19083 +47495 41135 +98215 38710 +22482 80507 +77086 67050 +53308 14323 +75294 88625 +82489 29928 +49093 32934 +96378 68073 +37250 71257 +16009 21611 +64164 47014 +69019 47495 +63457 43171 +50592 27922 +46024 70597 +49667 96082 +40856 11247 +42134 99956 +26021 55068 +20318 98162 +71497 13865 +43457 38142 +91041 26837 +16291 60225 +25447 98873 +79232 30924 +61697 96016 +46796 41899 +12977 58039 +62416 65959 +13416 27187 +61371 93612 +89884 50535 +62007 79719 +97904 65662 +69865 57990 +10288 21580 +71558 28042 +55800 30659 +23853 10422 +20471 13865 +20564 18219 +44027 13804 +46965 87046 +76735 64178 +95328 85608 +43478 40522 +26456 96905 +53931 30924 +81197 67359 +18024 63702 +34943 26837 +15279 44819 +91142 63960 +46127 30538 +31830 21574 +17905 63960 +35343 48107 +75727 87321 +77205 25447 +62962 11247 +79177 55811 +33599 83446 +18797 80213 +84721 86925 +50446 21611 +68889 26952 +91051 17166 +35127 29638 +99239 43522 +99122 27157 +29947 39341 +21459 97910 +22063 15281 +63297 43457 +42167 11247 diff --git a/2024/day1/main.ml b/2024/day1/main.ml new file mode 100644 index 0000000..a71012d --- /dev/null +++ b/2024/day1/main.ml @@ -0,0 +1,37 @@ +(** Get input *) +let from input = + let ic = open_in input in + (* Read a file *) + let rec read_line acc = + try + let line = input_line ic in + read_line (line :: acc) + with + | End_of_file -> + close_in ic; + acc + in + (* Split column into couples *) + let scan line = Scanf.sscanf line "%d %d" (fun x y -> x, y) in + (* Split couple into 2 lists *) + let split (acc1, acc2) (a, b) = a :: acc1, b :: acc2 in + let zip = List.map scan (read_line []) in + List.fold_left split ([], []) zip +;; + +let first a b = + let sorted_a, sorted_b = List.fast_sort compare a, List.fast_sort compare b in + let m = List.map2 (fun a b -> Int.abs (a - b)) sorted_a sorted_b in + List.fold_left ( + ) 0 m +;; + +let second a b = + (* todo *) + 0 +;; + +let _ = + let a, b = from "input.txt" in + print_endline ("first: " ^ Int.to_string (first a b)); + print_endline ("second: " ^ Int.to_string (second a b)) +;;