diff --git a/aoc2024/day01/example.txt b/aoc2024/day01/example.txt index e69de29..b8af9ad 100644 --- a/aoc2024/day01/example.txt +++ b/aoc2024/day01/example.txt @@ -0,0 +1,6 @@ +3 4 +4 3 +2 5 +1 3 +3 9 +3 3 diff --git a/aoc2024/day01/input.txt b/aoc2024/day01/input.txt index e69de29..fd6ff80 100644 --- a/aoc2024/day01/input.txt +++ b/aoc2024/day01/input.txt @@ -0,0 +1,1000 @@ +17633 15737 +79440 47531 +44767 73309 +86871 26386 +66575 90774 +31637 38259 +89855 50198 +81829 20253 +95071 98569 +34163 43719 +28696 84802 +10975 89855 +48232 15737 +14078 99983 +40882 73431 +75659 94769 +85341 13345 +92610 16338 +18043 59463 +60606 24540 +22376 46760 +14535 24785 +26022 94769 +90745 76304 +94386 84190 +22699 50602 +48716 38259 +93599 63674 +24797 57698 +89866 94029 +13841 87991 +12612 31743 +22889 53500 +84705 43272 +50982 26386 +19312 68554 +43985 36005 +23864 99058 +71953 45509 +58611 64279 +15949 98297 +89181 99781 +57095 54781 +19462 82924 +13989 32724 +74037 81607 +98414 34699 +17793 73309 +77332 64279 +17360 75855 +88430 38259 +59633 52523 +93603 31051 +93215 57119 +89750 48853 +73983 31051 +95163 35142 +50441 65929 +66767 72254 +96947 78965 +62870 50159 +46395 76148 +25246 21955 +39127 18070 +69589 72254 +87118 77505 +89577 11697 +64365 17982 +89417 90211 +38259 91050 +14458 32457 +42775 20442 +58228 72254 +87729 30434 +63480 17982 +90234 31051 +19242 94087 +77553 64344 +42962 27509 +20746 70689 +42003 14840 +24751 72254 +98825 66551 +35667 48627 +86088 43388 +87433 12947 +42556 73309 +77443 26386 +85947 66440 +26239 23875 +51904 10613 +45935 55396 +93900 79692 +94769 26138 +89944 29196 +16924 31743 +29441 72254 +31685 52714 +28455 38856 +82408 89991 +50492 79692 +48587 27909 +52190 51936 +14432 26239 +40296 90211 +87571 15975 +68554 82924 +60909 77747 +90581 50216 +17874 89227 +77699 84802 +52227 95383 +81281 38845 +74510 77249 +34913 52738 +17927 11147 +38246 67042 +95559 90562 +97142 94769 +17388 43583 +45933 73309 +61625 92202 +92202 60021 +32609 42173 +10121 15737 +42108 52145 +99781 88040 +71839 52086 +31215 12281 +23028 11321 +45513 36187 +44989 92435 +21042 26664 +41686 53092 +49396 13345 +87383 24542 +57374 94769 +92305 94769 +90956 73220 +59298 60825 +71833 61968 +39322 15153 +90211 98328 +78524 76232 +56222 48717 +88896 42647 +24989 84802 +15618 57610 +40750 15330 +57787 59202 +66076 64279 +27005 46356 +12299 27983 +99606 96968 +78648 88145 +90191 82924 +40731 26599 +28572 32457 +29036 39980 +12210 26664 +72284 11697 +76661 90364 +90125 24914 +42239 14455 +11589 61336 +33000 72254 +10758 31712 +10172 21525 +39561 91482 +47405 95316 +44894 83948 +46926 43967 +59896 87826 +85512 65546 +58641 79692 +83574 57878 +98203 84802 +43633 10581 +47564 69216 +87287 82731 +43747 13345 +91297 79692 +76432 18598 +75790 26386 +93818 96480 +31051 38259 +47511 64279 +97907 22026 +87367 90211 +59976 50541 +52149 99781 +17942 67693 +99139 69460 +66216 49858 +29264 38259 +82924 51350 +66027 25520 +82954 73309 +12290 67322 +20683 93568 +85750 13345 +54662 52685 +79346 31641 +76068 84568 +39015 79773 +30045 19435 +82962 25655 +20148 42108 +67813 29679 +65066 26239 +84222 63931 +13345 59242 +90893 48967 +51707 83112 +42399 15737 +40546 66000 +19343 92202 +95420 14200 +81543 55348 +28417 13345 +57947 91893 +76560 24540 +25531 23044 +60453 50868 +48959 38856 +66180 16455 +16907 83845 +79677 32420 +69680 92202 +65677 32375 +58869 79692 +49481 19672 +81530 26664 +30968 18603 +40003 36218 +70770 89227 +51169 17982 +39585 99781 +41887 90211 +98401 68715 +66079 60199 +79686 17982 +95976 34064 +62505 24540 +66440 31743 +66353 26239 +37729 66440 +78716 84802 +87428 62768 +52512 89855 +86117 84802 +54387 68022 +13759 78689 +40476 66769 +44961 19623 +99372 68799 +89090 79636 +61709 42060 +66586 11972 +68112 33028 +56457 31743 +58500 37748 +49835 52835 +69526 95383 +81736 84802 +72397 80320 +28792 44785 +67973 74969 +28140 79692 +99229 95383 +37502 67333 +21071 32457 +98028 82116 +13892 64667 +64351 19859 +39347 86071 +73309 94769 +25904 31051 +50933 28770 +81957 97102 +53641 15737 +88814 42916 +92575 26239 +62255 54132 +81785 72088 +75349 50602 +83203 89855 +98080 81605 +93942 31743 +97798 57279 +45256 73898 +51544 93706 +25195 88904 +68947 68366 +91830 68022 +91524 54353 +76708 31564 +90123 40095 +86890 21718 +98282 26239 +41961 42868 +11039 12856 +55505 73643 +56720 73309 +50589 35243 +18316 57600 +89706 26386 +77270 26934 +16280 31743 +38856 31317 +38306 72254 +22621 31743 +52085 35952 +25990 17841 +47713 46814 +50171 26239 +68656 30746 +16833 96013 +31650 59611 +38239 93728 +90966 22779 +20522 65624 +74289 88356 +30878 15020 +99343 90211 +46494 46080 +40055 98186 +62089 79296 +27261 11480 +14641 89227 +62672 51003 +43731 67153 +13641 93818 +81157 15737 +38752 93975 +22481 16596 +56161 26876 +11821 15737 +21702 42699 +95781 99704 +96637 61893 +52222 25556 +56805 32457 +43568 31051 +62828 15737 +19567 40953 +49559 25373 +55482 42108 +44986 95412 +53062 52392 +57314 26664 +37016 14626 +63361 92202 +68680 79692 +18476 89101 +20199 21632 +77823 89855 +69738 96520 +67170 40198 +79546 82924 +85110 88431 +54119 46332 +86781 92202 +90311 90211 +57902 82924 +54606 12067 +75696 90211 +31498 77415 +63061 47498 +20426 30513 +77880 79692 +80653 46725 +12847 77929 +67482 73832 +41457 22015 +96070 37125 +26083 87259 +15571 17982 +64763 81999 +69049 22033 +37215 73309 +68721 84808 +41483 43032 +19623 21237 +80415 94769 +40289 47860 +29800 15737 +51573 87470 +95147 93818 +49768 43274 +57896 47996 +11698 31743 +18534 93345 +33315 66184 +93295 26435 +13998 14286 +37866 42108 +27311 91778 +88630 50602 +61311 65732 +58088 54659 +39038 92202 +21263 32457 +68733 26239 +56327 26386 +10831 84802 +41475 14140 +65191 84802 +65689 10408 +99405 76417 +60130 26296 +62835 15737 +43765 25627 +37857 74612 +75626 26698 +82299 39722 +59124 44751 +11662 59626 +13403 38259 +84021 71058 +46515 31743 +19274 99781 +42981 66150 +81200 96138 +28115 52538 +66036 73617 +46632 44785 +44785 45999 +93611 94769 +58202 27856 +83448 31743 +47387 99781 +56227 19962 +10056 90211 +47436 42592 +25614 38259 +91347 94837 +93293 87462 +54018 13345 +93975 24540 +88534 28661 +55107 36119 +90676 30422 +74207 72254 +53544 11697 +82027 50515 +28952 93818 +24039 73309 +51681 59383 +84784 33827 +57727 38856 +17375 79692 +84334 75729 +85913 61227 +34190 70516 +88405 93909 +57014 89855 +49572 51212 +96919 57120 +57074 84802 +97433 94600 +39316 19546 +23267 73309 +49617 62475 +45398 49944 +93789 84802 +80870 79692 +66592 89412 +84914 21538 +15737 58959 +85633 17470 +24299 89227 +50996 28076 +59760 37410 +19853 36303 +87172 63500 +73167 26239 +69533 58203 +10294 47085 +10068 84802 +71528 59133 +90074 48734 +85177 13345 +39240 19623 +46901 31230 +31573 14744 +40287 76713 +52063 31743 +15665 92202 +55652 57799 +94618 43492 +35516 17699 +99836 97684 +12266 93653 +49382 94769 +58480 27713 +30160 95383 +15895 82924 +99570 35771 +86138 29452 +50602 15240 +65863 72856 +14014 17313 +23202 73309 +52442 99781 +98402 62424 +99960 93818 +30587 37482 +27344 71566 +14901 18107 +50646 87619 +39894 82924 +76102 79692 +93553 41109 +95383 19459 +43523 34173 +93147 15737 +52736 86381 +73737 38856 +74670 89855 +70375 44671 +35660 27596 +68216 72680 +87067 46274 +74103 75792 +95109 17982 +17982 92202 +81693 14959 +72925 13345 +79007 26239 +56890 75332 +43163 94330 +89721 89227 +98999 88213 +24385 71775 +31463 68554 +86271 72254 +76927 43416 +82914 88520 +10672 42227 +37107 72254 +53415 65905 +57507 32457 +61956 80596 +53426 42108 +24147 73170 +42957 89855 +82795 66358 +36784 93211 +33334 18704 +81024 79692 +50757 87305 +11023 19777 +56827 91998 +63122 72254 +22187 93663 +45790 24392 +45455 25374 +18321 90211 +50449 42108 +88021 45580 +49799 13345 +87225 94769 +97435 26386 +83558 87182 +38550 95383 +33452 13345 +56025 17982 +67783 93390 +84187 48977 +39395 89716 +68418 38259 +52548 89161 +28163 72254 +84823 89227 +31206 15482 +64705 44785 +58401 31743 +95618 38856 +91709 93818 +48005 92202 +76020 82924 +23833 93782 +52831 50653 +85010 31743 +10664 99781 +22411 13783 +43979 31051 +61257 57691 +61520 15737 +48746 88134 +14279 44785 +47898 89855 +93422 88779 +68022 92202 +14459 96167 +89227 40096 +20824 33236 +75208 79692 +49845 41520 +51562 87703 +93302 64917 +92836 95935 +49142 35882 +50653 24538 +70201 17982 +13156 26664 +72511 68022 +25934 38856 +91338 47911 +75287 41621 +15292 17284 +61020 24851 +24871 93818 +69502 32804 +22107 32457 +11069 17982 +90501 32457 +79735 33307 +22407 40613 +56638 73309 +43995 97846 +24346 10698 +19332 64254 +98053 96741 +64279 95856 +35846 80488 +87249 15737 +26693 91433 +23953 37660 +36613 18796 +68547 42108 +29801 84802 +95876 20755 +96850 26239 +69173 21661 +15143 95829 +88911 12796 +85370 24540 +37936 55116 +27252 62202 +52765 68022 +85421 74199 +29524 26239 +50768 66440 +43558 86684 +51533 89797 +52414 88190 +69357 10930 +37499 82866 +51109 73091 +56299 68022 +35287 90211 +24335 31051 +66593 96407 +34510 24540 +59870 24540 +95114 93204 +25535 45844 +77617 36433 +90219 94315 +79862 38856 +23936 94769 +97001 31743 +80515 77665 +79565 73309 +26333 64279 +75467 90211 +76518 59553 +43602 20354 +36813 99477 +43716 27733 +65228 92202 +23719 94557 +27276 37673 +79341 13382 +92730 64279 +13327 26664 +74695 71838 +26891 51076 +27088 98880 +69958 38856 +91806 17982 +85215 82201 +87804 97407 +87074 38259 +91912 67024 +88017 73309 +37176 26386 +86262 52774 +79370 72254 +59531 72254 +87454 61531 +56438 90211 +69511 28232 +56851 38259 +65430 80706 +36077 17982 +25960 94769 +71485 38856 +54398 90211 +28265 28278 +78120 87123 +59446 34818 +26386 89227 +83452 48947 +37184 37978 +61882 68022 +85743 83130 +58108 93975 +88676 91673 +56709 31769 +60790 80223 +13675 30664 +26508 38856 +86384 45096 +11697 51693 +18506 84802 +64140 26386 +72254 73309 +32457 31743 +47880 57178 +28362 66440 +41762 94769 +52860 13501 +76072 37914 +28664 30385 +38694 83674 +77076 73416 +52304 15737 +48805 97201 +79691 84802 +60446 31743 +50527 13345 +57332 73119 +91969 69182 +24540 42108 +52000 10568 +60350 85617 +65954 75315 +77087 26664 +11673 21083 +86822 31308 +46369 94769 +72162 80387 +78984 15737 +86619 94893 +97668 19111 +98988 92336 +82635 29846 +13455 94769 +58975 80353 +25865 16923 +53039 10038 +30768 15737 +12014 14617 +46545 19623 +25611 38259 +68704 97611 +19940 47269 +86483 63673 +57437 51287 +61703 26386 +48018 51102 +67563 66460 +40899 62325 +39500 34475 +54669 11697 +58288 88743 +74372 50602 +78876 81743 +44932 34577 +63464 81249 +83883 68022 +52588 82924 +32785 67603 +50300 78946 +43503 50602 +64904 84802 +76135 68022 +35611 93677 +64841 34132 +10588 30349 +63814 48233 +19013 17210 +80561 64619 +88208 26239 +10576 10445 +99430 92202 +38695 90117 +32677 30805 +28960 72254 +76150 37024 +27997 66440 +25216 38259 +75079 95383 +19640 32457 +87142 24540 +29691 73136 +43608 60367 +37613 29457 +11187 86291 +14208 97616 +85169 89855 +12976 24540 +64222 52066 +10454 14324 +26231 19580 +94650 15737 +58941 89855 +79692 27316 +54692 10285 +63161 69466 +89688 14267 +88077 93568 +81434 41368 +30007 84802 +93568 72254 +38883 73016 +69231 94769 +14633 72555 +22196 79692 +48676 84802 +87546 84802 +87523 24540 +95107 73309 +79211 15737 +55870 89227 +10953 36166 +11083 73309 +21863 36153 +38429 62600 +61106 92358 +97986 93975 +88704 38856 +93825 66535 +29839 40913 +24546 21464 +74178 15737 +42883 94769 +40996 38622 +42992 26664 +71684 42730 +57251 23426 +26664 31743 +68948 73753 +97206 68022 +57843 33400 +49113 13345 +60920 50511 +22678 89227 +72961 78397 +19802 38490 +86087 90211 +89652 66440 +49821 24540 +50537 79692 +73536 24540 +53929 98400 +60918 79692 +99061 49678 +37062 38238 +20934 72875 +67200 34807 +71415 94769 +99026 96883 +31743 14721 +59957 93818 +68759 76158 +41938 17982 +97993 69305 +80420 85851 +46816 93742 +28271 71400 +87923 32457 +67736 36498 +47677 92202 +56483 84724 +90236 79692 +64649 86922 +55425 46497 +12465 17982 +20096 82924 +60707 96546 +60329 73309 +17905 31743 +11135 17982 +29543 94769 +86447 79692 +77740 97091 +94482 84802 +93699 69588 +26097 13450 +98119 79692 +85496 14796 +86244 73309 +96663 59923 +15846 82924 +85935 72254 +27121 75478 +85411 84131 +30273 32457 +20039 82924 +78703 20706 +41844 64465 +68394 44785 +23329 90139 +58675 96125 +71413 93818 +73282 14167 +50904 89227 +85583 65246 +20025 17289 +47996 18160 +29785 31743 +95739 47118 +79616 92202 +88013 76315 +58837 91798 +78955 92642 +70336 26239 +24170 33987 +78256 24257 +10398 26386 +46571 93031 +93997 79692 +10668 45223 +86866 90211 +49014 53649 +69991 92202 +36977 90623 +92324 79031 +95872 13345 +84633 26386 +30462 24540 +32209 85790 +45036 80196 +17978 68976 +87590 50253 +84802 72481 +39492 68554 +35134 63205 +26840 17894 +75937 10487 +20865 70581 +22548 26386 +73301 56010 +55824 85167 +33501 38259 +97678 30745 +89755 41648 +59158 81505 +51201 66440 +31408 92202 +91451 90800 +70921 29799 +19367 13325 +77381 31051 +91572 89049 +77233 26502 +86139 70861 +60708 35871 +78193 26239 +24930 31852 +14024 82528 +64480 82924 +15775 26239 +75694 94769 +85608 73391 +81409 19623 +57211 26386 +94296 59256 +79582 21822 +59998 38856 +68629 13345 +17800 26239 +37775 44785 +70250 28657 +30006 39357 +80597 35930 +22013 93975 +78422 26239 +38548 24540 +86266 72254 +97848 26386 +84347 56506 +81988 28292 +17114 26660 +11319 73380 +41857 13345 +59624 18015 +75476 33080 +19083 13949 +58992 58123 +60891 32063 +30704 28458 +81706 98995 +65022 87326 +80970 46974 +78758 79469 diff --git a/aoc2024/day01/main.go b/aoc2024/day01/main.go index c0596d8..f87d468 100644 --- a/aoc2024/day01/main.go +++ b/aoc2024/day01/main.go @@ -4,6 +4,9 @@ import ( "bufio" _ "embed" "fmt" + "iter" + "slices" + "sort" aoc "go.sour.is/advent-of-code" ) @@ -20,11 +23,64 @@ type result struct { func (r result) String() string { return fmt.Sprintf("%#v", r) } func run(scan *bufio.Scanner) (*result, error) { + var ( + left []int + right []int + ) for scan.Scan() { - _ = scan.Text() + txt := scan.Text() + var l, r int + _, err := fmt.Sscanf(txt, "%d %d", &l, &r) + if err != nil { + return nil, err + } + + left = append(left, l) + right = append(right, r) } - return &result{}, nil + sort.Ints(left) + sort.Ints(right) + + result := &result{} + + result.valuePT1 = aoc.Reduce(func(i int, z pair[int, int], sum int) int { + return sum + aoc.ABS(z.L-z.R) + }, 0, zip(slices.Values(left), slices.Values(right))) + + rmap := aoc.Reduce(func(i int, z int, m map[int]int) map[int]int { + m[z]++ + return m + }, make(map[int]int), slices.Values(right)) + + for _, v := range left { + if r, ok := rmap[v]; ok { + result.valuePT2 += v*r + } + } + + return result, nil } + +type pair[L, R any] struct { + L L + R R +} + +func zip[L, R any](l iter.Seq[L], r iter.Seq[R]) iter.Seq[pair[L,R]] { + return func(yield func(pair[L, R]) bool) { + pullR, stop := iter.Pull(r) + defer stop() + + for l := range l { + r, _ := pullR() + + if !yield(pair[L, R]{L: l, R: r}) { + return + } + } + } +} + diff --git a/aoc2024/day01/main_test.go b/aoc2024/day01/main_test.go index b55d509..fd8cd41 100644 --- a/aoc2024/day01/main_test.go +++ b/aoc2024/day01/main_test.go @@ -24,8 +24,8 @@ func TestExample(t *testing.T) { is.NoErr(err) t.Log(result) - is.Equal(result.valuePT1, 0) - is.Equal(result.valuePT2, 0) + is.Equal(result.valuePT1, 11) + is.Equal(result.valuePT2, 31) } func TestSolution(t *testing.T) { @@ -36,6 +36,6 @@ func TestSolution(t *testing.T) { is.NoErr(err) t.Log(result) - is.Equal(result.valuePT1, 0) - is.Equal(result.valuePT2, 0) + is.Equal(result.valuePT1, 2756096) + is.Equal(result.valuePT2, 23117829) }