diff --git a/Cargo.lock b/Cargo.lock index 616a91b..00dde89 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "addr2line" @@ -19,7 +19,7 @@ checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" [[package]] name = "advent-of-code-2024" -version = "24.0.0" +version = "24.2.0" dependencies = [ "reqwest", ] diff --git a/Cargo.toml b/Cargo.toml index b852e6b..4d9b4ab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] authors = ["Toby "] name = "advent-of-code-2024" -version = "24.0.0" +version = "24.2.0" edition = "2021" default-run = "get_input" @@ -28,9 +28,17 @@ test = false [[bin]] name = "template" # day00 for alphabetic sorting -path = "src/day00/main.rs" +path = "day00/main.rs" test = false +[[bin]] +name = "day01" +path = "day01/main.rs" + +[[bin]] +name = "day02" +path = "day02/main.rs" + [dependencies] reqwest = { version = "0.12.9", features = ["blocking"] } diff --git a/src/day00/input.txt b/day00/input.txt similarity index 100% rename from src/day00/input.txt rename to day00/input.txt diff --git a/src/day00/main.rs b/day00/main.rs similarity index 100% rename from src/day00/main.rs rename to day00/main.rs diff --git a/src/day00/part_1.rs b/day00/part_1.rs similarity index 100% rename from src/day00/part_1.rs rename to day00/part_1.rs diff --git a/src/day00/part_2.rs b/day00/part_2.rs similarity index 100% rename from src/day00/part_2.rs rename to day00/part_2.rs diff --git a/day01/input.txt b/day01/input.txt new file mode 100644 index 0000000..721e1a3 --- /dev/null +++ b/day01/input.txt @@ -0,0 +1,1000 @@ +98415 86712 +21839 96206 +14958 40971 +91380 63825 +48482 40971 +47618 98239 +69295 72055 +73790 20692 +95008 91254 +30041 73739 +46680 55405 +12873 14682 +47722 74915 +81092 16759 +64463 31849 +82981 43091 +52948 63825 +86073 90988 +37341 87339 +76759 85103 +92565 20139 +70507 99102 +10215 32134 +80264 72821 +45235 31963 +41003 84233 +88490 76750 +87607 20692 +59690 33244 +76224 26934 +70928 58112 +12896 13883 +45046 97624 +14802 36874 +26477 90988 +99942 42762 +97700 68810 +31112 62593 +63110 73448 +15702 58189 +44444 28782 +56091 79236 +88976 38094 +33509 84823 +55932 52315 +37002 55140 +49031 69737 +62442 84713 +91598 28894 +96109 43306 +10029 65760 +81145 78422 +57749 88707 +30352 38306 +58767 84823 +65644 83417 +14392 60859 +38548 23806 +45420 84823 +86410 24207 +86074 38732 +34418 40971 +69259 84823 +76444 61427 +52200 60859 +80383 46070 +24950 29096 +92504 56091 +63952 37106 +21571 67919 +30674 98750 +73909 62113 +51915 81880 +58884 64362 +35784 90988 +77693 46396 +43415 40971 +17619 43889 +61513 58139 +86035 43930 +79559 90195 +74180 41607 +62739 98079 +53632 57293 +21594 96012 +56752 20348 +49825 90988 +27016 54248 +79239 60318 +79102 46726 +15178 47103 +69131 73658 +77863 72055 +57745 74229 +75083 30406 +30789 88659 +59510 68773 +37256 87339 +89551 24882 +24912 11151 +65718 87339 +18607 72055 +89744 34103 +13502 63777 +74252 93822 +26823 88948 +12926 20361 +57362 37728 +42529 72897 +55701 90106 +80515 90673 +53130 98750 +11755 39123 +15567 16982 +45347 73739 +56219 84823 +80487 63515 +57918 85690 +24138 78136 +89383 68948 +33043 72055 +74588 28108 +83590 64739 +71034 65419 +75815 23490 +20112 76750 +14438 82344 +10441 40971 +73704 18644 +73739 51191 +17858 91254 +45052 90988 +71745 63825 +61105 71049 +69625 64414 +72147 79179 +39613 42434 +49486 16939 +37159 88707 +81789 21633 +43834 31963 +96281 51697 +67908 31849 +25034 44271 +65569 75978 +58832 33079 +73347 31849 +77421 84823 +11586 91254 +67679 36415 +39964 22528 +37638 61931 +26753 90315 +90409 39489 +55746 76750 +44727 95445 +90778 60859 +32071 78505 +60089 50682 +32309 11406 +19358 80132 +48376 87339 +45291 47907 +56391 84233 +76798 48120 +78422 60859 +58146 21303 +53529 63825 +98604 91254 +34637 49452 +94181 39489 +27144 99118 +49195 80934 +98493 70617 +78396 98075 +59508 31032 +14533 87339 +65929 90988 +12476 17041 +18406 31963 +84233 66617 +12813 20863 +87866 98750 +92020 16503 +86199 29241 +14865 64023 +77251 18843 +29087 16712 +89093 67919 +84823 52315 +97124 63689 +21993 38094 +52548 98181 +64426 38094 +39508 81972 +88707 37155 +77577 41917 +69880 57192 +44717 79597 +60859 67919 +12172 39516 +52315 77348 +34818 40971 +68237 47371 +90048 31492 +91780 91254 +83184 65480 +47595 13030 +93135 39092 +90044 52315 +57788 93726 +62763 23239 +89761 38094 +10716 98750 +35248 55737 +59494 68055 +16810 67131 +88942 31963 +76030 59024 +68413 56574 +71633 40971 +27617 46689 +61233 48940 +13658 34496 +40703 65209 +28669 83621 +70132 52315 +37900 46800 +36084 12920 +70378 60859 +82602 86916 +38476 25624 +11688 52315 +63095 73739 +83433 37370 +66582 31849 +44408 68006 +91580 13383 +41394 18554 +15054 21090 +83901 91254 +68378 12571 +70868 72055 +25682 99471 +80283 84823 +58774 91254 +15043 44002 +62670 73739 +55915 52315 +34285 50248 +45860 50682 +26145 82948 +44170 74880 +69068 84270 +38169 38094 +15020 21645 +48351 46795 +52242 17322 +57262 46257 +45360 52054 +57518 87339 +12487 63515 +13358 15895 +38204 73180 +37940 94820 +75950 97855 +98789 72251 +55604 92176 +39930 61056 +84928 39489 +93961 90584 +85987 87014 +27022 40307 +77327 63825 +57151 31963 +86379 82871 +37155 88222 +44284 84823 +34832 96979 +85214 69001 +88783 27436 +12782 31963 +70752 80954 +97651 23189 +46554 46907 +43866 38094 +60090 58763 +89564 22691 +11558 52445 +61150 58975 +11230 29247 +46103 98325 +31204 44018 +44420 71528 +33659 10730 +30098 69230 +77473 98410 +67198 63825 +91560 90988 +17842 18473 +31031 10138 +25221 60859 +62150 66417 +14373 91254 +33995 70555 +32172 57970 +36488 94041 +95747 56450 +61567 90066 +74271 80136 +51290 96535 +27995 31963 +63515 87339 +72055 18311 +22226 63515 +59375 81818 +76512 49521 +85812 78856 +27537 78736 +87666 32193 +27624 12413 +64096 61750 +10749 20692 +27436 52315 +75435 50309 +86558 60859 +25138 18662 +41356 60859 +68760 72055 +45574 25507 +21577 98942 +31881 81870 +65016 73739 +90656 32647 +24122 87339 +78458 60859 +99290 42530 +63362 74484 +45324 74474 +55742 48062 +46944 44844 +71534 20692 +38348 53179 +90391 31963 +63186 72873 +43435 52315 +96716 32259 +99923 55737 +90098 70281 +14525 93644 +14634 38094 +15853 78345 +33108 33722 +83439 51275 +93742 60859 +68758 90988 +34771 17456 +29473 47384 +27697 39570 +29944 50455 +50249 32302 +89444 99416 +30805 90988 +94132 61365 +98750 31849 +83186 76750 +52252 65184 +89753 72055 +98197 98697 +76750 72055 +21126 67274 +21641 34760 +62076 33495 +14579 64696 +22715 81980 +90163 12406 +95784 68047 +85724 87339 +41620 10409 +92753 76750 +37666 91254 +38137 46672 +47110 60859 +54543 23101 +26495 51493 +43159 27360 +61846 39431 +83787 84233 +13506 20222 +60298 31345 +21742 73739 +91928 76750 +67392 27089 +59447 88657 +10147 52315 +88420 63825 +84317 67919 +18279 84823 +15984 90756 +83897 38094 +17524 90988 +26017 45126 +98635 20112 +82243 27089 +26750 26406 +69469 77146 +89732 11124 +41258 17720 +48062 78300 +68561 13517 +34283 85667 +97835 43741 +21351 67919 +32372 16720 +14088 38936 +89173 10152 +69129 43769 +46965 73739 +47834 91233 +17627 19189 +12268 67826 +37356 52315 +68366 63825 +50838 29430 +94974 62978 +65427 92335 +78704 92785 +98194 31849 +94860 91254 +10413 52315 +99435 76750 +16749 73328 +43068 16548 +18135 40488 +58421 12600 +37910 92811 +28769 34293 +95334 30803 +67748 59225 +44365 31963 +29273 20692 +46370 60859 +47709 63912 +63775 84823 +75548 28296 +33798 38094 +69672 89607 +74754 73739 +49066 30017 +89756 52315 +67919 43998 +88061 68521 +39137 50152 +59338 49890 +44756 90707 +98359 36495 +10113 80721 +96163 13016 +18965 30702 +95671 81809 +78007 93680 +72250 92806 +24831 48062 +93561 25138 +73962 87667 +90248 48062 +95762 63442 +96852 50225 +36995 31227 +87401 91254 +30838 84649 +11224 48062 +79061 50682 +83824 20112 +61242 75132 +97196 31849 +31927 48062 +22087 72055 +64178 33632 +92080 70089 +84339 28433 +57804 76750 +25387 37155 +81722 84823 +86463 63825 +26959 84233 +74918 36995 +47554 71702 +51450 77114 +15753 64312 +79468 83563 +60172 63825 +89187 66416 +47552 84233 +63836 96955 +50736 60859 +46257 33617 +27477 63628 +94159 73739 +26339 27964 +99674 93217 +61250 23628 +94382 64265 +11539 50180 +10492 62057 +15414 82089 +81830 76750 +79729 32501 +59935 78410 +55737 91254 +33501 31849 +98398 50682 +97275 76602 +38887 86626 +48245 15889 +67814 41213 +28276 96218 +52995 25138 +51768 84823 +23366 67919 +58043 94722 +33655 56522 +50043 72450 +73839 90440 +10752 63825 +15750 19911 +99509 60996 +12456 87339 +20727 90988 +62359 40971 +81671 64213 +91541 35482 +94485 50682 +67680 37155 +63058 32117 +50090 98750 +61000 60859 +11341 81637 +10675 70533 +22159 99742 +99705 32116 +32085 78422 +56367 18049 +78611 64924 +90242 50682 +61570 68777 +98193 79855 +63068 73739 +53915 13627 +92664 33858 +82118 62671 +23234 31957 +94674 39489 +32450 55737 +59276 99636 +39738 42163 +42659 31849 +29950 38094 +17039 96134 +31374 52568 +13625 72109 +33042 27336 +84607 97422 +31849 20692 +17061 20500 +12380 20692 +55834 76750 +40260 40971 +58188 99023 +45619 90988 +46734 72495 +25567 37155 +27593 36939 +27705 27436 +35918 53049 +72395 22908 +83024 25138 +59905 50682 +29942 51044 +23864 79568 +55150 40971 +39901 73739 +51429 84499 +26667 56600 +20692 49999 +44423 48413 +89639 90988 +62449 23693 +82368 68603 +37998 60859 +91308 84355 +47625 84233 +87235 48062 +10479 40971 +12470 77812 +79398 92110 +24157 35490 +84322 84823 +44438 27300 +46201 37155 +15815 44116 +52626 38094 +15858 46133 +89797 98911 +76844 17965 +36493 81353 +68425 84823 +22137 60859 +77167 33379 +87697 19256 +41603 27345 +93230 87339 +34535 39068 +62368 31963 +71318 34890 +14566 52315 +90439 45351 +87013 50682 +85339 92917 +91534 63322 +90988 31849 +37108 89248 +25398 15630 +60269 46354 +73929 31963 +85753 89789 +13055 76464 +79991 88707 +63825 91254 +98473 56091 +64938 57251 +38094 38881 +50465 22038 +73037 10811 +75967 49581 +68942 84233 +71786 76750 +99611 50682 +17590 35221 +87318 16184 +38505 36185 +19563 71760 +91493 59209 +75286 91254 +54534 45259 +70281 24482 +30770 52315 +84275 55737 +64420 41263 +67601 56131 +16784 14971 +46723 71705 +49609 33279 +10952 52768 +40729 44124 +90745 77061 +34124 87339 +79459 19662 +59660 88336 +33724 76750 +41878 31471 +29912 36995 +21386 84233 +32945 87339 +39159 86983 +81459 50051 +63810 76750 +27827 72336 +75116 76750 +70438 85513 +63535 37155 +55684 39489 +96958 95080 +62793 40971 +48649 37155 +73108 38479 +12038 73035 +49718 94369 +49882 21814 +90650 34851 +11116 61714 +79917 51077 +62707 55628 +92459 96915 +95690 53399 +24211 27436 +87266 91254 +15847 12424 +17896 54529 +53974 84823 +92698 72470 +40973 38630 +24809 20692 +71610 59555 +67213 88707 +42767 79241 +96580 42503 +79226 82565 +12051 34910 +14600 90988 +70962 48062 +72321 73739 +66896 77565 +55308 60714 +32305 40971 +15705 60028 +20331 48062 +90356 91254 +27446 21289 +32691 31849 +88785 48062 +51419 28561 +44311 79505 +95234 57870 +12935 40971 +62198 91752 +26331 82576 +63091 91254 +28603 82261 +40147 67117 +83978 77859 +19279 56339 +30511 77718 +19793 87363 +67630 67919 +85108 17324 +37977 15731 +58983 49370 +68423 35881 +10224 74695 +41736 40884 +13590 91254 +23362 40971 +99499 65408 +16460 73297 +35426 69103 +14873 10331 +80614 98750 +27859 86699 +94072 52315 +18981 27436 +51030 63825 +31979 31974 +10060 49581 +97457 59642 +31057 52315 +98560 84823 +47286 38094 +35565 40971 +42285 54785 +81392 82713 +22878 48062 +34487 65138 +24156 93846 +57704 49888 +51352 33050 +73714 50682 +73960 47692 +14686 25215 +38555 40971 +92632 61868 +12529 48062 +48406 66247 +44504 70356 +78562 84823 +21381 89200 +93855 56801 +45470 48023 +13650 59647 +82960 46206 +40965 72192 +31963 45048 +48040 67919 +53457 76750 +47995 38468 +56904 73507 +53710 74022 +99557 67919 +52346 87339 +22009 31849 +10126 20692 +96604 46257 +76629 49581 +87339 22783 +53577 32708 +93143 24815 +95929 82359 +41268 76312 +28360 50433 +18926 20246 +44735 55737 +21754 94200 +45995 20692 +91603 57841 +69767 82262 +37282 80348 +17730 76897 +19647 24004 +97462 76346 +71047 62037 +34121 72977 +49581 84188 +92169 33396 +14641 72055 +39489 56331 +64286 36007 +66660 55737 +61013 57502 +93637 89277 +79890 46257 +36204 69809 +87899 98750 +90417 60859 +66456 67919 +31910 50411 +95024 24713 +53501 97080 +47761 52315 +91582 45390 +35753 93253 +20424 98133 +52448 67528 +39925 38895 +28344 21734 +54088 88707 +40684 79586 +63495 39489 +42745 46257 +36411 20692 +24189 73700 +63663 55737 +51325 97464 +35638 57921 +16147 97192 +65808 26709 +93854 21334 +83029 98750 +85681 31849 +85513 20692 +48163 33364 +64824 70813 +80330 59638 +68279 56091 +63850 98633 +64014 31963 +39492 91576 +24595 82232 +53478 67388 +33189 72055 +79380 84119 +74284 54996 +70140 69650 +86881 87339 +66639 98974 +67556 62236 +85378 37807 +93552 11734 +77734 87339 +84788 63515 +25658 73739 +50845 42313 +39182 24860 +39825 50682 +50295 63515 +66539 21906 +31197 73739 +40971 31849 +12253 54780 +89300 49136 +64819 18235 +38111 67613 +29614 71066 +94658 79493 +35651 13233 +34949 41218 +41470 40932 +56805 58595 +97958 27436 +41542 43810 +98116 87339 +95005 64201 +26689 73039 +87929 72055 +48643 65869 +44264 80510 +42574 36244 +63706 36541 +10516 20692 +81888 48062 +46840 49581 +88271 62250 +94269 13433 +72975 51346 +40378 31963 +85436 67919 +98412 31849 +16302 25890 +92817 76750 +73558 95672 +61378 27919 +19104 34857 +86385 27774 +18893 20421 +62394 27466 +97168 78422 +76828 20692 +57785 11439 +69161 84823 +81891 88707 +14398 50682 +75597 84233 +40237 74679 +74627 72055 +68878 44137 +64971 55565 +83067 91254 +77491 79186 +88686 15795 +57603 13448 +45266 68597 +33706 36802 +26544 90988 +80230 41517 +98279 37155 +75578 87339 +78560 90689 +53825 69683 +46223 35266 +50699 31963 +11507 30160 +98508 73739 +85131 90605 +51806 78874 +66216 29974 +55896 72055 +87983 45827 +37421 24722 +27089 72055 +35796 31849 +50647 46257 +18865 87578 +95580 48404 +95447 60859 +57831 29451 +74714 36995 +81845 11460 +42033 66983 +77846 87339 +68670 86138 +48821 50682 +58352 37624 +76352 73739 +27924 45322 +14974 58194 +19887 19109 +23329 85568 +20214 61048 +36738 48062 +64224 63825 +56209 40971 +63592 90988 +96560 76137 +73748 48062 +90647 86904 +10910 88920 +85440 41920 +33264 90988 +76674 75533 +54141 16463 +68367 86319 +46421 73739 +71740 26423 +84766 10695 +83450 14876 +59985 31963 +77991 60859 +91254 84233 +70747 31849 +39254 26780 +16062 25138 +78945 65871 +50682 27436 +65403 91254 +95579 60732 +21033 29762 +29307 58721 +69595 98750 +40807 84823 +19522 93019 +90505 40971 +53911 90998 +52411 91271 +75197 48589 +90568 48062 +86892 84823 +15662 62119 +60408 73739 +61681 46257 +81491 31034 +83205 39489 diff --git a/day01/main.rs b/day01/main.rs new file mode 100644 index 0000000..79c4c71 --- /dev/null +++ b/day01/main.rs @@ -0,0 +1,92 @@ +pub const INPUT: &str = include_str!("./input.txt"); + +fn parse_input(input: &'static str) -> Vec<[u32; 2]> { + let input = input + .lines() + .map(|line| -> [u32; 2] { + let nums = line + .split_whitespace() + .map(|num| num.parse::().unwrap()) + .collect::>(); + [nums[0], nums[1]] + }) + .collect::>(); + input +} + +pub fn main() { + let input = parse_input(INPUT); + let result_1 = part_1(&input); + println!("{result_1}"); + let result_2 = part_2(&input); + println!("{result_2}"); +} + +fn part_1(input: &Vec<[u32; 2]>) -> u32 { + let mut left = vec![]; + let mut right = vec![]; + for pair in input { + left.push(pair[0]); + right.push(pair[1]); + } + left.sort_unstable(); + right.sort_unstable(); + let mut diff = 0; + for i in 0..left.len() { + diff += u32::abs_diff(left[i], right[i]); + } + diff +} + +fn part_2(input: &Vec<[u32; 2]>) -> u32 { + let mut left = vec![]; + let mut right = vec![]; + for pair in input { + left.push(pair[0]); + right.push(pair[1]); + } + + let mut result = 0; + + for num in left { + result += num * right.iter().filter(|&&x| x == num).count() as u32; + } + + result +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + const SAMPLE_INPUT: &str = r#"3 4 +4 3 +2 5 +1 3 +3 9 +3 3"#; + + #[test] + fn test_part_1_with_sample_solution() { + let input = parse_input(SAMPLE_INPUT); + assert_eq!(super::part_1(&input), 11) + } + + #[test] + fn test_part_1_with_solution() { + let input = parse_input(super::INPUT); + assert_eq!(super::part_1(&input), 2192892); + } + + #[test] + fn test_part_2_with_sample_solution() { + let input = parse_input(SAMPLE_INPUT); + assert_eq!(super::part_2(&input), 31) + } + + #[test] + fn test_part_2_with_solution() { + let input = parse_input(super::INPUT); + assert_eq!(super::part_2(&input), 22962826); + } +} diff --git a/src/get_input.rs b/src/get_input.rs index 15ca18f..bb0c5c0 100644 --- a/src/get_input.rs +++ b/src/get_input.rs @@ -14,7 +14,7 @@ fn main() -> Result<(), Box> { .header("Cookie", format!("session={session}")) .build()?; let input = client.execute(request)?.text()?; - fs::create_dir_all(format!("src/day{day:02}/"))?; - fs::write(format!("src/day{day:02}/input.txt"), input)?; + fs::create_dir_all(format!("day{day:02}/"))?; + fs::write(format!("day{day:02}/input.txt"), input)?; Ok(()) }