Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f3c6737

Browse filesBrowse files
committed
part 1
1 parent 7c6f495 commit f3c6737
Copy full SHA for f3c6737

File tree

Expand file treeCollapse file tree

3 files changed

+173
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+173
-0
lines changed

‎day8/input.txt

Copy file name to clipboard
+99Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
313213123212200312011243203120214010202554420335045116203101005212525131015305511140012431022113113
2+
222021333112322244231542054023354511304431216312534453560105660253445311020244233130144313440030322
3+
200012220300132310233100025524032003355144536126232161330106115213441450552235514000243120210031130
4+
102111124044033113241415155401056014506203413216651520206613342353052505445000321043121021430440021
5+
221134021010031320101542213340113552161113350123516154134654046250223033000413443321231233142213010
6+
112241233114245202523201433013460320664034202602144006022621253536213222400131120345151014014131133
7+
132211130114141401325141212460540656525106651152012063404633651340223361443201520054113541144043031
8+
323140320024314005210155316202152552621415660063112177564603241212312025036551350313225504314404112
9+
014013242041230510442265205625331115540222744516266167254216354043533020346502032534231121044441220
10+
233414030101315255335126055263144512111777464611565216157441673706100506241423030403420343112342030
11+
133144314412553330012446404244433465434666762135116677765213463444460612511131340332552424201204120
12+
010320402332512110363213044530335655613562223756572424746437567672262256431540210005310544243444101
13+
324403411232230200215144254550257275144535323452276762234354361552536153612012260502425410505314212
14+
044143021304533036524115200363122146144443357746134567133417572143572217253466665420004544002052304
15+
031301205442313664253200353721523337451137554555385833341715227554331566151244334446363035232422032
16+
412021132254126340642250212415471513377143653545744364662761514364426436251422450305502445230351142
17+
423013525424334650512351242132544127615655738268233852527877887577454317715373566210510633013145020
18+
314013042244622602006117664713652263528475833433263846468763678447134716635345050225504112315124312
19+
210530032555051320514661111414734263472852682724485772623266565822423365264512541061226540103142223
20+
202523210140346233027372362557125436368725536228624865344774378537888747171665564143331101501212340
21+
312102150200503360534132445517484678583587678756766763747725475724228724317675166660536246102044404
22+
014501150514404506226424155762432386227846258625478334768375862827476433553651311460116133631440213
23+
145345025402132601222234271268834838244287765994936689397836424284644676342577451356463132062234232
24+
030520533600060465674743543832435622275594666933473957864637427284284528866426734257033413611153542
25+
111125222126126664625217555874224623337857863456948836785373353757744683247375632677514312554402024
26+
454344234313551645731724622525365832657894545768964536939387863978465673442253722125752553236132004
27+
141351656653224237252122854224855258938593946648537669944575478986734723225856761571634036114323202
28+
301354130411223555466416844555847677853855433636774857897563386873392265447728545634555234214003555
29+
324351500351156142554654722557458488698547355994463766454687489898485753473777574124613414652504004
30+
000202526603566624271288867428799685358698759656849978553895836996388433252545546266753551322305123
31+
142140450656776227714466585833798874975635767795445874946645465477849772464684527525213236023524220
32+
324262353505673315724236445748536388973668654588599896854469535494838643744387872721615221063235321
33+
552206463167666154577658258569744567339446596474586868848866645743399869362886626547344767533546153
34+
352323434663437355725427826449799477395999565764668865854945787936654594832633678767541225131001503
35+
440166362233562667478335763956389857755767497475848959778498799895595785387844464757513162265652640
36+
104122124642766642664452283555537376598457767558647554969995685645795999475473687336165523560052001
37+
255352243015641217534667849769975859955778464876555786747474774794864778373768452562637255330444232
38+
456220432747476336866782246873698697677699688678997975678548856759743777979632262638565415133210633
39+
354641641663166247236463863687744889449497969668886767686756898498698544578857834482516366746042103
40+
125201564115437683566423743488648645944969969766988857659599646944656843556585768447477145122536100
41+
565166453317161563746425435958546648857589858689866779576599554476785965354884443284615264253254316
42+
121246126671552573824643637663796768749697876859599686798755755668478994537789677336675255111501306
43+
360355134677155672632437676667797548778989878956585555755888869798448799556895227877387243567410342
44+
201045427623176635776469999883749944896899977995889968875595559556975886998744463243563112165201326
45+
352250115546124536536769454447786744475757988989886986985776968688899563536676844455461472345336201
46+
444012305276547277522595375654994558855859877987996678887769879954854599865599846623773465534004160
47+
003621673437674224248486547979885878957565757686996887766986595599447858644667462254244517674205046
48+
045265132276456334554365893935749649965989576778789869677576755756899986563778785582684657425224532
49+
256151375615717542563666894835779554876779596976976699887778577957456866545674477228666746346403631
50+
161152464371764263643334333389748956969597666797767878866755966565484647664843525828682412373755454
51+
502011544714165463843278447485479797867666888668797796768977767569575788836334565474627531473704246
52+
151460364232447443233565968759985868786557997798976867676959589967898874545836432537554441566122314
53+
325354014323767722264889584534897666569996596668678778968696799996649764974489874252576563221244356
54+
300165663442414468382638967476995984959988868887699967676597986577798947648889527728225341345144126
55+
523044146537227284236854867884546566896896589678689877799567996884747789569869374746443236356121631
56+
623520016256236672238393557665746588457977578897867867689877778857948885353767526367472655634211324
57+
513112323743777428355697564974758755988785986568979868979685685997847967586943842248473255451053311
58+
450020506764535784366644976997588757498856779786876686775868855996746554394579645638756632417635430
59+
333302106653766675873339799535474646879996789788987875587977755587884588498366525263657241244563411
60+
021603345313213632267754575346995947848565695878969885767869775868759846376874235783854235221066060
61+
451051105676415353834239879477689664878678578559658579558858859685656646333643282578751361221450043
62+
323133151167312457234657559765757999468467698869969666865897797857485499569838464565771443341433624
63+
351226435216677645378442636393858569688464696659679658777559879978765539986842833528347477142364355
64+
312522015134552576843564246393479975856585945659576986575979678487794748787843877358147735663364153
65+
436221405132742616333723774335457748498747869445768557777496645469557694497322763335217424704452516
66+
531453464276746646458848784658447874499485559877948986585644748765566863496685884585514544145343261
67+
124004035175455643547236885494794479865464946999998568586788689967343377758845738571672264321061003
68+
200155306351543777457257657968974568856495865454798449784799858884637386375422526864243565062364102
69+
445601414321444235648773664866559437996644487476459565964785659539635568456223755627145663416360463
70+
450034302502443262354377457789598776596746648495968457946465588399656898646426472411777146341644150
71+
023146264504256441756645634487555664987468946766784647877558359936934483654774826324735714633603030
72+
333342045300642417421427522458538378785933374679488696549488964673985948667688553344343522163201010
73+
455545402043235615564685485576228935958544883643955984493945756635487843653852747147246556553523510
74+
521503005555124536565566284723484688885348465998948435535997783785742684873228712152117622054235215
75+
312504066020457274731625463858577544637764966338466339435677364997568623562286441516367015430243140
76+
205031053461063473777474186844553532897797357638539497889554739476455778658267742121765420506102155
77+
234254241452503617412556672484735427269749864873783849964559983463526474536714377341620430652445005
78+
005122256601541121767617243673775345873688335643477574686454362545566824655271216641353663405312143
79+
433412443503526366274346321587248483458325774873734889987843368227357425636513234740304061623033442
80+
150425005143315445343366357776572258884478742563678357783845862235758564513525337622046146253440425
81+
225155323264631456635462325511885764326675428846268646575225734474437376612544667111053406332354205
82+
035014011000365630427277363261244836668367562448734772533252234588746775752633154601553624254020455
83+
012332415442566650666617225747354638747746384682743326746678826728355131225614470132623462424342514
84+
013524113311511615516563562155256314453777643324383464236254565752742637247411046651633511302344051
85+
212122212430404140032134131564756752725322885466434352224875446515552517267614410524244004301240143
86+
311400235445132520254065617566464245641623888587556667385287735312776421161334445544251524420400232
87+
440215452341344043062300567544524332733611636552822742567416277354477557756312354353622415135520342
88+
212430430313114143406656305336551452762327233736172563224242725162751616161102623013055041203553040
89+
321010353433305435042151016603142762425754273737777221111751751754217734105034366116400050235001301
90+
141012030043441152515110066520525231112547624256173331346341451761472053411546323404310404443302411
91+
133133034522454043232213312145664546126452755453724144154261454733124054403605004150535010413000334
92+
331412441152240551104612131344064131112564126362561532523311462604061151623121543003234021543312303
93+
010301442302500534021525552205536466144454523741535255444641345430402356234514221410435324321440211
94+
222301014410242404221240250563134040056104626021173454550103400400616550340400310015454450242211021
95+
233412422204111420244300456264320032401430366334634641441411125102262216452333414201235210001133200
96+
132244430320123233143125240232350142156620032556633233042303530460352401020133104323414224314240333
97+
003213243020203044035550441012322051645661162162123160565554312321050133555533333123214002014231123
98+
000032030402033315113220225514041611664662663525144303562161423513133325130431534323303033322212313
99+
101211324120322333355055001035515221630532526633042003421442155144120444232152535003241233413001002

‎day8/program.js

Copy file name to clipboard
+69Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
export default function Day(data) {
2+
let treeMap = convertTreesToMap(data);
3+
4+
treeMap.forEach(row => {
5+
traverseRow(traverseRow(row), true);
6+
});
7+
8+
let rotatedMap = rotateMap(treeMap);
9+
rotatedMap.forEach(row => {
10+
traverseRow(traverseRow(row), true);
11+
});
12+
13+
// count number of visible trees
14+
let visibleTrees = 0;
15+
treeMap.forEach(row => {
16+
row.forEach(tree => {
17+
if (tree.visible) {
18+
visibleTrees++;
19+
}
20+
});
21+
});
22+
23+
console.log({visibleTrees});
24+
}
25+
26+
// function to rotate the grid of the map
27+
function rotateMap(map) {
28+
return map[0].map((col, i) => map.map(row => row[i]));
29+
}
30+
31+
function traverseRow(row, reverse = false) {
32+
let visibleTree = true;
33+
34+
let from = reverse ? row.length - 1 : 0;
35+
let to = reverse ? 0 : row.length - 1;
36+
let step = reverse ? -1 : 1;
37+
let tallestTree = 0;
38+
39+
for (let i = from; i !== to; i += step) {
40+
41+
if (row[i].height > tallestTree) {
42+
tallestTree = row[i].height;
43+
}
44+
45+
if (row[i+step].height > tallestTree) {
46+
row[i+step].visible = true;
47+
}
48+
}
49+
50+
return row;
51+
}
52+
53+
function convertTreesToMap(trees) {
54+
let rows = [];
55+
56+
trees.forEach((treeRow, rowNum) => {
57+
let row = [];
58+
59+
let outsideRow = rowNum === 0 || rowNum === trees.length - 1;
60+
let rowLength = treeRow.length;
61+
62+
for (let i = 0; i < treeRow.length; i++) {
63+
row.push({height: parseInt(treeRow[i]), visible: outsideRow || i === 0 || i === rowLength - 1 ? true : false});
64+
}
65+
rows.push(row);
66+
});
67+
68+
return rows;
69+
}

‎day8/sample.txt

Copy file name to clipboard
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
30373
2+
25512
3+
65332
4+
33549
5+
35390

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.