diff --git a/2024/day_1/README.md b/2024/Day1.md similarity index 100% rename from 2024/day_1/README.md rename to 2024/Day1.md diff --git a/2024/day_1/day_1.nix b/2024/day_1/day_1.nix deleted file mode 100644 index e69de29..0000000 diff --git a/2024/day_1/rust/main b/2024/day_1/rust/main deleted file mode 100755 index 6c6515e..0000000 Binary files a/2024/day_1/rust/main and /dev/null differ diff --git a/2024/nix/README.md b/2024/nix/README.md new file mode 100644 index 0000000..f363b50 --- /dev/null +++ b/2024/nix/README.md @@ -0,0 +1,42 @@ +--- Day 1: Historian Hysteria --- + +The Chief Historian is always present for the big Christmas sleigh launch, but nobody has seen him in months! Last anyone heard, he was visiting locations that are historically significant to the North Pole; a group of Senior Historians has asked you to accompany them as they check the places they think he was most likely to visit. + +As each location is checked, they will mark it on their list with a star. They figure the Chief Historian must be in one of the first fifty places they'll look, so in order to save Christmas, you need to help them get fifty stars on their list before Santa takes off on December 25th. + +Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck! + +You haven't even left yet and the group of Elvish Senior Historians has already hit a problem: their list of locations to check is currently empty. Eventually, someone decides that the best place to check first would be the Chief Historian's office. + +Upon pouring into the office, everyone confirms that the Chief Historian is indeed nowhere to be found. Instead, the Elves discover an assortment of notes and lists of historically significant locations! This seems to be the planning the Chief Historian was doing before he left. Perhaps these notes can be used to determine which locations to search? + +Throughout the Chief's office, the historically significant locations are listed not by name but by a unique number called the location ID. To make sure they don't miss anything, The Historians split into two groups, each searching the office and trying to create their own complete list of location IDs. + +There's just one problem: by holding the two lists up side by side (your puzzle input), it quickly becomes clear that the lists aren't very similar. Maybe you can help The Historians reconcile their lists? + +For example: + +3 4 +4 3 +2 5 +1 3 +3 9 +3 3 + +Maybe the lists are only off by a small amount! To find out, pair up the numbers and measure how far apart they are. Pair up the smallest number in the left list with the smallest number in the right list, then the second-smallest left number with the second-smallest right number, and so on. + +Within each pair, figure out how far apart the two numbers are; you'll need to add up all of those distances. For example, if you pair up a 3 from the left list with a 7 from the right list, the distance apart is 4; if you pair up a 9 with a 3, the distance apart is 6. + +In the example list above, the pairs and distances would be as follows: + + The smallest number in the left list is 1, and the smallest number in the right list is 3. The distance between them is 2. + The second-smallest number in the left list is 2, and the second-smallest number in the right list is another 3. The distance between them is 1. + The third-smallest number in both lists is 3, so the distance between them is 0. + The next numbers to pair up are 3 and 4, a distance of 1. + The fifth-smallest numbers in each list are 3 and 5, a distance of 2. + Finally, the largest number in the left list is 4, while the largest number in the right list is 9; these are a distance 5 apart. + +To find the total distance between the left list and the right list, add up the distances between all of the pairs you found. In the example above, this is 2 + 1 + 0 + 1 + 2 + 5, a total distance of 11! + +Your actual left and right lists contain many location IDs. What is the total distance between your lists? + diff --git a/2024/nix/day_1.nix b/2024/nix/day_1.nix new file mode 100644 index 0000000..4dd43e2 --- /dev/null +++ b/2024/nix/day_1.nix @@ -0,0 +1,21 @@ +#{ lib }: +let + lib = import; + filePath = ./example.txt; # Path to the file + fileContent = builtins.readFile filePath; + lines = lib.strings.splitString "\n" fileContent; + filterNonEmpty = word: word != ""; + isEmptyPair = pair: (pair[0] == null || pair[0] == "") && (pair[1] == null || pair[1] == ""); + processWord = word: lib.strings.toInt word; + processLine = line: map processWord (lib.lists.filter filterNonEmpty (lib.strings.splitString " " line)); + calculateDistance = pairs: + let + x = builtins.elemAt pairs 0; + y = builtins.elemAt pairs 1; + in + y - x; + processedLines = lib.lists.filter (list: (builtins.length list) > 0) (map processLine lines); + distances = map calculateDistance processedLines; + sum = lib.lists.fold (a: b: a + b) 0 distances; +in + sum diff --git a/2024/nix/example.txt b/2024/nix/example.txt new file mode 100644 index 0000000..68170ea --- /dev/null +++ b/2024/nix/example.txt @@ -0,0 +1,6 @@ +3 4 +4 3 +2 5 +1 3 +3 9 +3 3 diff --git a/2024/rust/day_1/gen_list.fsx b/2024/rust/day_1/gen_list.fsx new file mode 100644 index 0000000..2ef7740 --- /dev/null +++ b/2024/rust/day_1/gen_list.fsx @@ -0,0 +1,6 @@ +let rnd = System.Random() +[1..1000] +|> Seq.map (fun i -> rnd.Next(0,1001),rnd.Next(0,1001)) +|> Seq.map (fun (a,b) -> sprintf "%i %i" a b) +|> String.concat "\n" +|> printfn "%s" diff --git a/2024/rust/day_1/input/custom_random_input.txt b/2024/rust/day_1/input/custom_random_input.txt new file mode 100644 index 0000000..b656582 --- /dev/null +++ b/2024/rust/day_1/input/custom_random_input.txt @@ -0,0 +1,1000 @@ +29 36 +655 594 +52 245 +129 877 +408 357 +972 296 +364 692 +250 105 +883 557 +308 932 +311 572 +120 888 +882 575 +35 751 +222 342 +335 117 +755 708 +709 963 +18 146 +266 472 +164 936 +260 31 +549 753 +375 38 +334 252 +373 281 +591 44 +508 864 +60 626 +754 43 +623 343 +921 667 +487 776 +656 638 +428 867 +76 255 +342 214 +365 883 +69 333 +342 65 +343 868 +554 232 +98 995 +67 312 +258 114 +91 202 +576 439 +992 409 +772 568 +207 791 +228 282 +643 164 +367 760 +731 152 +827 832 +868 932 +796 638 +174 798 +309 65 +96 530 +625 777 +788 153 +293 235 +325 245 +406 516 +532 187 +825 390 +945 714 +743 785 +141 237 +687 226 +414 623 +353 724 +431 14 +445 21 +553 830 +52 652 +799 660 +965 771 +61 81 +139 426 +363 615 +787 164 +51 472 +189 209 +536 683 +913 818 +334 367 +163 395 +911 804 +662 66 +250 502 +301 541 +248 478 +165 308 +679 542 +184 203 +677 748 +49 190 +545 1 +187 259 +842 447 +836 397 +889 1 +696 571 +752 899 +696 855 +243 202 +445 703 +204 217 +55 173 +273 113 +11 132 +290 682 +461 4 +140 818 +948 747 +247 531 +465 487 +29 904 +569 690 +131 105 +345 300 +459 261 +505 55 +783 755 +616 480 +637 337 +803 972 +291 623 +882 204 +6 114 +810 439 +887 281 +281 309 +106 224 +553 443 +960 627 +963 309 +304 351 +889 132 +175 76 +117 141 +631 131 +348 302 +700 259 +556 661 +936 249 +301 627 +477 274 +41 757 +408 236 +123 219 +355 693 +152 923 +583 863 +586 663 +118 996 +245 65 +942 540 +517 699 +669 203 +512 131 +871 223 +240 310 +887 223 +304 122 +917 697 +97 564 +706 788 +272 487 +252 291 +554 258 +686 374 +181 107 +153 790 +504 203 +949 290 +106 977 +518 328 +495 782 +364 223 +983 961 +254 326 +99 771 +81 47 +348 857 +554 912 +731 986 +195 582 +385 488 +944 943 +1 767 +386 53 +133 969 +138 679 +579 191 +363 359 +631 74 +210 173 +918 62 +150 960 +439 189 +208 935 +726 206 +725 507 +598 276 +702 194 +124 558 +608 228 +930 175 +776 119 +928 473 +596 660 +750 922 +451 778 +40 298 +171 584 +384 304 +502 619 +507 420 +609 847 +235 983 +606 985 +595 207 +397 372 +85 240 +680 535 +73 991 +127 622 +40 403 +366 797 +339 894 +104 515 +348 505 +241 471 +694 5 +817 808 +182 370 +2 456 +166 79 +786 384 +987 771 +798 739 +929 32 +730 56 +521 566 +687 563 +173 752 +860 750 +673 837 +333 282 +155 796 +969 605 +492 662 +506 756 +680 158 +329 757 +502 930 +152 247 +647 164 +83 973 +993 495 +923 582 +751 810 +285 345 +157 91 +591 213 +898 445 +855 541 +289 811 +824 447 +853 618 +878 376 +600 259 +247 743 +864 738 +22 761 +813 404 +325 839 +705 415 +484 759 +224 455 +293 679 +786 463 +744 615 +854 978 +854 303 +231 935 +329 666 +570 729 +92 516 +548 224 +254 840 +580 143 +869 981 +301 126 +636 430 +852 591 +211 483 +403 802 +595 686 +917 945 +387 708 +1000 317 +76 736 +783 275 +747 743 +353 57 +283 684 +702 293 +856 218 +81 351 +279 425 +513 181 +500 846 +502 118 +962 495 +112 460 +786 566 +298 192 +450 547 +909 492 +7 973 +164 783 +232 976 +323 801 +546 219 +76 290 +60 437 +364 337 +293 748 +745 553 +535 535 +426 228 +239 163 +582 552 +600 896 +792 160 +592 347 +750 937 +753 30 +252 214 +25 145 +593 806 +124 415 +660 535 +187 777 +526 648 +486 65 +980 92 +460 944 +663 356 +215 575 +603 475 +545 937 +106 734 +755 141 +682 501 +788 443 +396 504 +611 403 +802 674 +166 162 +794 371 +391 251 +859 158 +410 864 +500 913 +771 225 +383 878 +162 152 +754 257 +70 54 +857 384 +151 211 +623 226 +764 216 +314 628 +50 763 +777 977 +817 92 +838 478 +581 654 +754 199 +19 73 +742 479 +171 599 +94 367 +372 241 +23 389 +598 864 +510 360 +232 912 +661 556 +297 425 +639 320 +807 473 +713 287 +944 22 +102 659 +53 340 +675 311 +114 401 +454 24 +645 386 +829 956 +684 855 +874 37 +320 207 +352 87 +360 962 +131 676 +68 979 +135 74 +462 593 +354 232 +327 11 +168 227 +471 211 +310 450 +359 555 +833 165 +803 475 +280 21 +750 831 +396 737 +220 434 +27 646 +825 342 +668 683 +718 33 +497 517 +570 571 +140 500 +823 299 +529 970 +601 493 +336 692 +645 579 +228 765 +670 402 +754 128 +900 556 +642 413 +79 391 +913 419 +160 210 +377 152 +20 307 +100 725 +157 76 +487 311 +963 675 +933 387 +212 240 +872 780 +163 495 +214 79 +949 895 +40 377 +27 29 +220 552 +491 214 +704 165 +204 197 +624 159 +323 336 +520 471 +545 244 +412 408 +560 526 +230 43 +728 1 +255 284 +217 60 +841 388 +893 889 +823 120 +442 234 +21 974 +226 670 +694 794 +378 31 +812 454 +960 910 +33 883 +828 681 +719 244 +814 934 +731 990 +542 967 +168 190 +741 40 +181 685 +583 882 +452 291 +138 554 +342 796 +373 646 +192 771 +76 75 +782 156 +74 456 +314 593 +146 320 +955 54 +342 338 +665 444 +510 766 +76 440 +66 638 +782 943 +243 490 +148 528 +594 578 +855 343 +607 360 +236 484 +728 398 +126 578 +805 438 +232 255 +189 444 +680 608 +960 999 +510 13 +911 221 +276 246 +79 497 +7 158 +39 394 +811 381 +562 25 +419 994 +743 348 +531 122 +118 870 +868 375 +296 170 +396 263 +888 523 +464 391 +272 329 +773 357 +536 380 +197 845 +306 539 +903 388 +4 931 +278 393 +684 540 +849 516 +736 742 +557 11 +786 283 +377 848 +534 693 +536 888 +590 773 +179 187 +490 235 +617 467 +167 353 +359 508 +114 337 +568 291 +999 142 +606 520 +738 304 +146 907 +887 564 +69 124 +513 120 +105 925 +851 652 +256 312 +928 667 +569 709 +292 527 +195 629 +121 978 +204 379 +478 366 +785 816 +80 661 +861 585 +159 296 +941 870 +742 772 +757 263 +339 267 +690 630 +692 569 +827 51 +90 933 +619 29 +694 652 +139 266 +583 369 +646 307 +878 652 +295 193 +398 734 +137 695 +958 608 +751 492 +475 200 +530 969 +134 512 +305 536 +493 194 +800 868 +415 326 +601 608 +548 418 +552 426 +466 157 +881 539 +56 552 +291 52 +2 272 +852 425 +6 346 +438 813 +620 955 +258 441 +383 421 +374 856 +702 574 +380 242 +279 563 +668 286 +237 127 +502 887 +404 223 +198 459 +175 788 +419 101 +434 861 +838 826 +895 533 +715 108 +90 163 +17 312 +414 205 +690 741 +667 187 +98 721 +899 129 +938 930 +481 276 +527 802 +591 374 +601 330 +255 318 +690 275 +940 875 +371 655 +200 502 +83 629 +675 209 +734 687 +450 977 +87 333 +616 233 +752 596 +826 873 +391 297 +672 327 +186 121 +954 556 +891 615 +133 167 +545 249 +605 541 +500 598 +350 88 +331 531 +936 633 +842 12 +276 668 +820 317 +985 670 +309 1 +374 991 +383 236 +817 890 +619 23 +845 363 +432 515 +889 442 +928 10 +945 286 +551 235 +521 799 +921 949 +142 204 +3 168 +784 670 +420 360 +84 948 +518 299 +388 325 +177 846 +937 779 +242 824 +356 854 +506 453 +257 637 +287 481 +398 632 +907 877 +139 238 +46 552 +612 932 +230 125 +103 365 +971 105 +744 953 +549 529 +554 490 +306 589 +264 819 +984 45 +949 582 +399 840 +672 593 +7 681 +575 929 +453 914 +394 512 +149 162 +530 783 +996 195 +57 262 +704 468 +460 477 +846 158 +982 313 +404 876 +847 228 +37 350 +61 246 +501 600 +744 172 +482 893 +880 546 +908 877 +953 741 +647 188 +614 574 +290 637 +67 43 +788 871 +377 232 +880 123 +454 944 +496 868 +501 87 +450 195 +668 531 +88 910 +666 518 +96 697 +134 148 +427 694 +487 238 +409 313 +784 368 +887 312 +539 45 +642 651 +125 930 +28 814 +313 747 +470 172 +431 380 +50 591 +656 927 +573 42 +330 433 +67 107 +772 910 +286 893 +729 902 +577 412 +892 42 +454 284 +114 553 +659 945 +860 960 +860 37 +37 364 +124 579 +297 860 +693 146 +593 651 +108 586 +415 85 +242 768 +316 933 +720 296 +933 829 +787 681 +665 840 +491 942 +757 988 +729 589 +898 745 +190 204 +782 509 +508 282 +77 171 +64 841 +371 556 +540 648 +473 835 +505 947 +1 422 +1 349 +483 664 +790 119 +597 812 +749 774 +809 676 +693 710 +238 399 +632 161 +366 887 +679 108 +30 795 +566 680 +686 408 +75 376 +653 506 +746 246 +440 314 +357 784 +835 977 +766 170 +182 273 +431 899 +977 189 +249 666 +520 240 +5 906 +880 211 +585 264 +832 580 +780 318 +369 97 +594 128 +157 397 +967 842 +259 598 +133 546 +805 291 +365 862 +165 844 +52 350 +257 954 +630 883 +607 224 +846 670 +58 708 +580 241 +82 695 +934 480 +130 136 +733 647 +237 304 +91 902 +318 247 +665 459 +651 654 +169 954 +871 209 +184 392 +823 707 +406 988 +632 737 +511 679 +1000 294 +923 598 +287 447 +49 56 +755 871 +894 321 +884 261 +174 367 +225 271 +641 438 +563 620 +189 618 +370 501 +28 169 +942 314 +927 105 +868 212 +742 506 +914 15 +1000 534 +1 18 +155 782 +909 111 +629 603 +452 591 +287 485 +989 964 +328 278 +37 103 +118 549 +545 321 +290 899 +697 746 +831 818 +256 524 +996 539 +534 839 +952 86 +328 862 +780 110 +542 347 +672 623 +997 957 +480 226 +283 447 +679 961 +458 358 +219 308 +960 643 +238 731 +977 646 +852 226 +48 973 +242 840 +118 218 +355 298 +11 215 +504 846 +330 320 +960 211 +691 352 +424 707 +806 566 +453 120 +839 653 +344 749 +817 737 +732 791 +222 555 +164 73 +134 693 +208 144 +118 592 +804 597 +819 313 +285 336 +950 198 +999 642 +800 307 +940 674 +278 167 +554 460 +210 988 +119 67 +400 165 +328 312 +794 708 +195 745 +706 677 +615 268 +53 268 +444 183 +857 743 +633 537 +856 695 +693 326 +71 798 +403 520 +795 373 +675 355 +603 438 +319 867 +868 233 +84 857 +695 831 +283 825 +168 61 +715 311 +263 323 +412 193 +147 449 +503 269 +986 950 +727 364 +415 196 +579 343 +662 190 diff --git a/2024/day_1/rust/input.txt b/2024/rust/day_1/input/example.txt similarity index 100% rename from 2024/day_1/rust/input.txt rename to 2024/rust/day_1/input/example.txt diff --git a/2024/rust/day_1/main b/2024/rust/day_1/main new file mode 100755 index 0000000..6359024 Binary files /dev/null and b/2024/rust/day_1/main differ diff --git a/2024/day_1/rust/main.rs b/2024/rust/day_1/main.rs similarity index 59% rename from 2024/day_1/rust/main.rs rename to 2024/rust/day_1/main.rs index ce4f358..fec6779 100644 --- a/2024/day_1/rust/main.rs +++ b/2024/rust/day_1/main.rs @@ -1,26 +1,36 @@ use std::fs; fn main() { - let contents = fs::read_to_string("input.txt") + let args:Vec = std::env::args().collect(); + if args.len() <= 1 { + println!("Please, provide the input file path to be processed!"); + return; + } + let firstArg = &args[1]; + let contents = fs::read_to_string(firstArg) .expect("Expected to open the file"); let splitted_content : std::str::Lines = contents.lines(); //DONE: Split string lines //DONE: get the values from each column - //TODO: calculate the distance between the columns - //TODO: sum the distances + //DONE: calculate the distance between the columns + //DONE: sum the distances println!("With text:\n{contents}"); //println!("\n{splittedContent:?}"); - let numbers:Vec<(i32,i32)> = splitted_content + let numbers:i32 = splitted_content //.lines() //.into_iter() .filter_map(|x| { let mut nums = x.split_whitespace().filter_map(|i| i.parse::().ok()); Some ((nums.next()?,nums.next()?)) }) + .filter_map(|(a,b)| { + Some(b - a) + }) //; - .collect(); - for number in numbers { + .sum(); + println!("Distance sum: {numbers}"); + /*for number in numbers { println!("\n number -> {number:?}"); - } + }*/ /*for line in splitted_content { for word in line.split_whitespace() { let x = word.parse::().unwrap();