This commit is contained in:
Karl Spickermann 2022-12-08 19:38:04 +01:00
parent 96c4f48dfe
commit bae13d779e
3 changed files with 187 additions and 0 deletions

83
day8/day8.go Normal file
View File

@ -0,0 +1,83 @@
package main
import (
"AOC2022/helper"
"fmt"
)
type Tree struct {
height int
scenicScone int
}
func main() {
//args := os.Args[1:]
lines := helper.ReadTextFile("day8/input")
forest := make([][]Tree, len(lines))
for i, line := range lines {
forest[i] = make([]Tree, len(line))
for j, tree := range line {
forest[i][j] = Tree{int(tree - 48), 1}
}
}
part1and2(forest)
}
func part1and2(forest [][]Tree) {
sum := 0
highestScenicScore := 0
width := len(forest[0])
for i := 1; i < len(forest)-1; i++ {
for j := 1; j < width-1; j++ {
if checkVisibleAllDir([2]int{i, j}, &forest) {
sum++
}
scenicScore := getScenicScore([2]int{i, j}, &forest)
if scenicScore > highestScenicScore {
highestScenicScore = scenicScore
}
}
}
fmt.Println(sum + len(forest)*2 + len(forest[0])*2 - 4)
fmt.Println(highestScenicScore)
}
func checkVisibleAllDir(startlocation [2]int, forest *[][]Tree) bool {
directions := [][2]int{{0, -1}, {0, +1}, {-1, 0}, {+1, 0}}
visible := false
for _, direction := range directions {
if checkVisibleDir(startlocation, direction, forest) {
visible = true
}
}
return visible
}
func checkVisibleDir(startlocation, direction [2]int, forest *[][]Tree) bool {
currentLocation := startlocation
scenicscore := 0
for currentLocation[0] > 0 && currentLocation[1] > 0 &&
currentLocation[0] < len(*forest)-1 && currentLocation[1] < len((*forest)[0])-1 {
currentLocation[0] += direction[0]
currentLocation[1] += direction[1]
scenicscore++
if getHeight(currentLocation, forest) >= getHeight(startlocation, forest) {
addScenicScore(startlocation, forest, scenicscore)
return false
}
}
addScenicScore(startlocation, forest, scenicscore)
return true
}
func getHeight(location [2]int, forest *[][]Tree) int {
return (*forest)[location[0]][location[1]].height
}
func getScenicScore(location [2]int, forest *[][]Tree) int {
return (*forest)[location[0]][location[1]].scenicScone
}
func addScenicScore(location [2]int, forest *[][]Tree, scenicScore int) {
(*forest)[location[0]][location[1]].scenicScone *= scenicScore
}

99
day8/input Normal file
View File

@ -0,0 +1,99 @@
332003300013411421030123310000325304355245416315402454123304251100551413414301042114403240340202013
130120131034131214123330450045513234151453520504226501101432120555550112535000130014212133321332113
221120234102441024240343113122535350650301140511165442365451334130334102412103112220300240240410033
201003103341210433533532125351404201522621325222421563264456331504145322423000453224222123321220333
031230441334332445422025052033156043024163224106103336113645142121364161131252032003041114411412021
220020011034120131030421351112001560056302340102663363436621063030163253101311044513331310020121210
303040332431011132533443553321623462145336356503166211061351546411261306522112352524223111304442113
222232314244103121400531144211642025551410166524434251333155410020524252654152222025520123303142134
042224141010553350340352020211461062016047171732434651455473401346405306461643421210440205441310240
131414414351042410204054060152045335375171544547253174334561651556000661445663062351033351130040204
421330222101314443436633206550451571441147331752243741723515751137750300263144202141521145051242430
304413012541454453626212024316241455547757536354351542142777234173745662454551154032335331331021134
210210020432025225546350564503461126533444677377311761441622516326645176426625622336441140312530032
321331544225031511163062251235515725645116556552716447134611121422154147221206525245321405031141022
132304244223434034644164163431246762324642345277558431347372614572744147525404511143400153134025230
433014255554304626035066241262275264471542552344752837378423431364621367116254664210141202530542210
341013013022206652606231157612322543268825577322777368835244322537337447537335222426156022255415144
441525355153361333331104577746612725264233863646327752763365353845325222561423350253602520443533440
331325510313351211266777763761227567864572264785467455456335374866514554722241446502256653043414352
223021111542064104141467134673114548632465363887386464736623733276867135422463573365121002544203051
234440212451036151623744516551447238632584877658432527675226444645478351316311314130326060612445003
033044450411501311543214676112356456332628328268867775386867238847585733154276434130400024043455141
205131331521440160315623445652882338268255429855577746736386265847343838432324621120436063113151534
140402552431642627426711567572286656272365849859756599936647555338227372237714366515211422436232144
200320133113652127237741127458347233869549439984447347799396896884254487755137323273723662433004102
454424155234224212665443255248273448987696973685793654947967344658738363223344343225642564053032543
204042231254546227227173383425466368946763933885435669749596368564787364536585763761277210314664203
000413654454332177326723345672532779349633764356436834584654668495657884625378221164774505026425413
104211300205311715275673636382227444595567898434345599648544779845678768388347314614742310641262315
314445336034346211516832335578373876466544933499649884577395587843454938885463235516577345221004303
312454541364371767524685754878934666577363898489498779648546864385644596874462483134516113200642424
045303023541545211177774473328459887983569477675788545664497958663479946723273556551566745654414551
315104421032435152386476237653439634759548869484955567445998495777843384344352472827152536033066153
005541041351421165773462627984936336589775654486658558888479674838753737875276667512234774606631641
342465056121667145785833876599887748648776987865445984558545644948958639677226254687646734301231545
550456224133553143372382765354534539768845445646854596849896894999799849377256565255732636263050651
252500660515557345284865448359656747876866486595579856584858769494858684374873544562312172202212334
052616136615134615787657448653579745658966465855689959587489489794773794834525445883123525523333601
252230540571447783264468947593555864557685577987986869757576675658486967664678685272326377531121454
413163451427745626242667677845887769799976669995586696577985969684648578656883457628861571436025642
533405114525476142726689645385596554758496765765598795556989875896575787974383873637675463761256614
454326344554154642756573364987695594479865587966585689568895957855974975333548887427326446522203044
216114153574223527762678337544477597569767966689859979656975697545485575737839438443886132712045365
125156666622272632365556785997895855986558977786966869968767985974484866833363785734652313512061636
322321415352243487443396957878886784758759777788777677655788869779499684595638364356883237212251446
122013647613644872445494688665557499576659588698877977985696986649564754335394787334241464246322355
605654146537634766832593985758685444759955657999799769877988857994746676947396585553386542761263544
213653425317331334336886398546858769868586766969966669676559789578969885393464626654824257614332425
660654431433636254452474779474874757675959989699776669987955967958545665666456488723857762667632624
302316342766454766363278374838448478956968676979999687797875698557444688784488876226726637647532365
636242311725575834473585863576767447899876977687688888867867959567798495435865837265222631673466406
062235543254168266743397385348444696655777679668976789966989675764697759836733964552835613545462646
640333544126212773487299459967465446887655668986886976666767658875567556473478954563422657411264042
540625433215572383378434996869859464995669567997788969786675997787457759898479882426636752542310414
445535166771723352824654693847865478896865796669698987668955589559967975639596952255564346215524205
221425127235511746738568359348455498598575855997977899887788556776765485458747726328451547753230153
214532604757232738234837789469967949479868968786789687989865889988588497583593337657226241341115620
004244467622352758548665446367486478788877985697798995669999777644666456469933826356824242327113564
266353354374115362785466543499745995759668555669555957566778696566987598783854846782725636654233143
531631243311643228822426956378987478954886887766685959675789555656676595553996574356555472444216454
140666423163211644345466589664758965767788585865675857896579844968677958746766768557511213772051231
234421130317541287775845944673487775548876959858669796556656565784695558999352648657476544226554635
412624633546563177676442768954956647579597689996578669686556674944546967458667638377245761312144410
352621121571142172762553299483794557966844949979766996656658586465459334647545764377443526446306142
006346322575555262872376536466933545779748779879878867649456994569974998665447554338732211431303113
554334563237315531875257467895335488595964994888856875774884857555594969967225623822432356664611145
451004264172434426643257788958894598687845878998475494658586487656436966346528627424414757464352412
221050361362741524742282864394477367968877699857448774499568854837979743562628644665437676533622521
355255324661136164174557438655596744798777687569864749968955695944564933443722484772514174556311265
430065024241631523322743433843653488937876477676994649678667784553585674765334237555126451026263325
235505456452451176728742366547446897648939846457559957659763984484633798325463455573553116236021123
115234031514534736163577244834455659564966385864459866983788898754869654436588443511424124332535540
351404033625617526367482264456275584336747746958739743757367759776843883862727732164231653501306242
503055012541653215346262842272827896834747967447549377968493597455838645446365744773157220423462223
553143615436255477475667832483727897549434843838855833978798965894468635542322233222537021036615034
002222462560346626774546373548542854933338778555785475683957754684227337526515767775450220143425433
131504260406323354665445316566825527286679849878744668534384467583656384425625441776212266506542432
215054255514263012751367165784852227765689933769757763433568352646878327626173155737352644405300343
552314225454222510753211174655762567463268427333439583897865725467264347257215367462301306160354104
255302200521051251765326645715455744274263586466448448464856568583488367547737246743335131401512412
101245322521566243614773147142427484788364664826554268425888478476726656461566577523262156415054432
442223113356204112601267136315116673824647668456465767535574488583325342131741417460621414452243501
030021515554410652516146332263743763547244733553874755783747688225247344423456213264214215420224550
413231405342104505430605655136127652642884747258725854637357624386534551152676140060146342324553214
043025432315263603012410724771723562544436732537283686754328266144444737731263642624006034433532321
104334254513411453146423047163424563275165826658888562874364647322215523235751010535365201041550134
300020553012410516650503127144151151366257317647264676757751532461215753567001666406110512441403243
102330504341544132334643313664461577763552415276253277551421216256257366432140103125631532553012441
133121400000310324062254440466672675621365462615365334463422371553612311060246343023025050324513003
414433432204250221550404116102162474676751443756673535654632655537372506016342016305523520322020433
300431421523212003523352645563411534764236144446653634512147234367106145251253516223042015230414132
243312200244203131000464166042310223354312441365122363622347664211452666653252200441444455224101342
234101331402523132440002366305604106515143667473324626357574660410555433104644155051000114411404322
323231340213153413415545102230611245662526641323132514750054002302435343103551525112155333241003331
011130141043235255230523325530536445442030625261123460622614140226632004323354541551014140002214411
102244200240002203012052520545350235023153142054205355115153522126310220541122512230331031112130111
013231143131433131124534302204532206566450455256355310122012430310255401351304225525542241003133010
132200122232433121333410132153131541542513264261463154353021340403623114231305043110312440012243111
001320030221212412130340411450015410126162032430413232552634423102222044153052245310344121344231122

5
day8/testInput Normal file
View File

@ -0,0 +1,5 @@
30373
25512
65332
33549
35390