This commit is contained in:
Karl Spickermann 2021-12-15 22:50:06 +01:00
parent 199b6502b2
commit 08c17309ad
4 changed files with 221 additions and 0 deletions

101
src/day15/day15.go Normal file
View File

@ -0,0 +1,101 @@
package main
import (
"AOC2021/src/helper"
"fmt"
"os"
)
type field struct {
before [2]int
cost int
sumCost int
}
func main() {
args := os.Args[1:]
input, err := helper.GetInput(args[0])
if err != nil {
fmt.Println(err)
}
var caveMap [500][500]field
createCaveMapPart2(input, &caveMap)
caveMap[0][0].before = [2]int{-2, -2}
caveMap[0][0].sumCost = 1
activePoints := make(map[[2]int]struct{})
activePoints[[2]int{0, 0}] = struct{}{}
getBestRouteLength(activePoints, caveMap)
}
func createCaveMap(input []string, caveMap *[500][500]field) {
for i, row := range input {
for j, char := range row {
caveMap[i][j] = field{[2]int{-1, -1}, int(char - '0'), 0}
}
}
}
func createCaveMapPart2(input []string, caveMap *[500][500]field) {
length := len(input)
for i:= 0; i< 5; i++ {
for j:= 0; j< 5; j++ {
setPointsWithAdditives(input,caveMap,i*length,j*length,i+j)
}
}
}
func setPointsWithAdditives(input []string, caveMap *[500][500]field,additiveColumn int, additiveRow int, n int) {
for i, row := range input {
for j, char := range row {
cost := (int(char-'0') + n)
if cost > 9 {
cost -= 9
}
caveMap[i+additiveColumn][j+additiveRow] = field{[2]int{-1, -1}, cost, 0}
}
}
}
func getBestRouteLength(activePoints map[[2]int]struct{}, caveMap [500][500]field) {
for len(activePoints) > 0 {
step(&caveMap, &activePoints)
}
caveMapLength := len(caveMap)
fmt.Println(caveMap[caveMapLength-1][caveMapLength-1].sumCost - 1)
}
func step(caveMap *[500][500]field, activePoints *map[[2]int]struct{}) {
point := get_some_key(*activePoints)
delete(*activePoints,point)
x := point[0]
y := point[1]
currentFieldSumCost := caveMap[y][x].sumCost
currentFieldBefore := caveMap[y][x].before
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(caveMap) && py >= 0 && px < len(caveMap[0]) && px >= 0 && (currentFieldBefore[0] != px || currentFieldBefore[1] != py) {
sumCost := caveMap[py][px].cost + currentFieldSumCost
fieldBefore := caveMap[py][px].before
if fieldBefore[0] == -1 || caveMap[py][px].sumCost > sumCost {
caveMap[py][px].sumCost = sumCost
caveMap[py][px].before = [2]int{x, y}
if py != len(caveMap)-1 || px != len(caveMap)-1 {
(*activePoints)[[2]int{px, py}] = struct{}{}
}else{
fmt.Println(sumCost)
}
}
}
}
}
func get_some_key(m map[[2]int]struct{}) [2]int {
for k := range m {
return k
}
return [2]int{-1,-1}
}

100
src/day15/day15Input.txt Normal file
View File

@ -0,0 +1,100 @@
9917778469615819483183699455918898424964113292979594899217697116697568121983521763244913199499788121
5195199619845968739632397612851418935346363915761483739863995137448128429751912128512841945792997997
7928138938149629997297859197239931813682169814398516119173393239111821839998199966767914869634768989
9589182722178952812292661145297996299568431411152797286181255782882621886783667512241963277181323819
8825152997721171489796162292188889326929118921682996985547629825318338716641424853118969419961763145
1739673726349385631111777132117593951931884918662819827999825568919391978271811298819188357814885542
1446898891188132653391543556217997571497845171689229796496485137157211788683969788949839727491816989
1215193872139129898194137165916123723363833119936888516931917116282381139926218667886799655297758835
8261993679578124515579711999782293283592514188331894552332922894185276972214992271169194593681591989
3466981615718287453912999458144121915969287551478393932992237814447469541887924477869835257592149937
6539481249881192119898772982889379192141991181528484214854559288476869782249199126939813511946859192
1453983229559672269689746969946491888978837441213248579181229229829961775169323398219238491331139211
4151583258733728997129921227897887889186247299699193351796329365993372245935215411529933917595923725
3971722944989325623936826494189939171817149779631286699291928395189845183411614691278184383929983749
9192837945812993439977596918699231147662918984897172897991977454688296856992964551293312942794289287
1888188792613375192938738739733681383811223328161161989353389698249891414525877893878894199426674788
1693434898947131341911227734311695916918289783277235164829619327126196826269244435564427985182218949
3828445516883749382977116173376718161386114817999789931592825769921879921345492625968595152289338998
1619439215699327274543271496582191428484175885951512766717588519815592823416985949723199233779989879
9978571554739394926896241391493528183586679822817179644626991798944412884539149116279815685919899978
9913662984881841991995629199889191376799772314198217999388139357627849266611118299818812839732156898
2991347842135521785327129714891451791517381999141489211412661299417894857492911691114719621171966481
9819867695791616987439297211874529982112593976892817611721439512197818249819289222921468752854517829
8692499191421915431819624613676759774269644413793995799682883994998396685497112784255684391223999979
7169537448482866696292264461193943123879219412289599979467944799739199289461175179349151827217586151
1899499476996629712527591319812498837989553762939279454917129929997781367234831643431927148829619241
9364618735529118585867287151463942359286756429699829591847169871727128979272996492886928899796186923
1659729991955992223968169328926738596926891285222593736531314233899614959349337298599217222277536981
1549982561753921724182599941974719483591299922121359228728871958581961314422999388589165724618924571
7947114923331182184818191956872491341672171645522936919924697189291919982956421636831431861293929316
4163567417399658312732759567991798548729183316597398241337188992184921878189891459639292189939115398
5271794989184296516727899919959737963136829641598218182873521292739427159827619818828788894819998599
5511441327998233211948779919191155598995599471329171141122891479612951857158465111339979919993852563
8819589296855151911862711161895244512242669921679284566999989795598479255693126871798677398499941389
9958625717796595199219229846795499993214961929199959749966724899119218892549198175211148961974218424
1214151918298826463687241925919195135179622962877895395848817158686133129512849896367858655624993341
1582546111952973358298298171849374985291863951294713417775989161782174219785159882979556513291769891
3914772971681791184171686127287489498949638996919499117989478912699215798244361762138817912934286911
2127851879487116988189689969279346318583677199853988159129499617313747815517825813191859979953592365
2214881899926499632399987818819919187895424639979129888284745882739194311146429541429482229299237379
7151569254244798769997197885313199649566759612973288989327723273669189661984755187964693783196518191
1811918369618983176519998979271171915818128888423966947283931624954167372917847282152171187965857748
6741629818924869218969887995119165769887922266639581649184569422299917838519242729869351941624899979
1925181919921128963696598944818916853693691442757749318991572411761595988488935599539899572585543988
7942712771835819645999799169413515515372931571879592111994571288993911437978897292948516819314111419
5731141152174925114389981738899297186199698359969811113563176188499788914283989561948791291919352717
7491948921773497957257812899553228346517639157786745897283743917494349517361989859819317955484982723
9391744189356757989119837467131929958889169196278922811227132676151229316781985194869858879179318992
8971131839247492994187543599349185423813851932798599223948989268211619312188994725191296984278751536
1695576557818829916828155851192669291463317613821991714959584848288115898997894881893879698726934997
7139384899291251489788969199769148524581739642516597751271798918919911126999675958193969671491449944
2167588932582712392711397899179212297678193197298641199938299252144721174911983997982555214897159914
8681735662798996419769111993763155252281392638797979995126792814984748586968496985199291793813342695
7936973991189483123717939711958143341613652121531817411779929637365337798294179392399813962591579935
9882781351453139489117389756287975996133625733153292346764964614227693984622481953483771176994628486
9226112991849951218937397129423522139999197692513117631712968153492196132418454175682999911571859974
2677614849489797911887571295786814932799979376992885368965692219298577381285619676981286768391295112
9172857595231595693618273697881992813228398584129811992621129935529797451818429117779893687126268993
6559918871328111912592939325999519997331997135884468491561291391164817232914987995963918744788885162
3192573916229831591829917194199119979165399448215149316734916989421755919415626839713672711413135714
8296571649939278941164683665999971717738899891869168736499711571568949412983914468128817199475269996
9254998599299224922728336881771238633497543229824352134994788951978996312626918286886868171599595969
9818968192241112948935138393348111112919259897256831465729147319137512719118429882382157893717254832
7181984919811512881911521175516531698989765163452832189899933118989891918719799176789398118258473992
8249652581416387799256619268927929869161164358859993548988185271318513761132818841559881261694991871
1376974972741669647659952991997932891684595946918641665829496831154148762143996419712931141796942792
6941972945836214986962754116963199157899239926191838941692122841191612472241169211972171917625281456
9967596367293755737418482939597281999934897948742991525288726399197139648892297919286629238348841319
4768399852591898947479691244688895143711119743747671593482179991796167919941924871386828585518741925
7829663947229292619983312812286259862947997195111387295139883149683614197973863961734559997996518982
4591511811223992939811238171769396451682192752721915489724161811856711859216111297821169991216957483
9937366329968988677981977255918981124315534469383819881723732958212686128186598345532291994533686991
9111154783777844173336293147261569459631374195462184991289445486111971754221815993199731894168881623
9189299941992975731835689719939637954494689697578956899757982821352878599384929189538369119852886919
9729731912169781155161883411999119792812756198984279969973312915594739995737818199789472973696584984
2982121729992124825275391648931645482415988876269281188979915819814964995657843489561969819829811989
9958488638177994131921616653983134587821233611144581611381928262231951111299117699522266719755994418
2912923789156994238816799598817765145249481761912491713613791775816113954918747849498159899283968523
1395431878856456918123749999811843167373698989837283847214891916327449388721988633915329876735628969
4335116298919845997925367511291899891221646695135251435335992179177896256989529612989899761647987983
8789588499327698926511716137832891994659296175652674942871296725575981238575217351715922393698612698
9469438494189171241283189439812546957117961987991996937796969399232292824148758919299914164214519194
1151499691297385311481932171151699882958774852899525395917636631782991326731462661992466458253994833
3671582886292773981397941631752473547186535784674177242268112919942484973288916414664869468389397879
4948118991798493439117197561169314529587231993259239227724518965919246294171611693988883791547249766
1216916956992975851673919899224827248679669699549999485982261891877569116919114831216619789618285649
4392347167483499924174689869719221831329239775867592197918185931461763377619499119932793799888896259
9279436838159816262992347424699911969479893493117278233846831961875944234961989996579992163148344778
1217493192955381873836719121899734913248191793363276938357843828794799722644284768996985133493329929
9927979189142869216346944813796936529683128112318957631812531111472921178357999774988946866877226216
9996537127912191362178399778971477184123213689118217127194168562962831147269837839911815349221657894
2923559342466119813221433298766799663945421797281198492995429116335699596149789991388944729779372171
4712289522597599549619282169214311493692928915128993771512141248957798273691119189796341142228822435
9594545756889112761249991521749561129615678321233191518324761817298115149397463167921923695419823276
7599912664195886495182217912418492999492899292668948548817927836914997139914648898673428695972348917
1463416882898291581488785967692181981763993919146971729738459346771191851217518521261188134192968748
8969428458619387591534682685823851241524999158183219841576211491285115979442198191568194911962365414
6551116926979789599467992212942916372386858947381974975198151113995819485815853938998631939519142887
2962181629633949925644938398829991982897619932979712895169766295893865787439781919153997193981677859
4841884135393929498471882713859579729345686219877874228877113424294991897184188689295384478759242558

10
src/day15/day15Test.txt Normal file
View File

@ -0,0 +1,10 @@
1163751742
1381373672
2136511328
3694931569
7463417111
1319128137
1359912421
3125421639
1293138521
2311944581

View File

@ -172,6 +172,16 @@ func RemoveElementFromStringArray (stringArray []string, selector string) []stri
return returnString
}
func RemovePointFromPointArray (pointArray [][2]int, selector [2]int) [][2]int {
var returnArray [][2]int
for _, point := range pointArray {
if point != selector {
returnArray = append(returnArray, point)
}
}
return returnArray
}
func GCD(x int,y int) (int){
gcd := -1
for i := 1; i <=x && i <=y ; i++ {