solutions for day 8
This commit is contained in:
		
							
								
								
									
										155
									
								
								08/code.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										155
									
								
								08/code.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,155 @@
 | 
				
			|||||||
 | 
					const fs = require('fs');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//const inputArray = fs.readFileSync('sample.txt').toString().split("\n");
 | 
				
			||||||
 | 
					const inputArray = fs.readFileSync('input.txt').toString().split("\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const treeMap = [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					for (i in inputArray) {
 | 
				
			||||||
 | 
					  const trees = inputArray[i].split("");
 | 
				
			||||||
 | 
					  treeMap.push(trees);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const width = treeMap[0].length;
 | 
				
			||||||
 | 
					const height = treeMap.length;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Part One
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					let talls = width * height;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					for (h = 1; h < height - 1; h++) {
 | 
				
			||||||
 | 
					  for (w = 1; w < width - 1; w++) {
 | 
				
			||||||
 | 
					    let short = true;
 | 
				
			||||||
 | 
					    const myHeight = treeMap[h][w];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // look up
 | 
				
			||||||
 | 
					    let heights = [];
 | 
				
			||||||
 | 
					    for (y = h - 1; y >= 0; y--) {
 | 
				
			||||||
 | 
					      heights.push(treeMap[y][w]);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    let shorties = heights.filter((t) => t < myHeight);
 | 
				
			||||||
 | 
					    if (heights.length === shorties.length) {
 | 
				
			||||||
 | 
					      short = false;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    // look down
 | 
				
			||||||
 | 
					    heights = [];
 | 
				
			||||||
 | 
					    for (y = h + 1; y < height; y++) {
 | 
				
			||||||
 | 
					      heights.push(treeMap[y][w]);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    shorties = heights.filter((t) => t < myHeight);
 | 
				
			||||||
 | 
					    if (heights.length === shorties.length) {
 | 
				
			||||||
 | 
					      short = false;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    // look left
 | 
				
			||||||
 | 
					    heights = [];
 | 
				
			||||||
 | 
					    for (x = w - 1; x >= 0; x--) {
 | 
				
			||||||
 | 
					      heights.push(treeMap[h][x]);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    shorties = heights.filter((t) => t < myHeight);
 | 
				
			||||||
 | 
					    if (heights.length === shorties.length) {
 | 
				
			||||||
 | 
					      short = false;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    // look right
 | 
				
			||||||
 | 
					    heights = [];
 | 
				
			||||||
 | 
					    for (x = w + 1; x < width; x++) {
 | 
				
			||||||
 | 
					      heights.push(treeMap[h][x]);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    shorties = heights.filter((t) => t < myHeight);
 | 
				
			||||||
 | 
					    if (heights.length === shorties.length) {
 | 
				
			||||||
 | 
					      short = false;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (short) {
 | 
				
			||||||
 | 
					      talls--;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					console.log(talls);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Part Two
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const scores = [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					for (h = 1; h < height - 1; h++) {
 | 
				
			||||||
 | 
					  for (w = 1; w < width - 1; w++) {
 | 
				
			||||||
 | 
					    const myHeight =  treeMap[h][w];
 | 
				
			||||||
 | 
					    let myScore = 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // look up
 | 
				
			||||||
 | 
					    let heights = [];
 | 
				
			||||||
 | 
					    for (y = h - 1; y >= 0; y--) {
 | 
				
			||||||
 | 
					      heights.push(treeMap[y][w]);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    let visible = 0;
 | 
				
			||||||
 | 
					    for (i in heights) {
 | 
				
			||||||
 | 
					      if (heights[i] < myHeight) {
 | 
				
			||||||
 | 
					        visible++
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        visible++
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    myScore = myScore * visible;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // look down
 | 
				
			||||||
 | 
					    heights = [];
 | 
				
			||||||
 | 
					    for (y = h + 1; y < height; y++) {
 | 
				
			||||||
 | 
					      heights.push(treeMap[y][w]);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    visible = 0;
 | 
				
			||||||
 | 
					    for (i in heights) {
 | 
				
			||||||
 | 
					      if (heights[i] < myHeight) {
 | 
				
			||||||
 | 
					        visible++
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        visible++
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    myScore = myScore * visible;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // look left
 | 
				
			||||||
 | 
					    heights = [];
 | 
				
			||||||
 | 
					    for (x = w - 1; x >= 0; x--) {
 | 
				
			||||||
 | 
					      heights.push(treeMap[h][x]);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    visible = 0;
 | 
				
			||||||
 | 
					    for (i in heights) {
 | 
				
			||||||
 | 
					      if (heights[i] < myHeight) {
 | 
				
			||||||
 | 
					        visible++
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        visible++
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    myScore = myScore * visible;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // look right
 | 
				
			||||||
 | 
					    heights = [];
 | 
				
			||||||
 | 
					    for (x = w + 1; x < width; x++) {
 | 
				
			||||||
 | 
					      heights.push(treeMap[h][x]);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    visible = 0;
 | 
				
			||||||
 | 
					    for (i in heights) {
 | 
				
			||||||
 | 
					      if (heights[i] < myHeight) {
 | 
				
			||||||
 | 
					        visible++
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        visible++
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    myScore = myScore * visible;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    scores.push(myScore);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					scores.sort(function(a, b) {
 | 
				
			||||||
 | 
					  return b - a;
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					console.log(scores[0]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// functions
 | 
				
			||||||
							
								
								
									
										99
									
								
								08/input.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										99
									
								
								08/input.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,99 @@
 | 
				
			|||||||
 | 
					122202212311200210333041243104410211022242435153422552354101423030331120103122123201123013212020121
 | 
				
			||||||
 | 
					212122122000221311323440442444413314215241554351515445144551151103011402331412314230102131310221002
 | 
				
			||||||
 | 
					200010233110132032201044033410403402323542453441213225433145441453242244421030303220011011132300002
 | 
				
			||||||
 | 
					120101110203333110240120011412225434434121313542334144355225111454523132420142243111113223031311111
 | 
				
			||||||
 | 
					120110223201233140434331404320315125532452213233242311511524412523343520430421042000030001030200222
 | 
				
			||||||
 | 
					000102322010022341104032321113312242521134214543411111515333425154553221421211441143024103333323012
 | 
				
			||||||
 | 
					012132022020312444102430014331221411222235334515444152433144133435532224431441304412411212000133031
 | 
				
			||||||
 | 
					120233223310314441323131225313444245412451315432455236445534542223421552522450312102202033233322003
 | 
				
			||||||
 | 
					110303300214411413041135513123311333342254632434525246464244653512154122151153240314331404133102032
 | 
				
			||||||
 | 
					320333021041401210342112413252521354154654526224624264656643323243514213144525443140321100432322102
 | 
				
			||||||
 | 
					213333013400314344445512541224344542425335542246245535342346525455632342143432241440034221113121103
 | 
				
			||||||
 | 
					103101323212441144514541213255356562253454324423562254643435566355652443131311521310340011410023010
 | 
				
			||||||
 | 
					232231310023333125425145335413643646335436344645256622626244424566265461143132555442033102212102133
 | 
				
			||||||
 | 
					230012330403333244113122423525434543453346345442546444536356456656653336522243141515320031310242132
 | 
				
			||||||
 | 
					001331331400242344452535542244353454256436245444356344633623323223624634633531345443212332124100001
 | 
				
			||||||
 | 
					320213230430015352541121166364225342324465546743674376745764665342525653462642431254322120131320301
 | 
				
			||||||
 | 
					030030441321345531435352552565634242236456747547675667333646754662532244322223425143335202013410331
 | 
				
			||||||
 | 
					130323430403445155252353542665556632743644346337573354575553473564426335656353332452444512100342303
 | 
				
			||||||
 | 
					000124441244513213434222225365536245467565344634335675756354647753423526454264443544455252333100012
 | 
				
			||||||
 | 
					303404133431225152343544434244326457656763736537576455577647443546747235534536353521453342020431303
 | 
				
			||||||
 | 
					311104212215444221552335243356444654364337535373347655455775663756457363653523325323222244130341434
 | 
				
			||||||
 | 
					033113243413224543336252525236557657445633537454545454747556455455777455542442433541422521522303132
 | 
				
			||||||
 | 
					032301305335212345536223463445733334466466456857685556646643443634434366755622423221321152153211112
 | 
				
			||||||
 | 
					432413234435244244452452522766657337677487457667574884664485837634653735355652265344225135521142200
 | 
				
			||||||
 | 
					214204451352332225552233236447335656736454774865747468575666547736456546737626423225514542353132003
 | 
				
			||||||
 | 
					033032334245244345353263544373576764747865456884456784664774575446775657546624634322432214245131331
 | 
				
			||||||
 | 
					121012311135135242665226463546373455674886866675578444744865445486444436446765632254623534551121111
 | 
				
			||||||
 | 
					341323122332344433445227455474347888748648456446654846846776658764776443766657642346253535143552144
 | 
				
			||||||
 | 
					324244533333334656636247465677737577755486464656557545574668654666685345546335423322234653144345021
 | 
				
			||||||
 | 
					043204235524544223242334547537787887788644766889958956876454754674478577343347433636323431422535420
 | 
				
			||||||
 | 
					132053544243564633627466765775657547755548985595865776876887476476586773674345367554624423333443214
 | 
				
			||||||
 | 
					434145415151462624423574737777874646757869897568985857768555766747845787346556464366425553353151300
 | 
				
			||||||
 | 
					033552122413443664556665676655648855845975596556995967956566699866477545537554553645645553335523524
 | 
				
			||||||
 | 
					413544333326325544236357664865456474858595889756579986899779997968486748464773366656464426231155442
 | 
				
			||||||
 | 
					004332522265552444763374547484644488886789598575569799777767888975656856667647455453254435233141511
 | 
				
			||||||
 | 
					035354414545334263444765576467446658985699666669667988675585776867776657644544534734656653433441431
 | 
				
			||||||
 | 
					442122443263522535743777354555686688857759689956668767555979587695855866658755676536345652311311234
 | 
				
			||||||
 | 
					231314151362463367455573668565545776596986867687779777769576568988867748688753444556446645553534525
 | 
				
			||||||
 | 
					045415334236364244375435664665477567587759896768777886876768796985898556758446336336326243331233454
 | 
				
			||||||
 | 
					342353524362463357737675678645755585686559698888978699669989566777695575467773733463432243352222445
 | 
				
			||||||
 | 
					244133134532623377737577688575658856867977866698779767897688987856558847648874553573743553546523534
 | 
				
			||||||
 | 
					021553556544442474674534678684779989779667798977898967696766875855597978664786474743343545232141545
 | 
				
			||||||
 | 
					045425316553544766676736774475658985557769887677898986998789979958865786565747357474546425542554235
 | 
				
			||||||
 | 
					123511255462335663763334745464955879987988867676898776768888899576578976678476464757366443234425311
 | 
				
			||||||
 | 
					325533243442663433734555785478977676679868889789787989899689977975777578786878557777434464652443351
 | 
				
			||||||
 | 
					341155355546553356347757544788568965668697968997877777976676899896768799766576567645554342336334233
 | 
				
			||||||
 | 
					421521345226644475636665485646689879769887867898789798988788768788759989487445577633675662663234325
 | 
				
			||||||
 | 
					225431534432355354466644477659579957678999798898887898798887799686577775655556855457566334643614134
 | 
				
			||||||
 | 
					254522334642647433556585856548559868968877679888988778897876767896886779646466464564775265336232441
 | 
				
			||||||
 | 
					312113532345553345433444768455667856989979687989987899997988898888658869478687547476363665422513141
 | 
				
			||||||
 | 
					152555423534245354346564447446976597699676889998999887989796789797775977768544533373735436335515423
 | 
				
			||||||
 | 
					341213542653444767556388768885878975698787998799977787779899987685979877545764473357337423424514315
 | 
				
			||||||
 | 
					322113552445453574657574755545799689867898669877988877997779898765669858668546477767666242656344212
 | 
				
			||||||
 | 
					523331354363325454365675645869975977878797967999887779897978887888798999548865636336353426535355452
 | 
				
			||||||
 | 
					523523423432344663765375787446557887679766968878979887789796777987687596555667445376536663356525141
 | 
				
			||||||
 | 
					551114235346366563637355556866885895767877798798797989787886976886787698548687646557754363564525313
 | 
				
			||||||
 | 
					251223245223334447766584755447956898986768669897979798997768689676988975874646433446733346424411335
 | 
				
			||||||
 | 
					214241355255455766365674476646975968859866869967978898867967666685895878644875637565576656255154544
 | 
				
			||||||
 | 
					455352455253232666575668454867955886956998987678966896777867699977589755646464755747533332342424541
 | 
				
			||||||
 | 
					444515424223524645435648587576677675775989869686879869776776888598575688877564767577434234326235545
 | 
				
			||||||
 | 
					431313414332525265734668687845775796567769886777989778777997885578687756877567364366345652533512134
 | 
				
			||||||
 | 
					333555123263656354334546784878748996895956799868879687768998999597665865666673657464554445565315153
 | 
				
			||||||
 | 
					234251415355332356354555748857549756797677788669666779776896855789587868886666535745644366551433223
 | 
				
			||||||
 | 
					425342555454555443357573485485875677656895597978676766678885768675668576658854377547455252453332433
 | 
				
			||||||
 | 
					323212322142542547356336654885588587756876988878668876996855885757567666665664655447625566342455323
 | 
				
			||||||
 | 
					145453124156454236753637566754554586888986779659899875656867888757855674866346635564564335311444142
 | 
				
			||||||
 | 
					241532355322446443663343474577685666578989895777658988767886579994465878877675775646446644411214331
 | 
				
			||||||
 | 
					144121414316432226357635465574878648885659856586566979685759558986575567664637365426532646212254243
 | 
				
			||||||
 | 
					110513511415656625443556556674684868678895878785759566769757697468556564753375577435453525411543113
 | 
				
			||||||
 | 
					010054252444265234547356444555575676545757579699968766586568964546866767534346564224265623245525511
 | 
				
			||||||
 | 
					120212515152224263635534376453556487856658876995968869757756754456584754356376564445254454534225340
 | 
				
			||||||
 | 
					334403322452135643232646334553464468686466757956767796875866688444464456337547673543644223334124002
 | 
				
			||||||
 | 
					424345231535522355445635437664744565688755754586858454858554458475568756654735523654265235423342312
 | 
				
			||||||
 | 
					031443144424522463542544764444765767644457666757567678447484574545767543635774453342345411351542313
 | 
				
			||||||
 | 
					321403155315156656344554665775567644878587754546578686767855867554637477555573424362546151514131330
 | 
				
			||||||
 | 
					100300432342253325546433443636475644557666885556847555458755555684535636335545343542245513545400031
 | 
				
			||||||
 | 
					241103243345314352532434567355636677768477667655874858784887748363644434657254223342542521512523103
 | 
				
			||||||
 | 
					013432002255524532425326442376655436757756476784578755546658775744775577372453335234234123311334110
 | 
				
			||||||
 | 
					224312132351342113543625426364775474475555435685668887764654565767573537642634534534253412535033121
 | 
				
			||||||
 | 
					342013333254343554666262433657555734443433335545744436575736345777633344224223656361344225330312223
 | 
				
			||||||
 | 
					234442324252344452264263235643446676735646567555767567465473767566476572456525232422431125340421040
 | 
				
			||||||
 | 
					030302023035452131512323435555347556466757645454545634364636667436454662255465636151444354004144240
 | 
				
			||||||
 | 
					002004133214113555342264554265222355765774744775436734535556664773753352634233435412243552441100341
 | 
				
			||||||
 | 
					011422341232151553353542265363535632735746674664776744464473335435625662443566132542533542101243120
 | 
				
			||||||
 | 
					203021201322125111143244446544365445365774633366633765656576533325536225455355341443312221224423103
 | 
				
			||||||
 | 
					311021234200144512534253453355352253535542456754747757743743435344545565465655544322254011444341222
 | 
				
			||||||
 | 
					101203222013410332153151244543442433553342325366345652255264423465522455665453535113251410414031300
 | 
				
			||||||
 | 
					103101032340424335231434112162665465333242654445464443552552544226533654451145222233401204012240023
 | 
				
			||||||
 | 
					300000234304043032354531115322565425623354453256432525255636642566343622125151231523130220222300331
 | 
				
			||||||
 | 
					320320110121204431154322441252432253656645325532363344542646546553223512543244125154443233034012021
 | 
				
			||||||
 | 
					223211323021301334023554115315242234323456322565626332455546252636654333515412114103233412041213321
 | 
				
			||||||
 | 
					020331330004113222113544411442545513454442262444225546542423526322541134153211543210202143401031310
 | 
				
			||||||
 | 
					033131211214044030334411533413134451124346654653645545223652313534244341353254324212241333001202010
 | 
				
			||||||
 | 
					011223212020313313300223232314353315524525531133464633645121312251455142242212211423301012011010012
 | 
				
			||||||
 | 
					202131322330230012310003211315154334411525215354321214224515115324133412515133121340021221223011311
 | 
				
			||||||
 | 
					102233123000103132110110422414142252322414331525452331522255224253512324323332322332002033332200121
 | 
				
			||||||
 | 
					010112331222023221443321410332323442443211235352422543351522342441341212433312114143012011012301012
 | 
				
			||||||
 | 
					102001321100332034112010340431405521533133132514423245132455311232555303030103043321011110131121202
 | 
				
			||||||
 | 
					102100020233112322130014030443133042115515544424443514351353232214340221142434234013000032002122000
 | 
				
			||||||
							
								
								
									
										5
									
								
								08/sample.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								08/sample.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,5 @@
 | 
				
			|||||||
 | 
					30373
 | 
				
			||||||
 | 
					25512
 | 
				
			||||||
 | 
					65332
 | 
				
			||||||
 | 
					33549
 | 
				
			||||||
 | 
					35390
 | 
				
			||||||
		Reference in New Issue
	
	Block a user