day 8 in common lisp

day-9-common-lisp
cow 2022-12-26 02:26:20 -05:00
parent 85f6e5820a
commit 8490f3a911
No known key found for this signature in database
5 changed files with 240 additions and 0 deletions

View File

@ -0,0 +1 @@
../../input/day-8-example.txt

1
common-lisp/day-8/input.txt Symbolic link
View File

@ -0,0 +1 @@
../../input/day-8.txt

134
common-lisp/day-8/solution.lisp Executable file
View File

@ -0,0 +1,134 @@
#!/usr/bin/env -S sbcl --script
;; Copyright (C) 2022
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
(require 'asdf)
(require 'uiop)
(require 'str)
(require 'iterate)
(defpackage :advent-of-code-day-8
(:use :cl :iterate))
(in-package :advent-of-code-day-8)
(defclass tree ()
((x
:initarg :x
:reader x)
(y
:initarg :y
:reader y)))
(defmacro make-tree (x y)
`(make-instance 'tree :x ,x :y ,y))
(defun scan-right? (trees position)
(iter (with width = (- (array-dimension trees 1) 1))
(with tree-height = (aref trees (y position) (x position)))
(for i from (+ (x position) 1) to width)
(for tree = (aref trees (y position) i))
(counting t into view-count)
(unless (< tree tree-height)
(return (cons :not-visible view-count)))
(finally (return (cons :visible view-count)))))
(defun scan-left? (trees position)
(iter (with tree-height = (aref trees (y position) (x position)))
(for i from (- (x position) 1) downto 0)
(for tree = (aref trees (y position) i))
(counting t into view-count)
(unless (< tree tree-height)
(return (cons :not-visible view-count)))
(finally (return (cons :visible view-count)))))
(defun scan-down? (trees position)
(iter (with height = (- (array-dimension trees 0) 1))
(with tree-height = (aref trees (y position) (x position)))
(for i from (+ (y position) 1) to height)
(for tree = (aref trees i (x position)))
(counting t into view-count)
(unless (< tree tree-height)
(return (cons :not-visible view-count)))
(finally (return (cons :visible view-count)))))
(defun scan-up? (trees position)
(iter (with tree-height = (aref trees (y position) (x position)))
(for i from (- (y position) 1) downto 0)
(for tree = (aref trees i (x position)))
(counting t into view-count)
(unless (< tree tree-height)
(return (cons :not-visible view-count)))
(finally (return (cons :visible view-count)))))
(defun tree-visible? (trees position)
(or (equal (car (scan-right? trees position)) :visible)
(equal (car (scan-left? trees position)) :visible)
(equal (car (scan-down? trees position)) :visible)
(equal (car (scan-up? trees position)) :visible)))
(defun scenic-score (trees tree)
(* (cdr (scan-right? trees tree))
(cdr (scan-left? trees tree))
(cdr (scan-down? trees tree))
(cdr (scan-up? trees tree))))
(defun trees-from-file-lines (lines)
(let* ((split-lines-into-digits (mapcar (lambda (line)
(str:split "" line :omit-nulls t))
lines))
(numbers (mapcar (lambda (row)
(mapcar 'parse-integer row))
split-lines-into-digits))
(x (length (nth 0 numbers)))
(y (length numbers)))
(make-array (list x y) :initial-contents numbers)))
(defun solution-part-1 (trees)
(iter (with width = (- (array-dimension trees 1) 1))
(with height = (- (array-dimension trees 0) 1))
(for y from 1 to (- height 1))
(sum (iter (for x from 1 to (- width 1))
(for tree = (make-instance 'tree :x x :y y))
(counting (tree-visible? trees tree)))
into visible-interior-trees)
(finally (return (+ visible-interior-trees (* width 2) (* height 2))))))
(defun solution-part-2 (trees)
(iter (with width = (- (array-dimension trees 1) 1))
(with height = (- (array-dimension trees 0) 1))
(for y from 1 to (- height 1))
(appending (iter (for x from 1 to (- width 1))
(for tree = (make-instance 'tree :x x :y y))
(collect (scenic-score trees tree)))
into scores)
(finally (return (let* ((sorted-scores (sort scores '>)))
(car sorted-scores))))))
(let ((trees (trees-from-file-lines (uiop:read-file-lines "example.txt"))))
(assert (= 21 (solution-part-1 trees))))
(let ((trees (trees-from-file-lines (uiop:read-file-lines "input.txt"))))
(assert (= 1736 (solution-part-1 trees))))
(let ((trees (trees-from-file-lines (uiop:read-file-lines "example.txt"))))
(assert (= 8 (solution-part-2 trees))))
(let ((trees (trees-from-file-lines (uiop:read-file-lines "input.txt"))))
(assert (= 268800 (solution-part-2 trees))))
;; Local Variables:
;; mode: lisp
;; End:

5
input/day-8-example.txt Normal file
View File

@ -0,0 +1,5 @@
30373
25512
65332
33549
35390

99
input/day-8.txt Normal file
View File

@ -0,0 +1,99 @@
220311003030020403303010231542401043011111536600504555130543502251500155511123400111142111343321103
112300011121001442140224403450400301003445152052111053406435260452010444113124523432423300221011201
030210134102431134241312344101144221020600111540536441502615542526153154322105541510214043131203100
030101204414334205055102335112246411105640305204415413550445035636165511302522134021011341010042300
231333121332014225332001232453144631040416204620146621036153421503540461150044430323330010312004330
313004122100223501210053543222333364566250521251132261434210544505630145430310211300234000331011113
012412044210025224134545004132405043251661531641253433253260055031356132633542211515154121433313333
223403114014255555503000342035532635342046556443773774444450206405043632662330550032142542032211310
100242031223054535004460205122361434151616645373551326734736364332154140655340554322211502021301422
431401423325414423503035443254641056037467637176755423251235366605645121661130561335005033241143132
303223123304431041420426563622516421776753123353756124574661615471604435005422154332445143042340424
320243144004444013251031465604646513135642442262575441115642435263165223031065313000204222125412104
122002131150055031454645120263773775533157351513157276612761513114315414102323430550452520223024322
442400504041420321512465200531646152466567424527451215215141725631467534743125652340253015312012211
304232133233542213221531357775467547373337162134833471236477571463472456124542445620504144015151422
312145240500031634266120023623337132742563423256554635742447513652312413114404640563132134245504334
102241141135325653546102133652773114325462366462547647422525733353647767521436321451530122543005443
144343141455144014314467421341365346542347332643357578865825554673752135624114640556506602451350232
441243044020543461441135565544322184758342838378753352744524773578231564762432525625353423431225210
304443143022414225106232772261446675445426335438555646846447573843847251274211636600312256555303142
303004450304246266227631711622523887473687878273335526667757385748853425345333774363212605153132341
231541553335163641412412276128558625882683583258732273355654747663247263774611741645413153462012134
512540106462151644671623764557535533877366255767846535753758275354368484766426475560164140560220332
200325003256216624167373756725753386556464596984476493863565874285358286562141147544135646330340152
521423426045110455366477454336662744636375686369848368855656338766535868252365342245652406663635444
325143246625651366452465566472785577755464486833753685334599777574426367357427766647745534646000541
142035142614357527172664626867286864497656647539574736765863985878668537672332143435672354342402200
402115663021231111473735247274463488788389969399794455838777463866665252277554546567456140205305335
414425135566351623353185335555739586888579945549558363373688586773657582484573717756461526021030021
153405106165521616675638523724787758457498763486869486593486777373353973322655234123422714165323213
002454525531472631474628833245565848566559859575785679756548368396867676276727237652414610661211503
302304255114244361477257533433435655836968776565755564555784764387349895587874476525477240213363411
000553414531611151674483872897383653598648797654779776549854495335546648627287687525577125431314005
204232400564226421567584754598534645844885494584598457845957476555468784847282275342654247302314615
045115061065473766727826528379367838467987998655957448955985448979477345343826276272532415200621434
550120546646177774768543785974894546878959965974654898945999699595899795644635465425136225335645046
556302536577536445475544794655563486856474558879957857474886955958533847988473852683676313440303640
100626413557777634338424498589945894456646866588958979857649598485767686537762876484212352433166343
242525032474643788477744736865977694759486568659756698755867877857994399566938462833351471633304353
332244425455234733677627699765738456769446855877656577675969585695578538879868362272556755663030020
046343213215513556668558775536566645456859558759858557585895694767986879363445872877411424542656650
515331306274442182557527878856554588685785787696597767559877799855448956494486733433633675325030100
342114227666721672883353894985987947695867576665575699796996867698487579875567433487831322744642364
540065045713275583625867884979954845798686769595688655769979798559495959557938387255665736254225032
106205316767657824665283537587768649899978685567877997998657967876867864959943625835831345425516321
110141463114412578736753573994676759986999796769898976765559575889778477944885863665286231467300203
644032125671346284562455445368556987856977988668777677796898765568994566565589365633833145272453334
460545273256573436862249486376479568675877557889666967997785859858746988837693345878347636472512302
342136331345115866465777476676554569688888678887879976969695978894988799695783654776835454112416212
434225025543222743684868583694765776998689789867696786677896555888665675748388754436555342746720235
636263555721237243768239677534647848856975986889687766699966676679495588667869627586668724667334426
633103343364326663552898385566948669767567777777999689999758789694789754646685445856262555511451232
152145115364274535448634885836688875775568656777977988887669866875889585488434323387357647235164321
606561462261137874756796373755847876579796979986676786689789579968644748868566647685775135531406152
410250253465337574644637536938599968796887596787787967977585565898468798949444464466542361454714064
030544155175617533452288758984968478476689989798699966779765685659747585749847462537423313543216612
611401537472217227872537567338475586486768666798867797877569576955557776366336886758327422461410414
415330416246627536224487793957998488548995797977587956859855675947744645474575444827645213327445433
463326543611137332345267589896759656899676956979677665577987898996486683785757677647531261533654450
551354362237277258784359489837398969895865988995889897577896657978979937354834372675456336454226041
211262361766132756683339396694359447645468869756878976758868954645658869839363758464364262147012651
301064110125426286655552939776685894477488897677856895895975986758997464544986666756833565662202556
422031301621157472683222989636778884967989975889756595697987999874964343866593357583726217334026440
142162412763327353674277446679689849644977997798579559589469468868677755456373377468464732726265153
526160541173717212627575863388387774875486479777676797468597655477966799599657444765465675456652011
251165660644352242558682573366746386656597499567586485569867549988967847556528856227561237366411636
244163004465433222484736846657978585756446659594884487687847788788366864385335623365445517314254620
434110322132544652235686444994547888848564645978698549679559986658353774636353287547513235651412653
212416233634562415626658545256839386835478785947498686995967444949889456536728324373112557601534261
435434454616326145423432683478946353936357589848746499574966735497735646648468758347532775410534343
024550535014447554553372238424959848484777465879954794488985494338565352746235455264773556034542015
313351426410311176275387482682544444446955766744958787746868936569764356824257673616131462100422002
312201400320061134162664385482788738548983499889989775337437639475985783638433415171251751440654113
154531401000612474551545724587863386473895446934738887837768466589654784428788774456346346103620323
541513516605546126517714738734486588386863574753885799977634778859444263746451367234673456125542350
324542015440362755526154738573554235444678994388498686678788494494784867244816715767735422644331340
521510135354356155436547615523735566855649894646878476694473459843274232545441522235602025363620520
532345411630014517561247224352264584734787368663753568537436638536834367641132615221036214614105230
130500010532446521724141343744446577753367224554979338988268627277568778757764223635125423346352240
005211030240423160452722537744523766476268564266686267267868488672377778241537344633406316523222053
401031212322536165661332777724644222584548667585266553282236873353822564255646212515321264655335000
132050350131063034245353542636634248567866466782822733368854442875738634517754337605264326323310552
242055145053551300641665774617717158484378377847863722225235683876346726415335576520434133423315140
420212431450462115612104535173257643832557837826233786667255842463474555124456032603252542441550451
101143034532526023313401212777267175116483624466566678638424736755742575654354414153232603110043024
004112000553532226456602061357464774411621253576768283372377472122417645272222265456426540303044330
000214140151415353051123241641452172357611716354273226363355267157417641644250131530261244033002212
314433040204132402111422555355165354124166712274546472647636332665351566642543566162041224054110430
120030403353224333631161514616621361515441672717213326172637617441126174245110550454053211154510143
044113301243535050630534325651052732333667672334663256444256677252744553434143625412515552022242411
212301204302220222103310242332364247143553146724275737653536435375450216300335034255232215432400044
100214223250210102122355012452525521151534667317437271422273165610411234111153420355224541411214011
102220004040333521144201535252045402043667711456465135367727200341240201205445503433001025222412401
301132133014323404415304656535542542535034524663742746216234614446555616434144505534244502220323201
001313232330211424313343111141503353422354166003563125012466532322024064101325131033325024441212011
333243322424341532020111415135040623435040550552365600044314634602153465313205551350413104214430410
200313334322431220433010514521302362614466565330411136330544661330564424123435134010442411101434301
213232010014424311402320410540520055056234623535443416222222052412461442122102342224042420233001132
211331340224211003504115003251531130611161462410255033600306643200341042155441434302113114300301010