Ver Fonte

Solved part1

Piotr Czajkowski há 3 semanas atrás
pai
commit
b3287396c0
2 ficheiros alterados com 1067 adições e 0 exclusões
  1. 67 0
      01/code.go
  2. 1000 0
      01/input

+ 67 - 0
01/code.go

@@ -0,0 +1,67 @@
+package main
+
+import (
+	"bufio"
+	"fmt"
+	"log"
+	"os"
+	"slices"
+)
+
+func readInput(file *os.File) ([]int, []int) {
+	scanner := bufio.NewScanner(file)
+	var lefts, rights []int
+
+	for scanner.Scan() {
+		line := scanner.Text()
+		if line == "" {
+			break
+		}
+
+		var left, right int
+		n, err := fmt.Sscanf(line, "%d %d", &left, &right)
+		if n != 2 || err != nil {
+			log.Fatalf("Bad input: %s", line)
+		}
+
+		lefts = append(lefts, left)
+		rights = append(rights, right)
+	}
+
+	return lefts, rights
+}
+
+func abs(a int) int {
+	if a < 0 {
+		return -a
+	}
+
+	return a
+}
+
+func part1(lefts, rights []int) int {
+	slices.Sort(lefts)
+	slices.Sort(rights)
+
+	var distance int
+	for i := range lefts {
+		distance += abs(rights[i] - lefts[i])
+	}
+
+	return distance
+}
+
+func main() {
+	if len(os.Args) < 2 {
+		log.Fatal("You need to specify a file!")
+	}
+
+	filePath := os.Args[1]
+	file, err := os.Open(filePath)
+	if err != nil {
+		log.Fatalf("Failed to open %s!\n", filePath)
+	}
+
+	lefts, rights := readInput(file)
+	fmt.Println("Part1:", part1(lefts, rights))
+}

+ 1000 - 0
01/input

@@ -0,0 +1,1000 @@
+77221   93653
+61169   27995
+49546   69782
+11688   41563
+15820   48282
+63235   37517
+28850   68189
+80735   25255
+42616   65322
+55068   99897
+30851   35509
+14907   49013
+64732   92011
+16709   68830
+67643   36207
+44080   23559
+96480   58330
+99450   40186
+21169   63410
+58096   21671
+53828   79572
+47026   44529
+90101   28656
+45773   73412
+71303   85991
+45304   78294
+96528   23559
+19794   17882
+74831   17211
+88912   11644
+98961   80435
+15788   96101
+16350   86995
+96806   19028
+99959   77868
+58887   90255
+42568   25255
+29278   88912
+94028   63269
+42810   21548
+97490   71747
+89051   24477
+73916   31808
+97751   88912
+73608   77868
+62426   13754
+29429   36177
+11070   10894
+78656   85185
+31308   60500
+96166   91114
+36906   85134
+99662   77868
+66402   41563
+37085   14475
+80907   53324
+61650   53324
+84223   63753
+83891   47827
+15973   77536
+68843   40593
+98854   31274
+42426   97404
+25255   67531
+44559   50141
+93909   50674
+28070   32858
+24880   24813
+29338   89795
+64883   77868
+66924   57646
+32984   66502
+32238   39062
+38816   23559
+58770   65322
+74953   91636
+70592   19028
+93111   11644
+31065   23537
+67370   19028
+18314   47412
+71075   74334
+21544   44529
+59137   50980
+20440   80749
+14393   38495
+72130   90255
+43828   68830
+23262   73660
+11923   50980
+47754   14164
+96043   34362
+27162   28090
+20838   50713
+75009   11121
+92099   31274
+22054   38248
+43518   80106
+68189   13153
+50520   12727
+51425   40674
+42998   31053
+52443   33051
+37256   54436
+16648   95555
+81123   17329
+46974   79835
+36328   77868
+76373   23559
+82592   70046
+70600   22417
+27116   89690
+28090   34969
+86348   23559
+53145   50980
+30203   83301
+23347   77681
+76433   26236
+28046   92869
+58920   50980
+25518   28090
+12791   44529
+61967   31808
+98022   53056
+24326   12263
+41095   13121
+24045   82448
+31274   16530
+78021   21589
+63029   19658
+75684   14475
+31258   36493
+40561   48589
+20457   54974
+14783   88465
+39433   23537
+34774   53324
+10455   14329
+26338   74416
+55227   37003
+36963   40380
+31910   68189
+94894   18220
+23166   44111
+48884   46787
+73036   89795
+83519   25205
+50205   93653
+85926   50141
+69194   23559
+25529   11642
+10182   31274
+13638   61987
+83588   19028
+75911   20768
+44692   24325
+43278   94888
+48148   24394
+86205   99327
+82605   58818
+52770   68189
+89068   84917
+66452   83679
+50217   85099
+64914   52073
+76460   78294
+43026   24410
+44179   50980
+47653   90255
+78061   50980
+14475   71478
+77868   37150
+99645   28090
+86267   70055
+58072   68830
+96428   66585
+77501   50141
+52344   93653
+49534   35509
+46925   12437
+40005   87987
+17329   86995
+17585   89795
+32296   59660
+81836   11644
+89435   65322
+55149   59423
+56971   50141
+26342   95142
+57894   56227
+49762   33007
+48949   65641
+40453   53505
+67613   23537
+27110   87987
+25231   63910
+92546   50713
+70074   27554
+39649   93653
+93231   30520
+60008   79179
+18449   47209
+68828   36207
+18908   72254
+55426   44661
+62522   87987
+60923   25255
+85721   68107
+35153   71814
+20053   24813
+24233   22904
+36237   92731
+92077   68189
+30105   50674
+37032   90255
+12745   54116
+41563   53324
+24700   29145
+60226   85335
+50713   64775
+29972   51790
+79176   19794
+40017   96101
+90724   41345
+51608   39176
+46178   50141
+26151   23489
+19569   44529
+34368   86052
+18948   50801
+62091   17329
+89749   38984
+68315   37514
+52396   19794
+14981   14475
+36038   69571
+50048   50980
+60796   41563
+69942   72877
+80976   25234
+56722   90255
+49489   28090
+60768   65557
+70753   85065
+15233   23522
+94697   87600
+40958   41532
+91709   40116
+83586   19028
+88140   24977
+56993   50980
+17980   53324
+12421   97982
+30183   40884
+50452   53324
+91314   90651
+69995   19028
+16588   28090
+22432   98658
+86088   77868
+46361   65322
+97178   75705
+37444   79320
+12275   96101
+47016   16942
+36822   87327
+62082   31274
+68265   64314
+87344   24603
+40642   23559
+87254   38755
+62372   50713
+89026   68830
+88519   96101
+53514   31808
+24686   41563
+23817   68705
+26704   89031
+37908   65322
+22665   35601
+75378   59200
+16797   86995
+55847   68189
+55932   72688
+41470   96101
+43557   17329
+45439   77868
+41230   76226
+25104   92869
+43736   93653
+85938   93653
+74153   54726
+73956   70350
+70419   37102
+81275   64160
+30656   32684
+46218   32617
+67969   31808
+61544   81806
+39376   56568
+14769   60336
+10199   97703
+61064   40091
+75531   30486
+17211   31808
+75184   74633
+24565   61765
+83922   44529
+11644   25255
+71836   14393
+53030   19028
+83437   33190
+33687   54998
+92054   82083
+48250   68533
+10977   34363
+51050   19028
+80898   68276
+26442   68830
+39875   65514
+24976   76417
+31946   23537
+95278   90255
+71678   26450
+49641   28580
+12962   13674
+13598   30362
+40620   21256
+49062   92869
+88449   79529
+80153   94081
+36918   70556
+77902   99228
+41848   99097
+26032   39378
+23830   50980
+49712   78294
+95464   65322
+76971   27393
+67074   22132
+17629   71246
+92019   92869
+20780   96521
+68372   16880
+49710   88060
+94680   52634
+28626   57101
+31808   60809
+25578   28090
+12020   25255
+46008   33046
+27712   36207
+17168   53033
+92923   87987
+94668   65843
+59490   48858
+69085   38485
+28242   14664
+18302   96101
+64047   71241
+62752   73532
+28873   59278
+22206   18029
+57298   85374
+26420   78294
+74055   30817
+74377   45566
+81846   17058
+35184   27305
+72054   89795
+78666   41563
+80545   96101
+69240   51322
+76319   36831
+23617   14475
+53848   93653
+65082   36888
+18802   93491
+63942   23559
+80208   22876
+17018   68830
+78018   51611
+35520   75130
+43479   88543
+18522   96101
+41260   15010
+58041   21675
+96272   68830
+73601   78294
+44788   52104
+67152   89795
+57671   61399
+33841   56764
+41963   53394
+41010   28090
+53937   65322
+75472   76675
+22253   92068
+24277   93653
+62012   14475
+27539   50141
+29180   41637
+27151   35922
+28752   23559
+31602   47673
+80298   50906
+94689   38297
+87987   48139
+51692   61967
+93653   86995
+99694   96638
+50948   11644
+87830   19794
+57579   93403
+60947   72082
+18514   53324
+99194   25255
+57630   93653
+49914   10526
+34613   94340
+32145   16105
+39569   62764
+14118   31274
+38901   19016
+47443   66925
+83812   31274
+61920   50141
+88684   50713
+32305   31808
+58695   50713
+20016   42720
+68955   31976
+96299   99509
+83258   96101
+49424   68830
+97130   14043
+34668   50936
+27567   35509
+57890   65959
+58399   78860
+88934   50980
+70656   66788
+91507   44529
+53671   87987
+75858   71137
+14726   13674
+25416   31274
+90521   50075
+37289   96101
+79415   19028
+21158   25355
+53210   16168
+35192   81526
+67240   23559
+42967   65310
+81598   93094
+12765   36207
+23853   41643
+78439   22984
+13052   50141
+56642   93653
+19028   82791
+89371   36244
+89795   50799
+41542   90489
+49486   72630
+26630   50763
+35019   68830
+62271   78294
+16433   77868
+42466   57338
+63666   39984
+56231   83564
+51132   96101
+33675   74200
+94670   70445
+51236   17822
+52493   64460
+45346   86995
+41620   31274
+80479   88912
+46200   90255
+13789   60686
+98564   76843
+44974   92717
+76047   80674
+60650   89430
+55723   45722
+89426   97853
+33961   98414
+32872   68189
+65322   12029
+29922   36159
+74923   34697
+37065   59688
+66622   23559
+68253   14475
+28664   53324
+62981   25761
+83161   50674
+22849   50980
+11119   76308
+22980   13794
+76171   19411
+96351   28221
+45573   90255
+19850   79146
+97828   90483
+23867   85271
+36353   50141
+46579   87987
+73434   23559
+57169   68189
+68768   41682
+59354   97617
+62181   64641
+34167   53324
+28374   24803
+55863   57352
+25058   33040
+76126   53005
+97283   51641
+25887   29253
+43067   50713
+51616   61868
+97533   16129
+50692   80951
+43173   41626
+93917   76055
+84713   53465
+24130   13599
+56773   50980
+15310   44529
+63375   50141
+75875   93653
+22284   33135
+46294   77836
+29713   14393
+95318   83346
+83264   61967
+39267   47771
+61101   86995
+47882   96101
+24345   70347
+50674   14475
+17296   12880
+52605   11644
+40817   78294
+36207   93653
+16045   96101
+91784   68830
+34583   69934
+71867   28090
+74898   22671
+54609   27206
+33070   26257
+18570   57394
+16766   87036
+62825   21503
+39793   17504
+12038   82452
+37265   12706
+69479   44529
+96101   44260
+49175   50713
+70708   55412
+50136   14475
+27300   96101
+75794   62368
+10277   87990
+30825   87987
+35563   24149
+12505   91890
+11323   14393
+89922   84455
+10927   65359
+50656   71304
+47973   11644
+17156   92869
+98964   96101
+50980   59345
+94733   23559
+22743   26134
+90255   54273
+80006   65322
+10157   92869
+70496   28090
+25569   41226
+79982   92869
+34897   76485
+83357   86388
+26352   79503
+67432   15174
+49155   72153
+36311   86995
+14394   65333
+70928   51667
+67367   23559
+48195   57003
+52001   26154
+63251   21651
+21894   89795
+50774   43252
+85637   36207
+79243   11644
+49512   57954
+22454   15919
+84214   20805
+67121   86995
+49247   78035
+79313   34840
+33168   19814
+48657   92888
+59824   74905
+30088   68830
+74246   90052
+78389   65322
+88418   28090
+93716   62226
+74962   29324
+47309   50980
+93517   14393
+23895   68175
+34548   25255
+32628   84730
+55988   31274
+82578   40953
+51728   16926
+19440   27805
+42922   45159
+52930   41563
+91562   31274
+23559   62680
+92150   26879
+78315   61783
+43601   91325
+87621   17838
+78580   65350
+94778   77868
+68519   83273
+35509   66389
+82288   52439
+65083   19794
+86995   13010
+66618   95850
+18656   70145
+98537   65322
+60842   52161
+68830   10132
+99689   24077
+33740   89795
+45595   53090
+78239   36207
+20185   36207
+87307   69366
+83009   77868
+34685   88642
+13674   28090
+82450   69909
+44812   75146
+54296   92869
+73558   94990
+21422   48229
+49473   10547
+66108   68189
+71169   74202
+78276   40690
+82977   78294
+61842   15634
+31582   95459
+39064   14475
+70204   18593
+71244   87987
+31642   50490
+98413   37109
+61175   50141
+37168   23559
+44093   11032
+18806   40848
+91249   73483
+95812   32572
+93644   68189
+30669   20850
+75404   92869
+53324   50141
+80675   50216
+14897   73401
+34074   29952
+16366   32630
+95656   28090
+73964   58896
+90705   66916
+92869   59662
+99522   78294
+35903   19794
+33033   42610
+55650   50980
+29289   11753
+32302   58345
+76036   57644
+74870   31274
+85035   77868
+79322   25255
+50332   31274
+78294   44529
+93000   53324
+36560   19028
+40041   53525
+70360   89795
+19074   98749
+32829   89603
+61436   71046
+89938   85369
+52708   28090
+83958   53324
+88155   67097
+32730   75527
+64221   23559
+60391   97432
+70047   20185
+12372   73527
+46501   29274
+66552   14746
+56373   51614
+95596   11782
+81196   72623
+32282   60515
+49504   14393
+57642   34348
+95402   87987
+81625   68830
+28527   37199
+71475   31808
+94280   23005
+55623   99355
+46783   29073
+36641   81559
+74788   14535
+87994   78773
+80189   77868
+33263   89795
+23537   23537
+25755   22264
+67413   72800
+74924   73774
+81215   78294
+78206   92869
+36161   50141
+54925   86995
+66305   49951
+42462   28090
+99051   88912
+44954   89795
+88856   28090
+63026   85778
+22651   24813
+39262   14475
+86176   69982
+65395   14393
+12926   78294
+81994   53324
+16933   30191
+46987   19794
+37215   28090
+15340   63748
+40271   86674
+23747   52856
+61156   11644
+65356   56048
+72336   51278
+21864   77868
+99351   13349
+24599   79046
+62281   23361
+81871   53324
+14029   42297
+50141   48720
+46677   43636
+10745   35965
+39344   80864
+38077   50038
+13983   89795
+66663   67877
+64175   19028
+62355   68830
+96823   79443
+46436   44754
+53388   68830
+45987   92723
+87646   11707
+75997   45545
+69968   93653
+30425   44529
+83211   88109
+28725   64932
+47043   89795
+34475   31274
+42302   94895
+66558   92753
+48964   50713
+19546   78294
+51026   89795
+89346   57408
+52993   54676
+29577   38385
+85932   60320
+18030   22556
+65993   19922
+74692   23559
+61588   89795
+50688   69541
+93760   44529
+75094   96101
+68976   86835
+46278   50980
+97836   44529
+17576   32708
+48519   46071
+72647   93653
+54724   34155
+45186   43448
+44529   41828
+90404   86357
+63175   44529
+88381   25963
+32140   23537
+43348   36423
+41733   88912
+84065   98953
+20389   96101
+38749   93788
+37929   35143
+63747   35340
+72249   93653
+98042   59381
+22741   95783
+88124   33425
+16493   44529
+36599   69408
+50436   41563
+67695   86149
+89091   20022
+60708   92607
+28750   68830
+80224   39113
+34580   96101
+79688   34798
+14870   68189
+47719   29252
+57652   33458
+65541   87987
+86435   19794
+95204   81239
+43800   50980
+94063   11644
+24096   36588
+36628   95478
+81443   17329
+61221   57166
+52782   78294
+16740   31274
+68369   45653
+28895   44529
+39222   65582
+97201   77602
+81119   11644
+41083   50141
+72917   25255
+46148   21269
+44495   18020
+99464   90525
+67254   58381
+52750   28135
+63899   55754
+14617   50713
+36962   50713
+41322   73436
+22504   38709
+72577   39875
+67799   68830
+68284   71156
+46011   61967
+50367   31808
+45832   87987
+88775   87987
+32562   26340
+43756   49887
+73444   16547
+67302   92449
+22133   85451
+17768   77868
+69281   39885
+87364   25255
+41209   27363
+37792   77868
+66667   73746
+22195   28090
+65037   37556
+28634   89795
+23901   50674
+40511   95801
+47779   45253
+76489   86807
+77873   66992
+87523   35509
+32876   50980
+62342   50980
+84222   41563
+88205   40382
+78856   20051
+97888   68189
+22233   17329
+35868   81437
+80155   93653
+82730   78294
+12733   19794
+55038   26258
+50682   23250
+42043   74279
+54719   71512
+54474   23991
+61776   25255
+55564   41563
+39404   31274
+94954   14475
+76258   89795
+87689   67995
+65619   59930
+41846   17435
+21990   22185
+60161   53026
+44955   57523
+84178   39875
+32693   87987
+19125   44529
+96731   93653
+98080   44529
+72947   95746
+67285   23344
+78101   77580
+43064   11644
+81350   23483
+46937   11644
+97686   79381
+66339   60744
+39269   50674
+61837   77868
+97647   86995
+44613   93568
+73127   68830
+64048   80370
+15530   87987
+63926   51860
+95853   91981
+26155   28090
+82222   19794
+10035   87445
+16445   93653
+67972   15558
+51780   68830
+52358   31274
+83942   60096
+64899   11955
+56009   96101
+42288   36590
+53349   65322
+76255   50713
+53044   26351
+37184   31274
+24813   19028
+62309   52490
+93607   19028
+27423   45568
+26128   78294
+72691   50141
+42162   60052
+78247   10613
+80209   31808
+38310   93653
+59228   47607
+24897   23559
+55176   37760
+33062   53324
+26521   39170
+93209   68189
+89881   23559
+11418   50980
+16884   78294
+31995   50980
+17858   98671
+49107   96101
+50512   54526
+56890   65322
+76173   65309
+51569   52845
+73919   23537
+62591   16739
+53735   79935
+14454   69804
+19976   46609