diff --git a/2022/08/input b/2022/08/input new file mode 100644 index 0000000..67f3d8f --- /dev/null +++ b/2022/08/input @@ -0,0 +1,99 @@ +133120320210233440424211425033311533112110111336536142004454550513525522325223123404213204010312200 +201131111014211324423255354022022243226445013613610423653614522135534505055120330313403031333103010 +312033232323231244025301315245341424106564334260061464515142114141551050411122254121204214442432210 +101223224313414001513045124413405604251532415616200623342234245060512030200002055453441222043022132 +302000022010423415300513533001221654231646352603366222420353100303160621313144421120541324031322221 +022041343104313414435021243154525050055065506466215004364316065443031303052541012204020300331224312 +103341401201343234101335152026143021665443260024145323023321363412215203352344024010350241320230403 +113420142141151035404302242616255522460104100534576523546245441245051223305614204355454223220120301 +141313120334140413444311032422155464031057323373712672513175353623531154646460010504312302300002224 +222122233241135333145102102244620325637577115357373355166254434732406430504542404102101335430322111 +303334221050505214133200215501142243567431213624743475716112467711442460620524310041114411011423020 +314200333004550151023253063621162447552734266222424315622125733562141045411643325165010440012411104 +002041023423222500521530364652321342767176666734452465675417573276212213343035032650010252311440041 +424112310115314154624123026322722176144615437667237551164455332134452657635041110204303331153310423 +403331525242111322464321027125164326531546671615634451453753666614652523355664316050035035431040223 +043240150545010061116502263626227235517673287377662722735257631252161774623714640414211242003124031 +120153432445266666010553121747235535567222344854553227754373283562766712275433030145041514430322020 +130205252451225625553367153422361135376345652238658565468884466665635712475577163231554521534443142 +111112125502543266131422234726554665525278247588365447865236325776254766323137341434403365242323513 +303144132003506154604725457356146262354554756268227328243768455722852644644163423635454133132303535 +355321430425362320027571672735837582654822875282477453446323242873743335342262324741355251635040132 +134315335666154514547724226424352647855667536237263286847676436882327536177762242121655513144251300 +540433012152253201644772517187257277883368828477658659796427367852347254442546347411045431414354124 +123440236041532033152645165666552487887293658447738333978367787858534683322123676621510536503333401 +513523462114216537146475772264724378429935336847846348444575554346687726237537112655265045664650022 +152254463440132245722174776775434287876777476649865634494799993998688326736773121545343121065304241 +250224151321236513227671833576566739347663376834388478944395578563955424353347453672417622666244155 +515223301360505722213752783545553644495694566664938776366634393433784855746658113613537562051065554 +554241245610333147211334537367835857366454575934899676895788946595666473766285253431355325406031452 +100314521636253352672223432738853968767459797559486648888395749643933624738538781345525612342532041 +341462234622615555235573736647638675877393747847457878744844899855446378852378466172227414636330202 +345125124225634624432677378235488673587578945954767695474694483979633396738646725252521775306550112 +413420352557414334236458423883348897736674897485778466695756868398399557654674634472471146623221025 +251143521254271126368347667353953675589776644454498798849645686658369675764886773874127564444411365 +221413142252611772737673846553885549867478965666795885777985495763559573589863845361134756354652555 +243065641366675316826485533843887969469458797465896675796454946875597936434758642384462157714462066 +520553502161266638252882759578566955579648578498775589886979787578896857766344736358531725733564135 +424035430516117723442423478595343855468789497988556567558494878666699996375768427758374271236421335 +252313015177661777278432468494855844559656687766987679695575558744879656544795683583262246546564621 +066230444571526644743382974447835556985988567697575656976987588986595376466888728567376162756500435 +466346066733165454546438886894789699679455689766757985688888664759568963398874674733715475614334101 +325014552177542688745486585579599747867559596676796865787567755944786444539663262282351475561402534 +241163303516225237353826934978454869995779765759887659668665685578986859343646684568651136735352562 +450361264326612736655359595445797648876877955597997887987765775498545883744548752574473726565411112 +554605215356217343577344399893857478777797855566878769757669857854854856357865565536552164136515351 +526446641376374684266375494495458695869978975788699978669887967564769488567376982882762633361324360 +131314671231526545866684867949989786895865986986978678997899677764988874736369482822342624761635224 +623254034413371684428636874768586849979569867987768799666579796798469744978968758623884477174221325 +451620621666445477562568544366499474968796999686779676678989576768596484478933764745243527321126622 +454130052341168772645439448775675586969558878998779766779998798589986885955369648636673756137543515 +052463667567627485865289577459895546659595768687867979887989666587865576357344373227477771625616331 +460630633631764266522893997758984967975685969969798677679989558694657986684466928784764257141450213 +624130637644652532575536775488944945689777797679988979876565896659695947897698966634635115577122034 +312106635521463635433584476586795448766769568869779667989675669974989668369378542526764316562641343 +162151455625714643563689555468449898859656786796979798687579985864586658783373657585741615177725655 +522113305657225658843435489885646557577988797667968888669868768989959977879557377455733555272325032 +304510566333111422674633384687874565587979787768979689685867565897484896499888736286384551117254613 +501463041455354857783745556844585568845776985978966685877875599798784456633539465754543114144204310 +033044042511137887672349889484876755578886688778959586777755687684946795536886522746575472552603266 +506221035322726455557224463853355996897775558969955685797667999588974556935495475632746634441041151 +433136161743725565262636565676447685475989796568778598958657768879849778786859753335661722543205305 +265454361556653132263478888989675487694888975877759959776785674478666588788563747772733262666414514 +052441631644734262283652379957577654769979687765967795697668686969788694345962866843241632716655446 +236003436545632166356247457495793686988694668988898785686844466844799668333966735544674562633412352 +521460600027621624875854349394389587767654998655578976768847769685597639387383648267451531624063352 +342222114334344716267243334967869638699594976567878976765649455495834476654882454551336614731204621 +033566313156263553885434364795389663465876465857797479766958478858465479967473865454116236530266023 +312652012603144155543338267594667756579949844587785799854455597889659839838335338227724422251205254 +300006533237512225736566526838475448787546947968579975977675656694697397732838257677271224106345603 +133143326544516157647526755363599347754745759889457964588954897669586537634253625642342472416641244 +154255220262671713413357272553997888597745494647794956965689397737688378433337485552276212554465555 +100240040401314462336283253854484757937369955476776566463546476496874852334852883176722554655020500 +114224164252562264455188387248845988369836588899857935337746669656565572827235667727764740332042104 +210035410165643165676172383567428973379998596753776399638478657839665546428878531777515004325230423 +210124643010215635561443542385838474565467799589387569934754637575772733563765332372676641151131504 +302221012422133674231332744234367852633548864367379437949776375866557752335873376756770315423300102 +332125266633066263556242566636554222886478446383395355568773939525563625678526672212234343222625410 +025325451166410144267526457225543863563576656495967865696633785745578788783545643421165350000423343 +002055431510265615765734547166486528454663847694453963495886284323368575223225414276066312104153401 +355245141260142612771253626632378444477523462347872628387736452238277834561572523626005254261254042 +203545210065351052363237141625823577635472367448676276452678742864237273162112414622640501555430121 +335510044411666505051763342712375473385677658477545568853568734655885763312211631300144242225254322 +420503535251235101224431771412136627846753333256668624663675776462422374227471570160056336045543305 +311442220344522144201454355125161416822475756464544562823568565277363143626236634605524051151344323 +221042430004563020306242752654674353744234455564453257548654537765566764222366445225313642034222311 +410315020300414623613025143245525766347157877736466252545465214236744364172443246502214503112203230 +342304354225251006345461616171626576425171512572743837515273314522745717111151266035240243530314400 +101402542354002125435251304674647534521162334775342133371111747561164265140621412560624450505304430 +011211315525203435452400223603127166746242764515517634515312173434163762164444142306041434112503201 +312201041521122523410120106532007714457744313215145447526753371116262403622123305243410143403304001 +420143230433241404304622552610552657576751252637557577175144676122663421114064036341501055504120312 +321114124142324224151525015553652243236362311234247651266113535462142056365353412114023231401141232 +144010233331150422332523034411415500511027722375253755262514425243333354512150011301540445044301243 +321223141143321313415515024451144622413543514327723454716501265226013550620005111202312303242203422 +220312222242043544521300416060642531526303363025212311016020154415132623214322110011054242222314401 +120043213410140522500232142235034335033562221102412305131143065020436150033340525535242031300403012 +333011210021031104353104420453564321600565055235040060414452305642301521213234000231332120021102311 +213000422032423433105500120521112552552162045443040565055142555024614151234210022135411312102303010 +311113213314300134505043355233124354500023350433035532626242646113034040102425400244311432332422101 diff --git a/2022/08/part1.js b/2022/08/part1.js new file mode 100644 index 0000000..5d9cffd --- /dev/null +++ b/2022/08/part1.js @@ -0,0 +1,87 @@ +import * as R from "remeda"; +import { readFile } from "../05/part1.js"; +import Victor from '@a-robu/victor'; + +const V = (...args) => new Victor(...args); + +const readInput = (...args) => + readFile(...args) + .split("\n") + .filter((x) => x) + .map( l => l.split('').map( x => parseInt(x) ) ); + +export function generateVisibilityGrid(forest) { + const size = forest.length; + const visibility = Array(size).fill(null).map( + () => Array(size).fill(false) + ); + + // top + const positions = R.range(size)(0).map( x => ({ + height: -1, + position: V(x,-1), + inc: V(0,1), + }) ); + + // bottom + positions.push( +...R.range(size)(0).map( x => ({ + height: -1, + position: V(x,size), + inc: V(0,-1), + }) ) + ); + + positions.push( +...R.range(size)(0).map( x => ({ + height: -1, + position: V(-1,x), + inc: V(1,0), + }) ) + ); + positions.push( +...R.range(size)(0).map( x => ({ + height: -1, + position: V(size,x), + inc: V(-1,0), + }) ) + ); + + const outOfBound = (pos) => { + if( Math.min( pos.x, pos.y ) < 0 ) return true; + if( Math.max( pos.x, pos.y ) >= size ) return true; + return false; + } + + while( positions.length ) { + const pos = positions.shift(); + pos.position.add( pos.inc ); + + if( outOfBound( pos.position ) ) continue; + + if( forest[pos.position.x][pos.position.y] > pos.height ) { + visibility[pos.position.x][pos.position.y] = true; + pos.height = forest[pos.position.x][pos.position.y]; + } + + positions.push(pos); + } + + return visibility; +} + +export const puzzleInput = readInput(import.meta.url, "input"); +export const sample = readInput(import.meta.url, "sample"); + +function printMap( forest ) { + forest.forEach( line => console.log(line.join(' ')) ); + return forest; +} + +export default R.createPipe( + printMap, + generateVisibilityGrid, + printMap, + R.flatten, + R.countBy(R.identity) +); diff --git a/2022/08/part2.js b/2022/08/part2.js new file mode 100644 index 0000000..10fc179 --- /dev/null +++ b/2022/08/part2.js @@ -0,0 +1,4 @@ +import * as R from "remeda"; + + +export default () => {}; diff --git a/2022/08/puzzle.md b/2022/08/puzzle.md new file mode 100644 index 0000000..45a66ba --- /dev/null +++ b/2022/08/puzzle.md @@ -0,0 +1,41 @@ +\--- Day 8: Treetop Tree House --- +---------- + +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). + +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. + +The Elves have already launched a [quadcopter](https://en.wikipedia.org/wiki/Quadcopter) to generate a map with the height of each tree (your puzzle input). For example: + +``` +30373 +25512 +65332 +33549 +35390 + +``` + +Each tree is represented as a single digit whose value is its height, where `0` is the shortest and `9` is the tallest. + +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. + +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: + +* 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.) +* The top-middle `5` is *visible* from the top and right. +* 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. +* The left-middle `5` is *visible*, but only from the right. +* 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. +* The right-middle `3` is *visible* from the right. +* In the bottom row, the middle `5` is *visible*, but the `3` and `4` are not. + +With 16 trees visible on the edge and another 5 visible in the interior, a total of `*21*` trees are visible in this arrangement. + +Consider your map; *how many trees are visible from outside the grid?* + +To begin, [get your puzzle input](8/input). + +Answer: + +You can also [Shareon [Twitter](https://twitter.com/intent/tweet?text=%22Treetop+Tree+House%22+%2D+Day+8+%2D+Advent+of+Code+2022&url=https%3A%2F%2Fadventofcode%2Ecom%2F2022%2Fday%2F8&related=ericwastl&hashtags=AdventOfCode) [Mastodon](javascript:void(0);)] this puzzle. \ No newline at end of file diff --git a/2022/08/sample b/2022/08/sample new file mode 100644 index 0000000..16d6fbd --- /dev/null +++ b/2022/08/sample @@ -0,0 +1,5 @@ +30373 +25512 +65332 +33549 +35390 diff --git a/2022/08/test.js b/2022/08/test.js new file mode 100644 index 0000000..f1b2b98 --- /dev/null +++ b/2022/08/test.js @@ -0,0 +1,21 @@ +import { test, expect, describe } from "vitest"; + +import { expectSolution } from "../01/main.js"; +import part1, { sample, puzzleInput, generateVisibilityGrid } from "./part1.js"; +import part2 from "./part2.js"; + +describe("part 1", () => { + test( "sample", () => { + expect( part1(sample) ).toBe(21); + + }); + test("solution", () => { + expectSolution(part1(puzzleInput)).toEqual(1703); + }); +}); + +describe("part 2", () => { + test.todo("solution", () => { + expectSolution(part2(puzzleInput)).toEqual("TODO"); + }); +});