Skip to content

Commit 1d529e4

Browse files
committed
Add day 8
1 parent dfe8fe1 commit 1d529e4

File tree

4 files changed

+285
-1
lines changed

4 files changed

+285
-1
lines changed

Diff for: 2022/08/day.08.input.txt

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
011201001000123232122300403211332202122424332111151523152310403222120311202224013311113203311122001
2+
102201211220103202323002411123324024134251423232343141424431144431022143443131141222323122010012220
3+
002022232232102013311311301321044421352134513235443522243113311323213013301030440423203311103101020
4+
002212021110130112400112141310424411421435324412235335534245212325313312413434430114302030310200000
5+
202000312203303110042324124431114212153432535412115323131323541522511253002131401212012131031321110
6+
120203002030304244102444143021314124552253521343321513511124144454343454102412220143321013220111212
7+
012133330030200444414333325255125142322121412245551424242135123513411533153323130432231002121211210
8+
012300220003100211444401144353345522135453435223464636451554321531323513234331131114224102232333310
9+
000201310302031301344341134512444312432566266533252332656654251123535525135213300040211242011230031
10+
121002133124022203240512452433331223524622643252532325346635555552514352434233454024134201432001111
11+
112213110140442401434531255323512525265655352543623565556265545555235551143135124203142220020302000
12+
033001202421424143555311243322416262636322654432542644633525453666655354514351121150231101002002310
13+
133303044321221102531154151541224245536464535622566355532543644552554644312231415325142303020031312
14+
112333344411024045335422225146442243536464622553556234523445362524655623435243115242532313102020021
15+
123023422310204444121415255225254435644446326664473564435422565325224635623542352154120001224144312
16+
102303024142131515433355242322223363453633455657436436574636544336652564626641215232314243234004001
17+
021344140434455245134415553542352643537665534644574553367446664254532523624521243554252411433032112
18+
100140101131444142414254644644434663647646555637737475474473636562243265243536422253531252202124322
19+
111043301134142345422324342454235374733474673456744577746755765343355535622342432251442244140231224
20+
232120141224122324244532255543256467566454337465647735433663734756446566663442525425254525142212311
21+
102310340244433433553362546534535757453376643763344566566557634664535744445254533545223521213213014
22+
330111232252114431226334564636467643777466445537346735563633337744734474466254662225321443542401243
23+
344311321253412213666443354643647347656735478884748547564466577774737677363226422622515344334010341
24+
222340105234132533423442424376674757757675647476557547564445666676437755355626455436313331225343402
25+
334241221121414455435265324646757456774784864846746867457546648735456677555624445353555434433340341
26+
333430021343121666326363537633737767788775478777445558858444854755434544735362564626265444342330020
27+
332224425333452333556456745375454657668644877658744844547774765844454475434765533256545152435214410
28+
411322431421116436336347456774356844455867864644844878664785864444475567664473365652253123453220003
29+
130345455115155526426437663665665668576686587847778476557678655576775553535745643522532344433523042
30+
241333521455132466523664543556384776587787745658797755565556576546847447434775733626365325451531301
31+
202135433134452323646546565545646885747548679876767658578565476458486465755545465664435422532454541
32+
344025243154435524235475655536845545775797795959686975588855546544875685664377465455353661444452422
33+
120511445324333353265334766654858656857755669695597899959957757545465687447345336332322634434414522
34+
041425252513666643357433333888555778459755889566967799659865567757457567576463765323236366123221323
35+
142343534134652443554434768544658774587669979986775985989879787975445856456554364645323653454511323
36+
443155144123223566654765746758688875856899955996667659755976778586644774777456744653322566354532232
37+
013242522162435447764573774877757879868788876799896979576555597985885468586473474464455426633333452
38+
101431333345553353553337548577478656967866886877887889699999856979976485874455544477534422463331314
39+
422325541533664573334736464857446675995799977868697688998787757585668584775766563477536636621514142
40+
313315332565562573637376866486547776557979868979996867779966955885566785455767644565336236452541411
41+
231122455364326235655644685888786685587796897688777698977986787596655865857448333576665523356143332
42+
314415154446535337443754887576795586569678967879977696896667866768856785586774365336636426455324541
43+
251324556334435537567468544665859588888869687767876697967887867856679658788866355353346553442543353
44+
134353225423362456734666455757877769577678987866887877799886886885859796767664444665534363236114533
45+
251241346545626367667578575687955957988796678987999979977897978877579787844755554533763646262143315
46+
353543555544353753474346565789555799696999667888997899777669899867697878476845756644734243432245123
47+
221521465632632737777766744748678869769669668898887888987898666789869785645678734643552655525624335
48+
351552456455622567337644647486678857766866879798779999878779869885869788874885873744772665326352324
49+
153242153665224557353455465857685566797887699998988788877767696668668675666664465757646436665554315
50+
321113152666424547657348876876678597898779877779998999978978777885976658558676664444674243656333212
51+
421511245364456777655754476646885699898678788797778788778778999795687589454774563737333365434325525
52+
213532265252653474345766566877669898969767779777879797899977669868595997784466766655547455234435452
53+
125232235356525353764645486577668857868788699799787978898988669878688867586564457353673236664223343
54+
441242562562223643773466848558599657669669968987979987787969676777796877655876456745562442232541525
55+
135122156635652746574578664849659556998698797778797889988788869797687977855447474475546334662522444
56+
144315413442434666777478887855885568968799966889978797797867786966685589646488554737453225335213325
57+
511213234443636433646678657654587788667696879878989799867699988765895758788675765445656425425334544
58+
052145146243336363474565857687976889776768867898889967686967668565998666768575534675445244546424155
59+
235513424332444756467574556745997958679678999799789777998968876679956958746554677637762355253554335
60+
331155256654246447545776676487485655676699678967897779688898989999768655488874564764434223445132152
61+
152152253254255234473347878744485766678897879689897888667968795675655864748584644653323363464154251
62+
245154344235223346354765457746867586969968687876867899766986679689585778787473555663424264553541344
63+
235253541263666477544377746867785766579566868896689699876766695956667455774677646576554255521144355
64+
033522343456423455634354575764856898657669957896677776766596688857787474488436433455326554242331222
65+
321532413246333226435563556578864759978657759897896799958558598588975755858856537673332523244522141
66+
101422552442534324467345475684574877989759655788785857697957976679848768474665346435664653421242444
67+
103425135453353666736674677847548667967885667765576995797857589796866646565443377775564436443333543
68+
041131245522253556554636537878457787468577768876778599999689667978487755747676763666266652435545351
69+
424232344536333364556466767555646468677996965895798968676966868665587584664535544755645232211324114
70+
334034423343366322247357747635787857555667756956768657779596877655676555367754633345545442422145333
71+
424344345121532564223643563774744574746844686888885779565686455664565643634566756543362551431252401
72+
232123114442152332546734767375678475664877645798695799654765488755876545334345452455463633534433233
73+
414423121145222454332477456654436757485685646546884446667744645744675377357654436554353221451345404
74+
234130223314353235262655676637577858558554556467788664555466588778677546456334642623666524333250443
75+
414001252524152463224524734765775677644765886656877578666575855788653533364773455446565352432424320
76+
024323134311552535243564667476365737764685474446876486685546667685363735646643655435355533243440034
77+
331141044111451522226436327536773637654887585478885586588745847444466365767232265352211455413531431
78+
241321311312323233252665352455436545735388846445565487667644555744667753556266645332452431541341044
79+
234030032312154414563442653244476576544654676884777445766434443476675746365245632242515342521301442
80+
202112004552434344564624422526757574735736566643654737673776743354737744242533546523434111443034202
81+
323422334452422412134566522262537334374446646774476534347655656336555632626663364214333341322443300
82+
031243421344143515452456444553266345437737337675574563437533573575456463665443432215213541123241240
83+
002114441013421154331235436253442576764555535635775735635377573353666526333623554524421352131443020
84+
111342301013125124521245222663363363467436357434736773473777464564655622533254352444544553311114233
85+
222323000331131531351433363334345533653474663466435557365553376535246564324662454422135233103003200
86+
002103300342403311123432563445263435443255635353333564444573456633466424623313351545133022013122320
87+
300304042333200223123432352234525626625234236453763536563235242224645265643515312312520000242200000
88+
210121244033001141551213125554433445222532265463624234562324443435344362451243522525243002401410013
89+
303323342040431313415255233251226626334264553655346266323665655652522363324341123235410333230402230
90+
300112203021311400115231251543524354554262365526643622264625434236554412121342322522123434120120203
91+
011001023222004142422445443143212526654365444643442536365453622222634154551441553400220414300301332
92+
230032020342121242113341435444543253355352433246243543645634456552525315313143243431133302112320020
93+
232132202131312144424224413332214322543242664652323324456222615212244414145113442320023403200012111
94+
112222030320432400004224215552515412325221525356363643351335211141344255554442403013303120331312202
95+
110321230332323212301432002424211134155124241552254334221154111545254253122214132321141212032221001
96+
021020121321302320401011302153145251455114411155452245422422135453412153112033343412340101230302010
97+
122203012232330222213342133012334131225335225325343355122151154444535354414423434423220001330003020
98+
221120213213221333210131242440001425551432522221141211124544343151315104244333311124033221102312011
99+
202201033331203122413021242022211002455531321354252412214332215513311231404032244320020330332301120

Diff for: 2022/08/day.08.ipynb

+182
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "5ad90425-320f-4c7d-aa2d-9702f332331b",
6+
"metadata": {
7+
"tags": []
8+
},
9+
"source": [
10+
"# Day 8: Treetop Tree House\n",
11+
"\n",
12+
"[https://adventofcode.com/2022/day/8](https://adventofcode.com/2022/day/8)\n",
13+
"\n",
14+
"## Description\n",
15+
"\n",
16+
"### Part One\n",
17+
"\n",
18+
"The expedition comes across a peculiar patch of tall trees all planted carefully in a grid. The Elves explain that a previous expedition planted these trees as a reforestation effort. Now, they're curious if this would be a good location for a [tree house](https://en.wikipedia.org/wiki/Tree_house).\n",
19+
"\n",
20+
"First, determine whether there is enough tree cover here to keep a tree house _hidden_. To do this, you need to count the number of trees that are _visible from outside the grid_ when looking directly along a row or column.\n",
21+
"\n",
22+
"The Elves have already launched a [quadcopter](https://en.wikipedia.org/wiki/Quadcopter) to generate a map with the height of each tree (<span title=\"The Elves have already launched a quadcopter (your puzzle input).\">your puzzle input</span>). For example:\n",
23+
"\n",
24+
" 30373\n",
25+
" 25512\n",
26+
" 65332\n",
27+
" 33549\n",
28+
" 35390\n",
29+
" \n",
30+
"\n",
31+
"Each tree is represented as a single digit whose value is its height, where `0` is the shortest and `9` is the tallest.\n",
32+
"\n",
33+
"A tree is _visible_ if all of the other trees between it and an edge of the grid are _shorter_ than it. Only consider trees in the same row or column; that is, only look up, down, left, or right from any given tree.\n",
34+
"\n",
35+
"All of the trees around the edge of the grid are _visible_ - since they are already on the edge, there are no trees to block the view. In this example, that only leaves the _interior nine trees_ to consider:\n",
36+
"\n",
37+
"* 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.)\n",
38+
"* The top-middle `5` is _visible_ from the top and right.\n",
39+
"* 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.\n",
40+
"* The left-middle `5` is _visible_, but only from the right.\n",
41+
"* 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.\n",
42+
"* The right-middle `3` is _visible_ from the right.\n",
43+
"* In the bottom row, the middle `5` is _visible_, but the `3` and `4` are not.\n",
44+
"\n",
45+
"With 16 trees visible on the edge and another 5 visible in the interior, a total of _`21`_ trees are visible in this arrangement.\n",
46+
"\n",
47+
"Consider your map; _how many trees are visible from outside the grid?_"
48+
]
49+
},
50+
{
51+
"cell_type": "code",
52+
"execution_count": 1,
53+
"id": "7336b37d-bfe8-4a1d-9e84-4af90282709d",
54+
"metadata": {},
55+
"outputs": [
56+
{
57+
"data": {
58+
"text/plain": [
59+
"1845"
60+
]
61+
},
62+
"execution_count": 1,
63+
"metadata": {},
64+
"output_type": "execute_result"
65+
}
66+
],
67+
"source": [
68+
"import pandas as pd\n",
69+
"\n",
70+
"grid = open(\"day.08.input.txt\").read().split(\"\\n\")\n",
71+
"\n",
72+
"grid = pd.DataFrame([[int(t) for t in l] for l in grid])\n",
73+
"\n",
74+
"visible_from_top = grid.gt(grid.expanding().max().shift(1).fillna(-1))\n",
75+
"visible_from_left = grid.T.gt(grid.T.expanding().max().shift(1).fillna(-1)).T\n",
76+
"visible_from_bottom = grid.iloc[::-1, :].gt(grid.iloc[::-1, :].expanding().max().shift(1).fillna(-1)).iloc[::-1, :]\n",
77+
"visible_from_right = grid.T.iloc[::-1, :].gt(grid.T.iloc[::-1, :].expanding().max().shift(1).fillna(-1)).iloc[::-1, :].T\n",
78+
"\n",
79+
"(visible_from_top | visible_from_left | visible_from_bottom | visible_from_right).sum().sum()"
80+
]
81+
},
82+
{
83+
"cell_type": "markdown",
84+
"id": "69490cf3-a977-4a63-9a2c-50c231886c54",
85+
"metadata": {},
86+
"source": [
87+
"### Part Two\n",
88+
"\n",
89+
"Content with the amount of tree cover available, the Elves just need to know the best spot to build their tree house: they would like to be able to see a lot of _trees_.\n",
90+
"\n",
91+
"To measure the viewing distance from a given tree, look up, down, left, and right from that tree; stop if you reach an edge or at the first tree that is the same height or taller than the tree under consideration. (If a tree is right on the edge, at least one of its viewing distances will be zero.)\n",
92+
"\n",
93+
"The Elves don't care about distant trees taller than those found by the rules above; the proposed tree house has large [eaves](https://en.wikipedia.org/wiki/Eaves) to keep it dry, so they wouldn't be able to see higher than the tree house anyway.\n",
94+
"\n",
95+
"In the example above, consider the middle `5` in the second row:\n",
96+
"\n",
97+
" 30373\n",
98+
" 25512\n",
99+
" 65332\n",
100+
" 33549\n",
101+
" 35390\n",
102+
" \n",
103+
"\n",
104+
"* Looking up, its view is not blocked; it can see _`1`_ tree (of height `3`).\n",
105+
"* Looking left, its view is blocked immediately; it can see only _`1`_ tree (of height `5`, right next to it).\n",
106+
"* Looking right, its view is not blocked; it can see _`2`_ trees.\n",
107+
"* Looking down, its view is blocked eventually; it can see _`2`_ trees (one of height `3`, then the tree of height `5` that blocks its view).\n",
108+
"\n",
109+
"A tree's _scenic score_ is found by _multiplying together_ its viewing distance in each of the four directions. For this tree, this is _`4`_ (found by multiplying `1 * 1 * 2 * 2`).\n",
110+
"\n",
111+
"However, you can do even better: consider the tree of height `5` in the middle of the fourth row:\n",
112+
"\n",
113+
" 30373\n",
114+
" 25512\n",
115+
" 65332\n",
116+
" 33549\n",
117+
" 35390\n",
118+
" \n",
119+
"\n",
120+
"* Looking up, its view is blocked at _`2`_ trees (by another tree with a height of `5`).\n",
121+
"* Looking left, its view is not blocked; it can see _`2`_ trees.\n",
122+
"* Looking down, its view is also not blocked; it can see _`1`_ tree.\n",
123+
"* Looking right, its view is blocked at _`2`_ trees (by a massive tree of height `9`).\n",
124+
"\n",
125+
"This tree's scenic score is _`8`_ (`2 * 2 * 1 * 2`); this is the ideal spot for the tree house.\n",
126+
"\n",
127+
"Consider each tree on your map. _What is the highest scenic score possible for any tree?_\n"
128+
]
129+
},
130+
{
131+
"cell_type": "code",
132+
"execution_count": 2,
133+
"id": "263e2852-7fd3-4c61-bfc8-0c48f40cb344",
134+
"metadata": {},
135+
"outputs": [
136+
{
137+
"data": {
138+
"text/plain": [
139+
"230112"
140+
]
141+
},
142+
"execution_count": 2,
143+
"metadata": {},
144+
"output_type": "execute_result"
145+
}
146+
],
147+
"source": [
148+
"import numpy as np\n",
149+
"\n",
150+
"score = grid.copy()\n",
151+
"for y in grid.index:\n",
152+
" for x in grid.columns:\n",
153+
" score.iloc[y, x] = np.prod([line_of_sight.expanding().max().lt(grid.iloc[y, x]).sum() + \\\n",
154+
" int(line_of_sight.max() >= grid.iloc[y, x]) for line_of_sight in \\\n",
155+
" [grid.iloc[:y, x].iloc[::-1], grid.iloc[y+1:, x], grid.iloc[y, :x].iloc[::-1], grid.iloc[y, x+1:]]])\n",
156+
"\n",
157+
"score.max().max()"
158+
]
159+
}
160+
],
161+
"metadata": {
162+
"kernelspec": {
163+
"display_name": "Python 3 (ipykernel)",
164+
"language": "python",
165+
"name": "python3"
166+
},
167+
"language_info": {
168+
"codemirror_mode": {
169+
"name": "ipython",
170+
"version": 3
171+
},
172+
"file_extension": ".py",
173+
"mimetype": "text/x-python",
174+
"name": "python",
175+
"nbconvert_exporter": "python",
176+
"pygments_lexer": "ipython3",
177+
"version": "3.9.2"
178+
}
179+
},
180+
"nbformat": 4,
181+
"nbformat_minor": 5
182+
}

Diff for: README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ My solutions to the 2022 Advent of Code as Jupyter notebooks. The markdown for e
1212

1313
<!-- AOC TILES BEGIN -->
1414
<h2>
15-
Advent of Code 2022 - Stars Collected: 12
15+
Advent of Code 2022 - Stars Collected: 14
1616
</h2>
1717
<p>
1818
The follwing table lists all solutions per day along with the corresponding completion time and rank. The table was generated using <a href="https://github.com/LiquidFun/adventofcode/tree/main/AoCTiles">AoCTiles</a>.
@@ -35,6 +35,9 @@ My solutions to the 2022 Advent of Code as Jupyter notebooks. The markdown for e
3535
<a href="2022/07/day.07.ipynb">
3636
<img src="assets/media/2022/07.png" width="161px">
3737
</a>
38+
<a href="2022/08/day.08.ipynb">
39+
<img src="assets/media/2022/08.png" width="161px">
40+
</a>
3841
<a href="None">
3942
<img src="assets/media/2022/06.png" width="161px">
4043
</a>

Diff for: assets/media/2022/08.png

10.7 KB
Loading

0 commit comments

Comments
 (0)