Day9
This commit is contained in:
parent
75a4a6a556
commit
0df7f2592f
|
@ -0,0 +1,128 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"AOC2021/src/helper"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
//0 -> Unknown
|
||||
//1 -> true
|
||||
//2 -> false
|
||||
type field struct {
|
||||
height int
|
||||
lowPoint int
|
||||
basin [2]int
|
||||
}
|
||||
|
||||
func main() {
|
||||
args := os.Args[1:]
|
||||
input, err := helper.GetInput(args[0])
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
heightMap := [100][100]field{}
|
||||
for i, line := range input {
|
||||
for j, r := range line {
|
||||
heightMap[i][j] = field{int(r - '0'), 0, [2]int{-1,-1}}
|
||||
}
|
||||
}
|
||||
sum := 0
|
||||
amountOfBasinSet := 0
|
||||
//find Lowpoints
|
||||
for i, _ := range heightMap {
|
||||
for j, _ := range heightMap[i] {
|
||||
checkFieldLowpoint(&heightMap, j, i)
|
||||
if heightMap[i][j].lowPoint == 1 {
|
||||
sum += heightMap[i][j].height + 1
|
||||
amountOfBasinSet++
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println(sum)
|
||||
|
||||
//find Basin
|
||||
for amountOfBasinSet < len(heightMap) * len(heightMap[0]) {
|
||||
for i, _ := range heightMap {
|
||||
for j, _ := range heightMap[i] {
|
||||
if heightMap[i][j].height == 9 && checkBasin(heightMap[i][j]) != 2 {
|
||||
heightMap[i][j].basin = [2]int{-2, -2}
|
||||
amountOfBasinSet++
|
||||
}
|
||||
checkFieldBasin(&heightMap, j, i, &amountOfBasinSet)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
basinMap := make(map[string]int)
|
||||
for i, _ := range heightMap {
|
||||
for j, _ := range heightMap[i] {
|
||||
if checkBasin(heightMap[i][j]) == 1 {
|
||||
fieldBasin := heightMap[i][j].basin
|
||||
basinString := fmt.Sprintf("X%d Y%d", fieldBasin[0], fieldBasin[1])
|
||||
basinMap[basinString] += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
rankedMap := helper.RankByValueAmount(basinMap)
|
||||
fmt.Println(rankedMap[0].Value * rankedMap[1].Value * rankedMap[2].Value)
|
||||
|
||||
|
||||
}
|
||||
|
||||
func checkFieldBasin(heightMap *[100][100]field, x int, y int, amountOfBasinSet *int) {
|
||||
if checkBasin(heightMap[y][x]) != 0 {
|
||||
return
|
||||
}
|
||||
directions := [4][2]int{{0, -1}, {+1, 0}, {0, 1}, {-1, 0}}
|
||||
for _, direction := range directions {
|
||||
px := x + direction[0]
|
||||
py := y + direction[1]
|
||||
if py < len(heightMap) && py >= 0 && px < len(heightMap[0]) && px >= 0 {
|
||||
if checkBasin(heightMap[py][px]) == 1{
|
||||
heightMap[y][x].basin = heightMap[py][px].basin
|
||||
*amountOfBasinSet++
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//0 no Basin Set
|
||||
//1 Basin Set
|
||||
//2 no Basin Exists
|
||||
func checkBasin(inputField field) int {
|
||||
fieldBasinX := inputField.basin[0]
|
||||
fieldBasinY := inputField.basin[1]
|
||||
if fieldBasinY == -1 || fieldBasinX == -1 {
|
||||
return 0
|
||||
}
|
||||
if fieldBasinY == -2 || fieldBasinX == -2 {
|
||||
return 2
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
func checkFieldLowpoint(heightMap *[100][100]field, x int, y int) {
|
||||
if heightMap[y][x].lowPoint == 2 {
|
||||
return
|
||||
}
|
||||
directions := [4][2]int{{0, -1}, {+1, 0}, {0, 1}, {-1, 0}}
|
||||
fieldHeight := heightMap[y][x].height
|
||||
for _, direction := range directions {
|
||||
px := x + direction[0]
|
||||
py := y + direction[1]
|
||||
if py < len(heightMap) && py >= 0 && px < len(heightMap[0]) && px >= 0 {
|
||||
if heightMap[py][px].height >= fieldHeight {
|
||||
heightMap[py][px].lowPoint = 2
|
||||
}
|
||||
if heightMap[py][px].height <= fieldHeight {
|
||||
heightMap[y][x].lowPoint = 2
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
heightMap[y][x].lowPoint = 1
|
||||
heightMap[y][x].basin = [2]int{x,y}
|
||||
return
|
||||
}
|
|
@ -0,0 +1,100 @@
|
|||
5678998989432198943298876799876586789998999434988989999876544345789987654545678931299765458679432456
|
||||
4556987678949987899097655679767345678987898949876579899987432134889987653237889542989654323488921347
|
||||
3245696569298765678976544598653234567896567899885467799876598245678998542126898769878943212367892356
|
||||
7656789689019654569895432987743125678923456998764345689987987656789987632015789998767932103456789969
|
||||
8767898789298965698789321296532012789912367897651234993398999867897898543234567897657899344868999898
|
||||
9898909899987976987679434987432123567893458976543349892109234978935987654945678956545888956779019787
|
||||
2999312999876899876548976798543256798954578987985498789290125989124698878876789545434677897892197656
|
||||
1098929989765678987537897899674345689765989299876989687989234998995789989989895432123456998999976545
|
||||
2987898763234567895426899999965457999879999101999878576978949876789891096593987643034587899698765434
|
||||
3496987654345979971015678989876598943989898999998765465767898765678992987432398854234567934569876745
|
||||
4985499795659898965434599575989699432396797878999874323456789654567989989321239954346789123456987659
|
||||
9875324989798787896545789434798789101234986767899983212345896543459878976310198765457991014569999868
|
||||
9766219878989656789659897321679893212379875456798432101236998532598767895421239876678962195998941979
|
||||
9854398768878968998767896432567954523459864323987543212456897431999656789532345987789653979896532989
|
||||
8765498656867978979898998543459995745569876434598798654567896569898545678945478998999969867789549899
|
||||
9876986545457899656979999876598989856978987945679898767878987798767634599999567899019898656678998789
|
||||
9989978432345898789767899987987679997899199896789979878989498987656424989987678999198776547578999678
|
||||
8998764321256789995456789999976567989921099798996568989595349998843213577898789878998652123469996578
|
||||
7987653210127897654365898998765465678932989699765439995430157898632102356999896569986541012345987989
|
||||
6398865432238976543234567899901234567899876587996549899321298976543214467899954398997632123657898994
|
||||
5219976554349899432129698999893345678998765346789659798932349998654323578999876497989543234567899543
|
||||
4323987665656798921098989998789556989987654223878998687893499969875987679987988986578994547678978932
|
||||
9495999877897987892987878987678967898998743012567896546979987855987898789876599965489789659789459891
|
||||
8989892998999876789976567897567998957989432123456985434569756234598939898765498744345699967891298789
|
||||
7678793459998965678975456976456789549876543254569876324598542123459329999654399534234898898999987678
|
||||
6547689567987854769894345994347695434987656767878987765987643044678998998765987620123456789998976567
|
||||
2134578979876543456789234789234589323698787878989998987896543235799987989976798731245977898987894348
|
||||
1023456798989874767890125696345678934569898999598999199987654376789876476799987654356898987656791239
|
||||
2434587997898765889931459895456989545679999545457889298998775479898784345678998767487929876545789545
|
||||
3679679876779976789432345796587997656798789432345678987989986567987673234789769876598934987656899696
|
||||
4598798785669899996543456998798998787989699421234567896767897678976543125898654987679875898967998989
|
||||
5689987674357698999876567899899899899876568994365898965658998789985432016789543298789876789878987977
|
||||
6898998543234567899997878965956789921965434589456789984347899899876543123895432109897989898989995456
|
||||
7987899932123456789398989954345997899875325678987896543256789976987754544789549212976799997699984378
|
||||
8996998761014767891239997896456896898765456789198987632149899965599865765678998999765678986569876467
|
||||
9965987654145678910949876789587965789876567991019996543234999843479876878989576788974599875454987678
|
||||
7894398743234599999898765678998954678987878992398897755345898764567987989995435567893987654323499789
|
||||
6989219894346789987769989789989563867999989989987789976456789879679998999876323467932198763212599890
|
||||
5678923998456799976545699899876432345693299979876667896597999998998999998987212348942019874343989921
|
||||
4567894986568897895434567921987321276789129864985456997678999987897999987658301239953198765454678934
|
||||
3656789997679936789323989210995490987899298743234357898789878546786789876543212367893239887565789995
|
||||
2345678998789325679999898929876989898998987654101256789898765435545678987665325456789357997676799989
|
||||
3476789019995434569878787899989978789997987643214347999999854321236789898775434567895467898789989878
|
||||
4567892129876765698765676789998767679876987654765656789998763210145698769887655698986578969899878767
|
||||
5688943436987986997654565678987654598765498769876777899989954321234789656998767899987699656998766456
|
||||
7899894545698999876543234567896543987654329989987888989679895452345678946989878957899988997987654345
|
||||
9956789656789123987632123479987659876543213597898999876565789667458789239878989745987567689998765456
|
||||
2345678967894234696541014578898767998632101456789767987434578978569890198767897659986456578909876567
|
||||
3456789878954356986532123456789878996543212347890345898323467999998932987656789798765343469219989878
|
||||
4567896989996969897543234587990989987654323456921256789212356989897949876545678999973212458998596989
|
||||
8689965699989898798656945678991399898785434567892345892101249876786899865434567899865393567967445698
|
||||
8792123989876767679987898789789989789897545679953556789234598765645697654323798999976989998953234567
|
||||
9893439876565654567898989897678976567998976789767787896545987543234789763212346789899877899762146788
|
||||
1999598765434123456999878978567985445879987899879898987657998652101679654101235898765766794321017899
|
||||
2398987654321014567894569865379876234567898999989999698769876543213568969233346789654545689987634799
|
||||
9987898998432123798923498765456989445789999998993987569899989984623479598944567896543234568976545678
|
||||
8765659996543235689212349878579876556895698987894595456989999876534689457899698965432124589987678989
|
||||
5434347987656345678901656987689987667894987656889696579878989987678994345678969999943012679799889998
|
||||
6521236798965456789892997899893198789989876645678989698769878998789101234589456987899323569654999987
|
||||
7632345899878569898799789910999099895469985434569878997645568999893214456678967965678954578967998976
|
||||
6543656901989689987687678921398987994398996945698969876431467989965323467889899876789995678979876765
|
||||
7654567899798799976546469892987856789987889896987654986542349878976545578996791987899989789998765434
|
||||
8765678978669899865435346789896543899765678789998743297656598967897767899545932398999878999987654321
|
||||
9889889765554987654321234599787012987654885678987654198787987656899898989959893999989656798999766432
|
||||
6998999984323498765634345987652123498753234569988621019899876547999979569899789899877545987899997949
|
||||
5656789993212549876765678999543934995432123458976542323999987656898763456798656789765434876789989898
|
||||
3245678995301234987876789987659899876541012367897643434589998867897652367987545679654321345899876767
|
||||
5346889876418345698997895698998767987632123456789656565678939978998743489876434599865632346989765656
|
||||
6757895987567896789298994349898657898743246577898969996799320989987654578987645689976543559876534345
|
||||
7878934599678987892129789656798745569854768688957898789978999999898965789998786792987654667987621234
|
||||
8989321698789998943345678969959433498769879799346789698767688998789896999979897891099788789599732345
|
||||
9399934789899999874456989298743212349899989890123699545654567987678797899765998999988999995498543456
|
||||
1239895698999889965569890129654101257999897921234568932343879876587689999854349998877989654397654767
|
||||
3398789987898779897698789298765233398998765434345678921012998765434567899873234987765678965298765878
|
||||
5497679876789657789987678999874345479999876845456989532323459764323456789984749896584567994349896989
|
||||
9986545985698745678998545899985456567899987656567896543456798765436578999875698785323456789956997891
|
||||
8765439876789434567899676789876567898989998787898987654578899877748789854976987654212345679897989930
|
||||
9854323987896567678998787898998678959678999898999498767999901987659899765987998765401256798769878921
|
||||
9965214598987679789799898997679789344569989969894329898999892398767978979998999878919347987656767932
|
||||
9876329699998799897689999999569896103498767456789212959998789999898956798939389989898959876545658993
|
||||
3987898989899899954599987878979975212997656345892101345894569889999345987821278998767898765434545689
|
||||
2198987676799989323989765468998764329876543234789292456793298767893239876710367987656797654323434578
|
||||
3999997545989878939878954359899765478987832145678989667894987656789123985321459977645498967410123467
|
||||
9889998659878967899867895456789876569998953256899679998965976545993239876432598766532359978823234589
|
||||
9775879769767456798756789767898987878999866367996567899876987636789998987543679854321267898764345699
|
||||
8654569898659345986545678978987898989398765456789456999987898747898767987654678965432878929875456789
|
||||
8743453986545239875434567899876789999219876567894347898998999898989654599865679876543989434986578999
|
||||
9432012987432129854323456789765898998723987678989256987899298969678913479876789987767998745797989378
|
||||
6543123497654098765212345678954767896545698989879129876789197654599904567989894398979997656998990165
|
||||
8656934598843259876323467989653457997959789698768998695778986543489895989199965219497998797899989234
|
||||
9879899789654348985434578999942346789898994599545986434569999642376789891019876101356989989987678946
|
||||
9998778998765467897665689678799457898767895987439876545678998921235679792198989212349879978998567897
|
||||
9897669899989878998786789545678968998656789876545989656789987892346895679987654323498768967899456789
|
||||
8789456789999989659887895434567899876545457987676798767899876789457893568999876654599545656789577897
|
||||
7678345678999896543998976125678923965432346798787899879954965878967932477899987765987434346899789956
|
||||
8543234567898765432159876546789019876553456899898965989965954767898921276789998879876521235678993234
|
||||
8732145678969876643345987657893247987864579965999754296899843459899632345899879989987432346789210123
|
||||
9543234689654987754456799768985356798977678954987653124989752576789543656946965399876545457894321234
|
||||
7687657995323499767569894978976467899398789432098764349876641345689654568939873212999856567965432345
|
||||
8798767897435679898989943989989878910239896545139975668965432456799767899123982101298767878978644567
|
|
@ -0,0 +1,100 @@
|
|||
5656921987125678979998760234965456789998768789239876323457896545467894567987232345679209876567998656
|
||||
4349899876234599568987654345894345999987656678945965212348965434356943459876101956798912987459876541
|
||||
5498789854345689467898765456789297898999736567899874323489654321248932398765219897987893498598765432
|
||||
6989598765459789356989878767896345987432125456789985434578965432357953499954398789376789569679876543
|
||||
9875459876569893249876989898965476976521012345699876645689876543456894689875679643234598998789987656
|
||||
8764345987678989139765393959796567965432543457789987887792987654569965678976798432123467899899998867
|
||||
9773239898789678999878212345697679876543656598999999998901298767678976789598987621012345943978929998
|
||||
9652198789894567989992101556789799989854567679659898999992399899789987893459996532136756964569919999
|
||||
8543999674923469879989232345899899998765678789798787899989989929896798932345987643245697897679897899
|
||||
7659896543212359768978943456789997889876789896987656789878679210945679651267898789976789989998786789
|
||||
8798789954563498957767894567899986678989898965432345678964567921234989743456989891988995679876545856
|
||||
9987679899874987843456965678979875567999987654321239789443459432345899954589876910999434678995432545
|
||||
9877578789989876532567899789765983458999898965410198999321598996476789875678965439876323789789541234
|
||||
8765467678999997673458978997654901269998769876322397898945976989987899986789876598765212345678930347
|
||||
4323234589989998789569769898769892978987654987439986787899865678998998997899989999874347456789821236
|
||||
4310123689878999897678958789898799899987743498598765696789987799569987898999998789985456789897642345
|
||||
6421234578956899998989345678987688789996532987699764545698998912499876949998989698976587899998759487
|
||||
7834545789345778999997236789876567678987653698789843234567899943989989234997678587989798968789898998
|
||||
8945656793234567899876345698765434589998974569898762126789999899879894349876583456899899655698997899
|
||||
9658789932123458999765458999654323678909895689987651015897998789965789498765432345699921234987846789
|
||||
8769894321012367899876567987721012789219789797896543123456789679874397679876321256789430349876734599
|
||||
9889965439143456789987679876432125699998679896987654234899896598765498989997442345678921467965325678
|
||||
3999877998956567897898999876543234789896598965498765856789965459876789899998656576789939569876566789
|
||||
4599989867898789956789789989854345699765497894349976767897654367987895698999767678997898978987977892
|
||||
5689998756999892347898698998768456789754346789234989878998796456798964787899898989445677899298998901
|
||||
6899899845890901239986567769877687897673235692123999989449987567899763456789999894323456910149989992
|
||||
7998789956791912398765436456998998987542124989239898793234598978999854667899898765212347891239878989
|
||||
9898598767989899459954324365679549099993235978998769654395989899898765678998769986323456789398765678
|
||||
8767459879876788969843210234568932198789549867989659875989876789769877889987653295434568996987654567
|
||||
9856345998765657899764321246789845987699998759878943989878965678954989998998854598546878945699543458
|
||||
9943235987654345678976532347899659876587899548767892398767954569893292677999765987657899234598754589
|
||||
8632047998775457899876545498998998765456798732455921019655343456789101456899876798768910123459897678
|
||||
7542123989896668999987756789787899654349986621434893198743212347893252367967987899979439294998998999
|
||||
9753239876998789998999887897656798743298765410123789987654323458954543478945598999989598989897659599
|
||||
7654449995979893987899998976545679854349874321245678999995934567895676569123499789999987678789343489
|
||||
8765998784569932396998769987437789967656985432356789987889895678976987878994987678999876545679212978
|
||||
9899887643458921985489954599545699878787898765467899996579789789987898989789986568998765432389343459
|
||||
1998798532467939874367893987657899989898979887598969875468678993098999997656542456989876546456954568
|
||||
0129654321256899765459932498768989692929567998989657965376578992129899876542101349876987687767897678
|
||||
9298999432345678976578943569879976541012468999876549874212456789234799997653212498765498898878998889
|
||||
8987678956756799698689654698998765432123567893986432987301234894345689998765323989874349999989329998
|
||||
7676567898767894598798765987969876865238678992195410196513346965576796989865439876543236799893213567
|
||||
8543458999879932989899878976845987974347889989954329987325457896678895678976545987754545698764301348
|
||||
3212345789989549878942989895434598765456998876899498765436567987989934568987656899865656789965492349
|
||||
2101676789397698767891098789423459876667897794768999887687678998999912347899767999876767899877989499
|
||||
3212567893219985456932987679554567987798976643456899998798989999129893456789879987987878999989878989
|
||||
4323456789998764387899876568965679698899765532345698779899294989098789567894989976598989789998757679
|
||||
5434567899899765678989875457976789549989654341234987665989123978987678998903496987439395679986545567
|
||||
6545678988789876789976982349897895434678943210129876534578939865654567899212345698921234598765432498
|
||||
8766989876578987898995431298789954323567954321239884325989097654523458989423566999890123459878521389
|
||||
9877899765467898967984310345698965212456895592398765456789198763012369878954879898791234567987632478
|
||||
9989978984358999549865321234567892101569986989989879877998999832125489767895998787679987678996543567
|
||||
9898867893267899432979832345698943452978998979878989989567899944345678956789987654567898989987654578
|
||||
8767456792125678921098765456789654567889999767467899895456798767856789345991986543456789993398765689
|
||||
7654346789334569432399886667896987698996987654348998754345679878969891239890995432545898942109876797
|
||||
8743234579456696543989987778975698999785698765459879543234789989878992349789894321234567893299987896
|
||||
9652125678967789654976599899654579987654569876767965432126567895989989498658789432356778954989999945
|
||||
8721014567898998769865456976543434976543456987879896943013478953997878987545696545467899899878901234
|
||||
9832123456789659978954348987632123987654568998998789892134567899876767897676789656578935698767892545
|
||||
8743234568995443989875467896541015699879878949998678789255778998765455698797898789678926989659789676
|
||||
7654345789654321295996568987432123456998989439876589678969899999654364759898919899799019976545689797
|
||||
8965676898743210134987699898943296587897998919887434567899989788963213345999323999898998965434568989
|
||||
9876789997654329245698789789894989799986897898776623456789876587892101296789454599987987654323679979
|
||||
4987995789975998956789895699789678989875766987654512345696567456789742679896567989876398766554789568
|
||||
3298954679899887967899934987654567878964345696543203456965435345897653498987979978964239987667895457
|
||||
9989763456798776798989429898543476567895256987653212569896321256789776567899898769879125698998912345
|
||||
8765432345987654789878998769452323479792123498765323456789432345899987678956789656998934569979101236
|
||||
9899321296998765699869987654321012345689234789765434767896543656789398789547897997987897698654312377
|
||||
0998910989899876986545699876542123467894345679876545678987656767891239895438456789876789798765425458
|
||||
1987899876789987899634569987643444578965467989987687989698767878932367994321387899965678969886534567
|
||||
9876798985678998998785678998754555678996879897698789094579978989973456789210998999864567954997545678
|
||||
6765987654589899219896899679865766789987998789999892123467989698764567894339899598765899543987659889
|
||||
5434599543456789995987954589976778997899987659899943234579692569875698965998789439976798762399767996
|
||||
4323498432345899884398943478987889656768999548789867455698543459876789879887678945797897653989888945
|
||||
5419987521236789765459654569998995443459998929689978996987656767987894998776569896898998769876999236
|
||||
6598996432345799896598767679989764312379876213567899989698967878998932989675456679999889899965210123
|
||||
7987987643457899989679898789876532101999954101377999877549878989999549876543234578998778989874321256
|
||||
9896599754598999978989969898989953219889893215456789765434989991987657987832124567989656578995452345
|
||||
8789459895689988767897654947898764598767789326767999874323497890198798998775012579876545456789678456
|
||||
5678968987899877658989932236789879987656698987978999985212646789259999659654323456987434368999989567
|
||||
6989879498998765434567890125678998798544567898989989432101239894349898745965434569874321239989998698
|
||||
7897989369987654323456921234578987654123456789999978999219398965498789659876756998765442398978999799
|
||||
8996593254598987312347892347689897543244789899878867878998997896987678967987987899876599976767899892
|
||||
9989432123459876201234989498996798798765679998765756767987876789876567898999898913987988765356789921
|
||||
9878941012367965312345678999345699899879789459854543459876565698985458799898789109899875434267896540
|
||||
9767892125679878323456789987656789999989897698763212598765434597654345689798678998756964320134589432
|
||||
8856999234569999474567898998967896798995979899854443679872123789986456895676569899549876534565678993
|
||||
7545898945678998765678987899898965987654168998766558789983234895497587894123456789434987655676799789
|
||||
6534767898799439976989876799789654496543054579878669893494545679398998973236789896545999768789897678
|
||||
5423456999896521989898765687678943219832123567989778912965876893219349765445678987659878978897934569
|
||||
7312346789995432398789954554567894398753234578999889999876987932101239876576789998798967989976545778
|
||||
6101237799989545986566893213458965989964545679432999889987898993299545987689899999987652397999758889
|
||||
4232345678978959875455799102348979877899656789599989678998929989988956798797999899876541656789767896
|
||||
5643458989769898765344568923456798765678997899987878567899019876567897899896789789987630345689878945
|
||||
6756567895456789654123579434568998654567989999996759456789198765458998998965675679898321234678999234
|
||||
7887678943379899763234678945689876543234878998875642345689987654349899987654324598765432345899899165
|
||||
8998789321234789854345789656999987632123569987984551234793498875456789998783213459876543656789678976
|
||||
9549895452375699965656798767899874321012478976543210159892109986767899989654302345998754567897567897
|
||||
0234976565467789798767899988998765434566567897865421767899212987898909876543213566789766678998486789
|
||||
1655989876988894569989910199019876545679788998976562345678924598999212987654424587899887889239345678
|
|
@ -0,0 +1,5 @@
|
|||
2199943210
|
||||
3987894921
|
||||
9856789892
|
||||
8767896789
|
||||
9899965678
|
|
@ -186,3 +186,26 @@ func SortString(s string) string {
|
|||
sort.Sort(sortRunes(r))
|
||||
return string(r)
|
||||
}
|
||||
|
||||
type Pair struct {
|
||||
Key string
|
||||
Value int
|
||||
}
|
||||
|
||||
type PairList []Pair
|
||||
|
||||
|
||||
func RankByValueAmount(wordFrequencies map[string]int) PairList{
|
||||
pl := make(PairList, len(wordFrequencies))
|
||||
i := 0
|
||||
for k, v := range wordFrequencies {
|
||||
pl[i] = Pair{k, v}
|
||||
i++
|
||||
}
|
||||
sort.Sort(sort.Reverse(pl))
|
||||
return pl
|
||||
}
|
||||
|
||||
func (p PairList) Len() int { return len(p) }
|
||||
func (p PairList) Less(i, j int) bool { return p[i].Value < p[j].Value }
|
||||
func (p PairList) Swap(i, j int){ p[i], p[j] = p[j], p[i] }
|
||||
|
|
Loading…
Reference in New Issue
Block a user