From 9ade7e03135ca0a9b5a6d8748132347664d58672 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Thu, 8 Dec 2022 10:15:42 -0500 Subject: [PATCH 1/7] hygen --- 2022/template/part1.js | 1 - 2022/template/part2.js | 1 - Taskfile.yaml | 6 ++++++ _templates/day/new/hello.ejs.t | 15 +++++++++++++++ _templates/day/new/part1.ejs | 6 ++++++ _templates/day/new/part2.ejs | 7 +++++++ _templates/day/new/prompt.js | 7 +++++++ .../test.js => _templates/day/new/test.ejs | 11 +++++++---- _templates/generator/help/index.ejs.t | 5 +++++ _templates/generator/new/hello.ejs.t | 18 ++++++++++++++++++ _templates/generator/with-prompt/hello.ejs.t | 18 ++++++++++++++++++ _templates/generator/with-prompt/prompt.ejs.t | 14 ++++++++++++++ _templates/init/repo/new-repo.ejs.t | 4 ++++ 13 files changed, 107 insertions(+), 6 deletions(-) delete mode 100644 2022/template/part1.js delete mode 100644 2022/template/part2.js create mode 100644 _templates/day/new/hello.ejs.t create mode 100644 _templates/day/new/part1.ejs create mode 100644 _templates/day/new/part2.ejs create mode 100644 _templates/day/new/prompt.js rename 2022/template/test.js => _templates/day/new/test.ejs (50%) create mode 100644 _templates/generator/help/index.ejs.t create mode 100644 _templates/generator/new/hello.ejs.t create mode 100644 _templates/generator/with-prompt/hello.ejs.t create mode 100644 _templates/generator/with-prompt/prompt.ejs.t create mode 100644 _templates/init/repo/new-repo.ejs.t diff --git a/2022/template/part1.js b/2022/template/part1.js deleted file mode 100644 index b509eb0..0000000 --- a/2022/template/part1.js +++ /dev/null @@ -1 +0,0 @@ -import * as R from "remeda"; diff --git a/2022/template/part2.js b/2022/template/part2.js deleted file mode 100644 index b509eb0..0000000 --- a/2022/template/part2.js +++ /dev/null @@ -1 +0,0 @@ -import * as R from "remeda"; diff --git a/Taskfile.yaml b/Taskfile.yaml index af75c42..eaa296d 100644 --- a/Taskfile.yaml +++ b/Taskfile.yaml @@ -6,6 +6,8 @@ vars: YEAR: 2022 DAY: sh: date '+%d' | perl -pe's/^0//' + DAY_FORMATTED: + sh: date '+%d' GREETING: Hello, World! tasks: @@ -25,6 +27,10 @@ tasks: cmds: - firefox https://adventofcode.com/{{.YEAR}}/day/{{.DAY}} + download: + cmds: + - aoc download -d {{.DAY}} -o -p 2022/{{.DAY_FORMATTED}}/puzzle.md -i 2022/{{.DAY_FORMATTED}}/input + default: cmds: - echo "{{.GREETING}}" diff --git a/_templates/day/new/hello.ejs.t b/_templates/day/new/hello.ejs.t new file mode 100644 index 0000000..095a04e --- /dev/null +++ b/_templates/day/new/hello.ejs.t @@ -0,0 +1,15 @@ +--- +to: app/hello.js +--- +const hello = ``` +Hello! +This is your first hygen template. + +Learn what it can do here: + +https://github.com/jondot/hygen +``` + +console.log(hello) + + diff --git a/_templates/day/new/part1.ejs b/_templates/day/new/part1.ejs new file mode 100644 index 0000000..97460f5 --- /dev/null +++ b/_templates/day/new/part1.ejs @@ -0,0 +1,6 @@ +--- +to: 2022/<%= day %>/part1.js +--- +import * as R from "remeda"; + +export default = () => {}; diff --git a/_templates/day/new/part2.ejs b/_templates/day/new/part2.ejs new file mode 100644 index 0000000..e59c761 --- /dev/null +++ b/_templates/day/new/part2.ejs @@ -0,0 +1,7 @@ +--- +to: 2022/<%= day %>/part2.js +--- +import * as R from "remeda"; + + +export default = () => {}; diff --git a/_templates/day/new/prompt.js b/_templates/day/new/prompt.js new file mode 100644 index 0000000..bfff832 --- /dev/null +++ b/_templates/day/new/prompt.js @@ -0,0 +1,7 @@ +module.exports = [ + { + type: 'input', + name: 'day', + message: "advent day?" + } +] diff --git a/2022/template/test.js b/_templates/day/new/test.ejs similarity index 50% rename from 2022/template/test.js rename to _templates/day/new/test.ejs index 07ae1e1..f82ac18 100644 --- a/2022/template/test.js +++ b/_templates/day/new/test.ejs @@ -1,17 +1,20 @@ +--- +to: 2022/<%= day %>/test.js +--- import { test, expect, describe } from "vitest"; import { expectSolution } from "../01/main.js"; -import { solutionPart1, puzzleInput } from "./part1.js"; -import { solutionPart2 } from "./part2.js"; +import part1, { sample, puzzleInput } from "./part1.js"; +import part2 from "./part2.js"; describe("part 1", () => { test.todo("solution", () => { - expectSolution(solutionPart1(puzzleInput)).toEqual("TODO"); + expectSolution(part1(puzzleInput)).toEqual("TODO"); }); }); describe("part 2", () => { test.todo("solution", () => { - expectSolution(solutionPart2(puzzleInput)).toEqual("TODO"); + expectSolution(part2(puzzleInput)).toEqual("TODO"); }); }); diff --git a/_templates/generator/help/index.ejs.t b/_templates/generator/help/index.ejs.t new file mode 100644 index 0000000..90a29af --- /dev/null +++ b/_templates/generator/help/index.ejs.t @@ -0,0 +1,5 @@ +--- +message: | + hygen {bold generator new} --name [NAME] --action [ACTION] + hygen {bold generator with-prompt} --name [NAME] --action [ACTION] +--- \ No newline at end of file diff --git a/_templates/generator/new/hello.ejs.t b/_templates/generator/new/hello.ejs.t new file mode 100644 index 0000000..5680d96 --- /dev/null +++ b/_templates/generator/new/hello.ejs.t @@ -0,0 +1,18 @@ +--- +to: _templates/<%= name %>/<%= action || 'new' %>/hello.ejs.t +--- +--- +to: app/hello.js +--- +const hello = ``` +Hello! +This is your first hygen template. + +Learn what it can do here: + +https://github.com/jondot/hygen +``` + +console.log(hello) + + diff --git a/_templates/generator/with-prompt/hello.ejs.t b/_templates/generator/with-prompt/hello.ejs.t new file mode 100644 index 0000000..ba6abc5 --- /dev/null +++ b/_templates/generator/with-prompt/hello.ejs.t @@ -0,0 +1,18 @@ +--- +to: _templates/<%= name %>/<%= action || 'new' %>/hello.ejs.t +--- +--- +to: app/hello.js +--- +const hello = ``` +Hello! +This is your first prompt based hygen template. + +Learn what it can do here: + +https://github.com/jondot/hygen +``` + +console.log(hello) + + diff --git a/_templates/generator/with-prompt/prompt.ejs.t b/_templates/generator/with-prompt/prompt.ejs.t new file mode 100644 index 0000000..76ea532 --- /dev/null +++ b/_templates/generator/with-prompt/prompt.ejs.t @@ -0,0 +1,14 @@ +--- +to: _templates/<%= name %>/<%= action || 'new' %>/prompt.js +--- + +// see types of prompts: +// https://github.com/enquirer/enquirer/tree/master/examples +// +module.exports = [ + { + type: 'input', + name: 'message', + message: "What's your message?" + } +] diff --git a/_templates/init/repo/new-repo.ejs.t b/_templates/init/repo/new-repo.ejs.t new file mode 100644 index 0000000..08e7cff --- /dev/null +++ b/_templates/init/repo/new-repo.ejs.t @@ -0,0 +1,4 @@ +--- +setup: <%= name %> +force: true # this is because mostly, people init into existing folders is safe +--- From d63c1d13bdbe23c0582c6436d3ca68591abfdb53 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Thu, 8 Dec 2022 11:15:24 -0500 Subject: [PATCH 2/7] part 1 --- 2022/08/input | 99 +++++++++++++++++++++++++++++++++++++++++++++++ 2022/08/part1.js | 87 +++++++++++++++++++++++++++++++++++++++++ 2022/08/part2.js | 4 ++ 2022/08/puzzle.md | 41 ++++++++++++++++++++ 2022/08/sample | 5 +++ 2022/08/test.js | 21 ++++++++++ 6 files changed, 257 insertions(+) create mode 100644 2022/08/input create mode 100644 2022/08/part1.js create mode 100644 2022/08/part2.js create mode 100644 2022/08/puzzle.md create mode 100644 2022/08/sample create mode 100644 2022/08/test.js 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"); + }); +}); From 78c820dc44978fffb26b70be57159629c0c4fcb1 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Thu, 8 Dec 2022 11:18:50 -0500 Subject: [PATCH 3/7] prettier --- 2022/08/part1.js | 120 +++++++++++++++++++++++------------------------ 1 file changed, 60 insertions(+), 60 deletions(-) diff --git a/2022/08/part1.js b/2022/08/part1.js index 5d9cffd..3c7377f 100644 --- a/2022/08/part1.js +++ b/2022/08/part1.js @@ -1,6 +1,6 @@ import * as R from "remeda"; import { readFile } from "../05/part1.js"; -import Victor from '@a-robu/victor'; +import Victor from "@a-robu/victor"; const V = (...args) => new Victor(...args); @@ -8,80 +8,80 @@ const readInput = (...args) => readFile(...args) .split("\n") .filter((x) => x) - .map( l => l.split('').map( x => parseInt(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) - ); + 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), - }) ); + // 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), - }) ) - ); + // 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), - }) ) - ); + 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; + 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]; } - while( positions.length ) { - const pos = positions.shift(); - pos.position.add( pos.inc ); + positions.push(pos); + } - 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; + 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; +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) + printMap, + generateVisibilityGrid, + printMap, + R.flatten, + R.countBy(R.identity) ); From 258a8f97957e02d0c6ff8acb7eda64b11aa01af4 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Thu, 8 Dec 2022 11:19:09 -0500 Subject: [PATCH 4/7] remove maps --- 2022/08/part1.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/2022/08/part1.js b/2022/08/part1.js index 3c7377f..b1b0c13 100644 --- a/2022/08/part1.js +++ b/2022/08/part1.js @@ -79,9 +79,7 @@ function printMap(forest) { } export default R.createPipe( - printMap, generateVisibilityGrid, - printMap, R.flatten, R.countBy(R.identity) ); From 7fe01fdb576c3d41bf8f727d481f256779f10b20 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Thu, 8 Dec 2022 16:19:44 -0500 Subject: [PATCH 5/7] prettier --- 2022/08/part1.js | 16 ++++++++-------- 2022/08/part2.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++- 2022/08/puzzle.md | 27 +++++++++++++------------- 2022/08/test.js | 18 +++++++++++------- 4 files changed, 79 insertions(+), 30 deletions(-) diff --git a/2022/08/part1.js b/2022/08/part1.js index b1b0c13..c8f6654 100644 --- a/2022/08/part1.js +++ b/2022/08/part1.js @@ -10,6 +10,12 @@ const readInput = (...args) => .filter((x) => x) .map((l) => l.split("").map((x) => parseInt(x))); +export const outOfBound = (size) => (pos) => { + if (Math.min(pos.x, pos.y) < 0) return true; + if (Math.max(pos.x, pos.y) >= size) return true; + return false; +}; + export function generateVisibilityGrid(forest) { const size = forest.length; const visibility = Array(size) @@ -47,17 +53,11 @@ export function generateVisibilityGrid(forest) { })) ); - 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 (outOfBound(size)(pos.position)) continue; if (forest[pos.position.x][pos.position.y] > pos.height) { visibility[pos.position.x][pos.position.y] = true; @@ -73,7 +73,7 @@ export function generateVisibilityGrid(forest) { export const puzzleInput = readInput(import.meta.url, "input"); export const sample = readInput(import.meta.url, "sample"); -function printMap(forest) { +export function printMap(forest) { forest.forEach((line) => console.log(line.join(" "))); return forest; } diff --git a/2022/08/part2.js b/2022/08/part2.js index 10fc179..53b599e 100644 --- a/2022/08/part2.js +++ b/2022/08/part2.js @@ -1,4 +1,50 @@ import * as R from "remeda"; +import memo from "memoizerific"; +import Victor from "@a-robu/victor"; +import { printMap, outOfBound } from "./part1"; -export default () => {}; +const V = (...args) => new Victor(...args); + +function visibility(forest, x, y, dx, dy) { + const pos = V(x, y); + const inc = V(dx, dy); + + let vis = 0; + + while (true) { + pos.add(inc); + if (outOfBound(forest.length)(pos)) return vis; + + if (forest[x][y] <= forest[pos.x][pos.y]) return 1 + vis; + vis++; + } +} + +const visibilityAround = (forest) => (x, y) => { + return [ + [0, 1], + [0, -1], + [1, 0], + [-1, 0], + ] + .map((d) => visibility(forest, x, y, ...d)) + .reduce((a, b) => a * b); +}; + +export default R.createPipe( + (forest) => { + const viz = Array(forest.length) + .fill(null) + .map(() => Array(forest.length).fill(0)); + + for (const x of R.range(0, forest.length)) { + for (const y of R.range(0, forest.length)) { + viz[x][y] = visibilityAround(forest)(x, y); + } + } + return viz; + }, + R.flatten, + R.maxBy(R.identity) +); diff --git a/2022/08/puzzle.md b/2022/08/puzzle.md index 45a66ba..3cbf273 100644 --- a/2022/08/puzzle.md +++ b/2022/08/puzzle.md @@ -1,9 +1,8 @@ -\--- Day 8: Treetop Tree House --- ----------- +## \--- 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. +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: @@ -18,24 +17,24 @@ The Elves have already launched a [quadcopter](https://en.wikipedia.org/wiki/Qua 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. +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: +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. +- 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?* +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 +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]()] this puzzle. diff --git a/2022/08/test.js b/2022/08/test.js index f1b2b98..37e84b4 100644 --- a/2022/08/test.js +++ b/2022/08/test.js @@ -1,21 +1,25 @@ import { test, expect, describe } from "vitest"; import { expectSolution } from "../01/main.js"; -import part1, { sample, puzzleInput, generateVisibilityGrid } from "./part1.js"; +import part1, { sample, puzzleInput } from "./part1.js"; import part2 from "./part2.js"; describe("part 1", () => { - test( "sample", () => { - expect( part1(sample) ).toBe(21); - - }); + 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"); + test("sample", () => { + expect(part2(sample)).toBe(8); + }); + test("solution", () => { + const solution = part2(puzzleInput); + expect(solution).toBeGreaterThan(160); + expectSolution(solution).toEqual(496650); }); }); From 7c7f785b2c27e1a5beee20ff42d31b5c496c7725 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Thu, 8 Dec 2022 16:20:07 -0500 Subject: [PATCH 6/7] add dependency --- _templates/day/new/part1.ejs | 10 ++++++++++ package.json | 2 ++ 2 files changed, 12 insertions(+) diff --git a/_templates/day/new/part1.ejs b/_templates/day/new/part1.ejs index 97460f5..14531b0 100644 --- a/_templates/day/new/part1.ejs +++ b/_templates/day/new/part1.ejs @@ -3,4 +3,14 @@ to: 2022/<%= day %>/part1.js --- import * as R from "remeda"; +import { readFile } from "../05/part1.js"; + +const readInput = (...args) => + readFile(...args) + .split("\n") + .filter((x) => x); + +export const puzzleInput = readInput(import.meta.url, "input"); +export const sample = readInput(import.meta.url, "sample"); + export default = () => {}; diff --git a/package.json b/package.json index c1baac4..232dcb9 100644 --- a/package.json +++ b/package.json @@ -10,8 +10,10 @@ "author": "Yanick Champoux (http://techblog.babyl.ca/)", "license": "ISC", "dependencies": { + "@a-robu/victor": "^2.2.2", "debug": "^4.3.4", "fs-extra": "^11.1.0", + "memoizerific": "^1.11.3", "prettier": "^2.8.0", "remeda": "^1.3.0", "vitest": "^0.25.3" From c67248fb17aba8b4f832f6985b1a2513ed411f3e Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Thu, 8 Dec 2022 16:27:32 -0500 Subject: [PATCH 7/7] prettier --- 2022/08/part1.js | 12 ++++++++---- 2022/08/part2.js | 20 +++++--------------- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/2022/08/part1.js b/2022/08/part1.js index c8f6654..34dd036 100644 --- a/2022/08/part1.js +++ b/2022/08/part1.js @@ -73,10 +73,14 @@ export function generateVisibilityGrid(forest) { export const puzzleInput = readInput(import.meta.url, "input"); export const sample = readInput(import.meta.url, "sample"); -export function printMap(forest) { - forest.forEach((line) => console.log(line.join(" "))); - return forest; -} +export const passthru = (func) => (arg) => { + func(arg); + return arg; +}; + +export const printMap = passthru((forest) => + forest.forEach((line) => console.log(line.join(" "))) +); export default R.createPipe( generateVisibilityGrid, diff --git a/2022/08/part2.js b/2022/08/part2.js index 53b599e..061879c 100644 --- a/2022/08/part2.js +++ b/2022/08/part2.js @@ -1,8 +1,7 @@ import * as R from "remeda"; -import memo from "memoizerific"; import Victor from "@a-robu/victor"; -import { printMap, outOfBound } from "./part1"; +import { outOfBound } from "./part1"; const V = (...args) => new Victor(...args); @@ -21,8 +20,8 @@ function visibility(forest, x, y, dx, dy) { } } -const visibilityAround = (forest) => (x, y) => { - return [ +const visibilityAround = (forest) => (x, y) => + [ [0, 1], [0, -1], [1, 0], @@ -30,20 +29,11 @@ const visibilityAround = (forest) => (x, y) => { ] .map((d) => visibility(forest, x, y, ...d)) .reduce((a, b) => a * b); -}; export default R.createPipe( (forest) => { - const viz = Array(forest.length) - .fill(null) - .map(() => Array(forest.length).fill(0)); - - for (const x of R.range(0, forest.length)) { - for (const y of R.range(0, forest.length)) { - viz[x][y] = visibilityAround(forest)(x, y); - } - } - return viz; + const va = visibilityAround(forest); + return forest.map((l, x) => l.map((_, y) => va(x, y))); }, R.flatten, R.maxBy(R.identity)