-
Notifications
You must be signed in to change notification settings - Fork 0
/
task 8.js
259 lines (230 loc) · 14.3 KB
/
task 8.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
const input = `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`;
const test_input = `30373
25512
65332
33549
35390`;
// part 1
const check_tree_is_visible = (x, y, forest, log = false) => {
const height = forest.length;
const width = forest[0].length;
if (!x) return true;
if (!y) return true;
if (x == width - 1) return true;
if (y == height - 1) return true;
const current_tree = forest[y][x];
// check all by left
const up = forest.slice(0, y).map((l) => l[x]);
const down = forest.slice(y + 1, height).map((l) => l[x]);
const left = forest[y].slice(0, x);
const right = forest[y].slice(x + 1, width);
if (log)
console.log({ x, y, current_tree, left, right, up, down, height, width });
if (left.every((t) => t < current_tree)) return true;
if (right.every((t) => t < current_tree)) return true;
if (up.every((t) => t < current_tree)) return true;
if (down.every((t) => t < current_tree)) return true;
return false;
};
const solution_1 = (input) => {
const tree_arrays = input.split("\n").map((l) => l.split(""));
return tree_arrays.reduce(
(acc, t_arr, y) =>
+acc +
+t_arr.reduce(
(a, t, x) => +a + +check_tree_is_visible(x, y, tree_arrays),
0
),
0
);
};
const mapped_test_case = test_input.split("\n").map((l) => l.split(""));
console.assert(
check_tree_is_visible(1, 1, mapped_test_case) == true,
"The top-left 5 is visible from the left and top. (It isn't visible from the right or bottom since other trees of height 5 are in the way.)"
);
console.assert(
check_tree_is_visible(2, 1, mapped_test_case) == true,
"The top-middle 5 is visible from the top and right"
);
console.assert(
check_tree_is_visible(3, 1, mapped_test_case, true) == false,
"The top-right 1 is not visible from any direction; for it to be visible, there would need to only be trees of height 0 between it and an edge."
);
console.assert(
check_tree_is_visible(2, 2, mapped_test_case, true) == false,
"The center 3 is not visible from any direction; for it to be visible, there would need to be only trees of at most height 2 between it and an edge."
);
console.assert(
check_tree_is_visible(3, 2, mapped_test_case) == true,
"The right-middle 3 is visible from the right."
);
console.assert(
check_tree_is_visible(2, 3, mapped_test_case) == true,
"In the bottom row, the middle 5 is visible, but the 3 and 4 are not."
);
console.assert(
solution_1(test_input) == 21,
"With 16 trees visible on the edge and another 5 visible in the interior, a total of 21 trees are visible in this arrangement."
);
// solution for 1st part
console.table({
"part 1": solution_1(input),
});
// part 2
const get_tree_scenic_score = (x, y, forest, log = false) => {
const height = forest.length;
const width = forest[0].length;
const current_tree = forest[y][x];
const up = forest
.slice(0, y)
.map((l) => l[x])
.reverse();
const down = forest.slice(y + 1, height).map((l) => l[x]);
const left = forest[y].slice(0, x).reverse();
const right = forest[y].slice(x + 1, width);
const highest_up = up.findIndex((t) => t >= current_tree);
const score_up = highest_up == -1 ? up.length : highest_up + 1;
const highest_down = down.findIndex((t) => t >= current_tree);
const score_down = highest_down == -1 ? down.length : highest_down + 1;
const highest_left = left.findIndex((t) => t >= current_tree);
const score_left = highest_left == -1 ? left.length : highest_left + 1;
const highest_right = right.findIndex((t) => t >= current_tree);
const score_right = highest_right == -1 ? right.length : highest_right + 1;
if (log)
console.log({
coords: { x, y, height, width },
current_tree,
dirs: { left, right, up, down },
scores: { score_left, score_right, score_up, score_down },
});
return score_left * score_right * score_up * score_down;
};
console.assert(
get_tree_scenic_score(2, 1, mapped_test_case) == 4,
"For this tree, this is 4 (found by multiplying 1 * 1 * 2 * 2)."
);
console.assert(
get_tree_scenic_score(2, 3, mapped_test_case) == 8,
"This tree's scenic score is 8 (2 * 2 * 1 * 2); this is the ideal spot for the tree house."
);
const solution_2 = (input) => {
const tree_arrays = input.split("\n").map((l) => l.split(""));
return tree_arrays.reduce(
(acc, t_arr, y) =>
Math.max(
acc,
t_arr.reduce(
(a, t, x) => Math.max(a, get_tree_scenic_score(x, y, tree_arrays)),
0
)
),
0
);
};
console.assert(
solution_2(test_input) == 8,
"This tree's scenic score is 8 (2 * 2 * 1 * 2); this is the ideal spot for the tree house."
);
// solution for 2nd part
console.table({
"part 2": solution_2(input),
});