diff --git a/01/input b/01/input new file mode 100644 index 0000000..e8a5bae --- /dev/null +++ b/01/input @@ -0,0 +1,1000 @@ +16435 48069 +29877 97906 +75256 47355 +25417 59861 +32479 25840 +93953 70621 +21297 57288 +41677 60361 +42091 83949 +74831 40059 +53533 90866 +83870 18758 +78929 96272 +85446 75644 +31544 20514 +44640 95346 +34692 16581 +58962 98590 +96508 73793 +51394 82185 +67019 14797 +21518 90866 +62995 55469 +92092 60333 +10928 40778 +34981 54922 +26876 16871 +66391 76794 +35901 72739 +18722 70980 +59497 95346 +53275 28152 +72863 99447 +72352 17518 +26468 24820 +47173 72739 +26320 91005 +45747 59416 +75354 53464 +92639 39605 +64080 70333 +88779 94760 +29791 29540 +14969 24339 +68889 95657 +43718 66328 +60142 37044 +42450 32006 +35427 24596 +93679 54211 +40579 99513 +77933 92955 +37363 58645 +21837 90108 +35039 32006 +62179 52479 +39307 87105 +41332 22158 +94358 79034 +98963 86986 +54717 23049 +43654 87991 +69077 29183 +74844 10737 +91597 95657 +66949 30736 +81313 60629 +90912 58645 +47394 49695 +57684 49541 +19355 51537 +97320 60629 +51077 10528 +84716 17518 +88351 72102 +92708 95275 +26477 81328 +52505 95346 +58645 11050 +23594 85720 +87114 42541 +38462 90479 +65339 97931 +78707 39134 +15918 43734 +41210 56231 +33740 74486 +50265 19552 +18061 65123 +17367 98281 +11451 59861 +88878 53722 +14797 99513 +62073 24339 +65570 84766 +42945 17518 +23455 12294 +25840 58543 +94923 36663 +64165 40778 +89639 68002 +27073 68295 +64190 41338 +95406 23115 +28580 93136 +58531 68295 +17091 39868 +89097 17518 +15175 57673 +70127 72739 +40844 30310 +32925 78574 +49275 63049 +58522 49668 +97797 71228 +29582 25233 +21116 66017 +23705 74772 +28596 52133 +84202 56951 +81833 95086 +21602 41338 +60874 14188 +79507 17518 +92289 83068 +59695 26824 +30349 15180 +76097 37974 +90866 49353 +94385 53723 +70221 44740 +20886 49695 +93814 65374 +67792 97310 +24209 49695 +46911 10601 +52145 23998 +35113 67360 +10340 24820 +23363 21859 +46370 66071 +92407 31362 +57569 35818 +16621 52898 +44760 73118 +91961 16871 +49347 99513 +77133 34172 +85477 27153 +40137 26229 +78890 77054 +19791 50353 +66006 31362 +82274 24820 +77644 62375 +93936 79010 +47149 43188 +36490 68295 +23305 47089 +54503 57531 +95877 94109 +20859 60322 +94959 17518 +77232 96684 +63958 56864 +75367 59264 +15010 16750 +67442 11605 +19373 24339 +12449 16871 +49748 43188 +52620 45936 +47647 26176 +24137 82456 +90427 58020 +73412 91408 +32252 69890 +29261 92008 +74693 32006 +41579 59648 +31429 96033 +56097 46048 +44950 60750 +43971 39856 +59469 12788 +11373 21570 +12061 76677 +65372 19313 +59499 43522 +13640 58645 +26832 91689 +54344 16581 +65273 95657 +33991 52033 +11683 86765 +58927 91644 +39325 71159 +90238 91743 +40526 20631 +26038 67879 +16808 91750 +13799 37351 +37248 29318 +45920 72408 +85648 19390 +66092 95346 +58553 99520 +63800 63316 +56780 58645 +81096 82429 +30787 86285 +59633 61266 +43677 19293 +79162 43188 +13632 43188 +83126 83818 +10649 53368 +54696 74844 +27113 20522 +32739 74844 +49396 26932 +55625 61210 +57489 24339 +96581 60629 +18357 23964 +50387 56444 +78519 68400 +96718 46458 +91448 16871 +20052 60629 +95230 32006 +22052 69248 +90604 25946 +11139 41121 +35546 97243 +58323 89900 +44445 68295 +94442 72739 +16048 58645 +74949 69890 +81915 53723 +31606 45624 +25300 49703 +41338 45059 +30274 57910 +68688 72739 +31971 91743 +19565 70945 +78989 77933 +39992 60452 +87227 36663 +59542 99513 +47044 95657 +40969 37351 +25297 59861 +74790 58645 +86721 16581 +61396 59794 +66410 16871 +96524 14797 +67455 39856 +49695 88656 +93222 60629 +97465 43188 +98283 58100 +24511 37192 +80045 91061 +97212 39856 +14409 78213 +62374 21104 +85065 13069 +24675 37351 +23975 56283 +17562 37351 +82898 69763 +92248 72739 +88802 74265 +93090 62276 +16605 23345 +56872 60629 +42331 44246 +40692 63017 +64053 40778 +56691 91743 +70406 35082 +74671 19741 +81878 73790 +35731 78345 +97104 37351 +98067 63953 +68290 87534 +82210 69702 +21560 90866 +79248 43333 +37000 25840 +82588 16581 +52634 41338 +90100 46437 +16029 91163 +38326 47108 +60226 26252 +77086 37351 +79446 68295 +97224 50705 +17675 68295 +64106 87392 +85496 25840 +75485 99513 +75698 22859 +24342 79034 +46880 54120 +68573 66287 +46608 24820 +25059 51395 +91385 11684 +82361 58645 +11563 63197 +53683 91495 +85007 44104 +56710 15212 +64028 60629 +73189 39856 +44895 95868 +66152 94252 +72272 98068 +79283 39400 +39856 44986 +62116 36663 +77850 24378 +27195 77933 +29717 61825 +58456 75051 +16162 27830 +94771 56372 +72874 76831 +26966 30051 +28969 65366 +79145 35697 +75048 31362 +29692 72739 +86495 36663 +78868 65405 +48543 80206 +48252 36663 +27605 31362 +38669 14797 +76442 29177 +36193 19056 +73037 68295 +34757 17518 +91312 16871 +69099 75003 +44500 84372 +61860 10606 +64698 95346 +39869 95346 +93116 24339 +30369 62058 +65899 93381 +68519 62639 +24048 50583 +47972 43188 +53891 17199 +54451 16384 +31280 12954 +20399 59861 +45783 16886 +98775 34718 +48390 45773 +93583 28085 +96206 30318 +12588 72739 +15698 42059 +97712 24685 +38580 68295 +34395 48669 +23049 13857 +24820 63441 +56864 14051 +46690 69040 +77603 58645 +46878 44485 +47171 62062 +46708 66867 +50525 15967 +50282 67958 +55230 17518 +32552 60629 +41714 40778 +35644 14797 +67623 58645 +56606 12810 +85977 69890 +52701 99767 +60629 49978 +17680 47067 +25372 68295 +98212 97859 +40568 91743 +41527 35470 +10528 12131 +44728 69890 +40026 56864 +15934 18029 +12465 68295 +40634 99513 +59014 22289 +68197 18925 +18082 39856 +75040 77629 +45384 32310 +41916 53723 +35128 52273 +80754 26932 +31342 33095 +27303 95346 +64827 98396 +99513 40642 +41314 39714 +19671 50384 +30923 72739 +45018 24820 +79548 27333 +55411 95346 +47707 44497 +50875 20288 +27914 72739 +19353 19177 +97123 17111 +30579 39228 +51476 29286 +13574 68295 +79359 94446 +48141 70031 +43241 24491 +48136 90788 +87448 59861 +61514 93540 +42728 35079 +83862 74844 +71949 90248 +79924 42545 +20640 37351 +16549 99836 +52489 40778 +89623 84050 +72593 53723 +23289 78424 +82803 60629 +11727 14729 +61214 95346 +12199 31177 +19169 42147 +73807 14797 +11622 26567 +25058 82587 +28775 30058 +91583 72739 +55536 92087 +94095 16581 +72890 30154 +62775 73678 +70295 74844 +41751 50232 +56993 40778 +82031 89970 +74057 92795 +17518 36518 +99629 93756 +61336 39856 +43975 60629 +81289 60317 +69367 91987 +40283 90866 +61911 14797 +95657 58645 +39067 22979 +67250 40444 +23292 98418 +88887 52557 +85949 25840 +38715 93195 +40612 39856 +87534 46295 +70873 95346 +85930 95058 +89567 58645 +93731 14797 +52695 16248 +14578 16871 +46609 36713 +59861 68864 +14621 96581 +20328 42649 +17390 38199 +68295 16871 +41982 16581 +17348 36663 +72246 89941 +90096 30182 +19205 59402 +90078 31362 +90572 20465 +78173 41338 +99825 41124 +84869 48272 +97111 60629 +46975 54184 +82586 24853 +11832 90866 +70440 74844 +13421 24820 +90891 59878 +70480 96457 +65072 17518 +88516 42832 +47360 24339 +87714 16581 +85563 47998 +58866 20085 +46273 99513 +15260 72739 +45102 30648 +29064 24339 +88846 37351 +90349 73175 +98819 16581 +30367 16581 +41951 60629 +86574 14180 +25619 87579 +79116 29532 +57943 95571 +26147 63579 +75102 11022 +87742 41296 +66894 59569 +45617 40778 +23600 53723 +89356 36150 +68062 59438 +57149 23878 +79451 75997 +47353 40778 +74094 89703 +79034 56864 +86009 68295 +81225 63214 +31607 16581 +12367 24339 +50512 79034 +65420 68295 +17083 16581 +80423 53723 +30678 91572 +53711 96581 +52826 39856 +94443 40778 +12923 91792 +19993 72739 +41316 16871 +43188 37351 +41603 37528 +41116 54751 +47492 98390 +52271 59349 +82016 25145 +11995 21137 +87664 95346 +60087 23049 +77487 16901 +77742 99513 +99837 20739 +68435 37491 +62077 68336 +80241 62885 +82671 40778 +81232 95346 +46669 64387 +32390 54356 +49245 60629 +40533 41398 +84643 99513 +39991 18128 +30313 16871 +59426 67766 +47142 99513 +71211 23816 +59935 37011 +31958 92255 +32153 14797 +86833 74758 +52808 43188 +40778 88213 +79022 49695 +87874 82407 +72182 92976 +39448 78503 +97758 40778 +81407 43795 +80992 24339 +19276 84821 +46590 95657 +10895 45077 +80793 51112 +63442 40778 +30847 69890 +32880 51194 +59288 15697 +96900 79034 +63642 34441 +33550 90746 +72357 53146 +97040 96439 +40378 10528 +59435 73396 +31818 22834 +69483 42827 +93889 25840 +11266 27279 +56221 16581 +66149 11829 +96835 41338 +63931 58300 +24339 51351 +95811 77063 +74048 59624 +66188 69890 +20544 96581 +40662 37351 +99514 39856 +71983 43188 +78773 99907 +10964 17518 +56979 59861 +93887 88701 +34552 41338 +27038 71349 +69694 77839 +24942 16871 +18028 16581 +34653 49695 +90330 96581 +98296 57537 +53814 33645 +89964 99513 +37589 17342 +10909 91358 +98527 61419 +32976 28119 +33686 37351 +79495 96581 +45397 53294 +79339 86426 +48269 96581 +67186 90866 +76245 99759 +84154 16581 +84888 51210 +33764 89139 +62796 89821 +42861 22488 +78612 52016 +13839 61720 +40455 95346 +52976 51066 +55173 70350 +47339 87604 +29374 13727 +23406 41338 +84137 99513 +61630 49695 +89925 31362 +80845 75693 +88731 24339 +10621 16684 +23613 60875 +22775 19909 +82644 55448 +55188 68295 +13213 85572 +46020 43188 +38894 22924 +68114 96447 +39872 43590 +84638 35446 +92596 14492 +66910 33854 +63454 81254 +37266 35231 +22257 69890 +89661 16581 +63655 58645 +63891 18254 +85248 39765 +22476 49695 +80842 44794 +10278 97117 +96407 72472 +13688 69809 +20190 50894 +16581 81145 +44118 16735 +44289 61819 +83096 66134 +78920 67405 +30294 22869 +12953 87534 +58065 40778 +80257 41656 +65705 79885 +36010 96068 +12082 33841 +57211 54993 +37556 60629 +70791 39856 +83423 90866 +69501 99513 +75688 95346 +19823 31534 +38253 58645 +38183 33154 +32520 72739 +65756 17518 +62670 12393 +17780 24339 +39071 69840 +90304 55306 +23602 69069 +40739 17518 +46361 87753 +15902 17518 +37784 40225 +30916 42636 +23707 99099 +91290 60611 +33864 60629 +93983 78860 +35469 30176 +21070 24339 +83225 14434 +81426 30459 +83168 49695 +20409 69540 +57527 41338 +49574 48918 +36663 64740 +60394 55739 +87952 77933 +87075 42596 +48587 15230 +69841 40778 +50428 41338 +61418 55126 +99136 64249 +66778 30866 +23040 15532 +79526 91743 +37251 36885 +67189 23898 +47448 69375 +90585 40778 +98236 52391 +63822 22592 +19611 30041 +99334 17027 +78062 33410 +67694 18537 +29196 30111 +85703 93883 +62167 58645 +93337 36476 +77737 80458 +96370 13575 +42996 18247 +23203 46338 +72739 26537 +59099 17518 +13739 80947 +56865 21685 +72378 95346 +86826 96979 +18269 42316 +57660 49695 +47959 43002 +36489 39334 +50097 95346 +13746 95122 +48963 53723 +30101 17518 +83534 73195 +59841 80333 +69877 53999 +92081 18047 +49205 50055 +48937 13416 +16871 78838 +80567 99399 +64382 72739 +36178 71044 +55868 49695 +46804 48607 +76304 58645 +38942 47637 +56622 31362 +67077 26932 +22630 10374 +73652 99587 +24594 16871 +40607 95967 +24466 48026 +99685 45844 +41070 32483 +53109 91914 +13303 24079 +90567 95346 +63113 38867 +69961 58038 +71621 30015 +48447 51612 +39391 47825 +31318 79034 +54449 69890 +95458 12438 +17168 25163 +59625 57006 +33979 13374 +52341 77943 +31362 95866 +17105 90866 +58391 99513 +59577 89128 +56230 55488 +95001 32006 +60469 78894 +54419 60629 +63827 93358 +98150 35728 +33965 78590 +42272 53373 +27833 18242 +82723 52358 +50442 16871 +63005 29081 +80894 11880 +90215 85310 +93510 41660 +11301 68295 +76924 58645 +96481 20952 +65294 53723 +36656 82851 +28496 68673 +14320 68295 +46083 45432 +22802 39856 +98104 98886 +67972 39856 +10720 24339 +83626 37351 +67343 45562 +96312 99513 +31279 74844 +26421 85831 +84257 32937 +16680 21022 +20670 19227 +62741 95346 +59245 98417 +67916 37351 +89880 51840 +88768 54835 +33728 31362 +64155 37351 +17490 37351 +97853 58367 +21580 59627 +50349 72739 +17014 10528 +95346 92967 +15226 43188 +26310 68295 +50644 68295 +25134 59861 +79187 36085 +21594 81212 +59975 88554 +14723 76634 +59988 95657 +44916 73103 +25459 53723 +99883 17518 +91474 38109 +50240 72739 +90665 40778 +41306 41338 +20093 40778 +77543 84251 +50058 27789 +84976 91503 +25891 68176 +46484 60629 +97473 35979 +86274 69890 +38121 59861 +15406 76925 +74418 39631 +14474 12043 +59783 57253 +37351 80209 +33242 29891 +32795 85969 +86292 27806 +14090 27607 +31394 94227 +17591 67502 +22263 47087 +17909 39856 +37774 72113 +67198 25501 +25288 74844 +48203 91565 +99738 35641 +18706 58645 +54532 68295 +18588 60629 +79083 69890 +86349 31362 +93820 41744 +18344 69890 +80025 52503 +32006 74591 +59686 33314 +10486 97011 +55016 98273 +57123 38719 +72794 24339 +87491 50497 +79579 53723 +65429 24980 +31765 16581 +73697 17518 +91743 97390 +21682 37461 +69890 12585 +72614 19372 +88539 32006 +45679 73733 +41030 91552 +40402 74844 +32675 57436 +40058 58645 +10228 16871 +67908 36615 +32413 12311 +73185 63638 +68526 84874 +49242 95657 +80390 41338 +94376 58645 +47392 77933 +91154 90866 +58956 98634 +81713 45892 +41037 85888 +80016 46343 +62931 53723 +75384 40778 +47227 11574 +99463 32006 +53299 40778 +49977 68975 +49678 49695 +54480 26569 +50457 50082 +53723 41338 +99495 56029 +41074 38257 +90818 72739 +26932 74844 +49941 71563 +16356 37996 +76996 56074 +11524 85522 +30416 90866 +91352 61520 +24977 49695 +49451 89237 +21400 39529 +87568 37175 +96017 95346 +90873 68295 +91037 47664 +75674 99513 +11356 75031 +66871 37296 +33330 86365 +66278 96190 +18682 72272 +42394 36663 diff --git a/01/main.go b/01/main.go new file mode 100644 index 0000000..e7dd0a2 --- /dev/null +++ b/01/main.go @@ -0,0 +1,93 @@ +package main + +import ( + "fmt" + "os" + "sort" + "strconv" + "strings" +) + +// Given a list of paired integers with columns representing two groups, +// compute the total distance between pairs in the sorted lists. +func main() { + + // open input file + + input, err := os.ReadFile("input") + if err != nil { + fmt.Println(err) + } + + // Walk the list and create two lists, left and right + left, right := extractLists(string(input)) + + // Sort left and right + sort.Slice(left, func(i, j int) bool { + return left[i] < left[j] + }) + sort.Slice(right, func(i, j int) bool { + return right[i] < right[j] + }) + + // lFreqMap := frequencyMap(left) + rFreqMap := frequencyMap(right) + + // Walk through left and compare each element to its pair in right + + differenceTotal := 0 + scoreTotal := 0 + for idx, val := range left { + var l, r int + l = val + r = right[idx] + + // Sum distance between elements + difference := l - r + score := l * rFreqMap[l] + + // Take absolute value + if difference < 0 { + difference = -difference + } + differenceTotal += difference + scoreTotal += score + } + + // Return total distance + + fmt.Println(differenceTotal) + fmt.Println(scoreTotal) +} + +func frequencyMap(slice []int) map[int]int { + frequency := make(map[int]int) // val -> score + + for _, val := range slice { + frequency[val] = frequency[val] + 1 + } + + return frequency +} + +func extractLists(input string) ([]int, []int) { + var left, right []int + for _, val := range strings.Split(input, "\n") { + // fmt.Println(idx) + t := strings.Fields(val) + if len(t) < 2 { + continue + } + l, err := strconv.Atoi(t[0]) + if err != nil { + panic("aaa") + } + left = append(left, l) + r, err := strconv.Atoi(t[1]) + if err != nil { + panic("bbb") + } + right = append(right, r) + } + return left, right +}