From b2fd306d964d4386e1e8dae54637fd93a0e35071 Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Thu, 8 Dec 2022 21:11:50 -0500 Subject: [PATCH] day8: Add part 1, unoptimized --- 2022/08/input | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2022/08/sample | 5 +++++ 2022/08/sol.scm | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 166 insertions(+) create mode 100644 2022/08/input create mode 100644 2022/08/sample create mode 100755 2022/08/sol.scm diff --git a/2022/08/input b/2022/08/input new file mode 100644 index 0000000..67c19e5 --- /dev/null +++ b/2022/08/input @@ -0,0 +1,99 @@ +211212222022130201331244204031330314133103224443222153333314030424430144322333133111111001110111100 +011010002312022022213333124244332200012535311241312425233452234232332434002404314010213010300011020 +000122211022332331322030422114134234533543153332141433442315454242340242334121020031011333101002002 +212001231120020114443023323004341314244553111423553551151313122524454330124123430302021112131311210 +010023233323302131223113103422131413124511225231551512331151422122222242244011341133031332000123002 +010122332320232121222313343422344153542155353422554151444431132214155255442032200104444102212213121 +220311123023010212113012214434253241131125125144553514355135411412253214211034342023023233100210312 +012021233322101322014410134244343233151425141125355346552431312541441525552551132210132341231131213 +212022110210121130141215541523415532154426463253225335424345244125452453353521224302113344031023330 +001331301002442002234333243154523422345544442555654454522262236353114253113323520332302343300332303 +022221002233442142312112451142555566553525565662234553562446526522354241453541211024234444203213103 +121222311004442143452433443451433246356323333462224663645332426432564453315351524511430324331330313 +020033121433043013523451315525355225662246464433535443555244533263653654532332123344232243011131122 +331231332041133215352242534163532264356442645556436224536332662235525664241551351544514242443122020 +211224100401201134234315434244552434552354262423454635464345236222546422664131225224254043442021032 +231012303303415245123421452352524655622263645375354655347574553332462346426612522542555340213441312 +211043401112034321445551526323233226554374763356556636777355756535336234634241534141122200101402030 +112341023410451542221225654622622255353544565345645766565677556375252446634536424143355510132222200 +321013312335143545355422445454636567463677663737736766333543334747425233256224451325314525244413200 +134134112125343512236525526335246366357455663434636554535666675345375546365323352441311155004121134 +134103302234334422434623253335635454465444433673744644777565643336547576243523623215313211444123100 +121214332422533433642563564345446775634333677454565663433553464453353655453565463223325554140013211 +140340201515224355236356553354564345656637544677644666867363673577375377725566454643343552314401102 +242234123324245145352363463474556736655588548485584448876875743457645676744566665635145243345232034 +240002433255552223455653336765744666634687546687484788677745778643457356557523233245233345322203230 +203033145215334435435262347664466473474457867646854578857546674775433665576464664524234245315323221 +220240145522153253653343354334676665555545854865676474576864645785537443574743555546543552211424323 +203303223355443662564323675465776857766847558445856455464888855484856565473734263625333553514131204 +141343234343462623453376444565367475584744446846868687745447856768847544336577422435266221431351302 +011105311221546266246563346475574856785486544597695996967678786457746835375453766563243343232132222 +342153442535526543536765573565848468586567799675857888667865686547568643564577744364252242513532434 +303412514235522642433473653567558668878798557765777686957585688674776587434464373226246544543135343 +134313521546426353375756547586484656765785675686695796677998878746476587743677774454422656531211434 +330442321112644425464657363687688585599859586878658555897556995948874866676663357754662526513313351 +031152255535654264775364478686688678756896855798669886769655865898565686588637345474344563445412320 +033412231333255266746776747686577558998969786586588679955768678756678668685355457353433435511243215 +233232325556234436547474646776556656557756668998669967658757689688987578684577474336565246255532551 +232114313222322224464545378545768899675966969678976998999568556595885874447557776576555222565311333 +153554334255253356567537674646656569995678676788797887778695969889798866846683333743344642351554423 +345414252243643374353655675557769798967656689687786868687886675866575467874583577644342363531311344 +452213536622456254743557585655789799758988877898878787688797798767876666484675654443325664563135251 +151114452544554546436654876678799866898876787668968979699879995587987774468485375644536533255541211 +211514523322654673674356655476766668559777787767896697698867686989688685867584637757652452536532521 +325431245645266644556638674587856588976776679887879899868878797869579957766466547664576666553424155 +351353232222444754473488547447588977799698788797978898877869989965559967448746574666366225422555234 +413323553464546456467374574775588578597999986799989888988976899856686587855784743346666424425221545 +231444363436566774435677574767669997976869899777989877888676989766766555785667844345663255424355441 +131332342664544366547384447849957999767988788889999977889998688686776576587687545564772363266441344 +344323153255254475756447668487668876766786779788778999997667996768669658886648456344746335633653214 +432125224655454763335585885578768897889868679897777999877987996687598957855747536767467246264213342 +251123242522227743643744785665768788969768778798979798899976766779955656454747664477375562423352144 +335215134645536564436664555587969577787979887889798779897877686768869888764467653676646454662351122 +455541145534236474533477845547655956799686678788787897899877888979567875855484663636455423534225343 +443412355542265645777686678647889565778786879979978999977689898868965569867655855463733536555332123 +554333534644243647547485474889997578686899879987778777979796897956885659856478875353562443646414133 +523521346442456477365347646745578595568686668879987988977879897858556977787654645645753546466532354 +415551454256456643353367445455568986897777877789988897768879899869965985464454846544732365343211244 +045253325223423633465535668844665696698867976878987997697669698795567764745785435374775534356323555 +324424513464646646773334458686678768868668789699687898687769799596757997646887333456353525562532334 +333314122436424634656654458578885567975698998866676766687677986678988577887848647435423555654245141 +441323436226324474354554865578679696675896767799799699977678687576559874657778736635335633336333245 +325541255634234335777446557648487588986657787766698699877877978598595778688447674476666264664525524 +334224432254623447467743486544648996678656987989788868878788589778785746455873547654626326245514114 +413352531625243455733366467566645868987999997776889888868757777796558764784776476377665635553522333 +425235531356553325567457674875556778688685899689977798685876768589645658567837344646524442654525432 +103455532456342526657354677664785588679779979577568567955568596777454678585774353354436633221153331 +422433153464256665575466474547467484759655656769697755696555696797648476876754465655362463541153510 +200221512352422423435457444587878556556776655658757679865666957677874646877574343466422645122433143 +233312332322644356544567546775446655776887566656897679779679567464757846575634746754632245245312322 +440413534453435243635457637637844865678595957869877877556795746685484886655763473646525455242423424 +133015323213554533656353736775587775585487676857757689778864864846456683634546376645266241223134331 +341325554355154464532756673544377888566757758978576696758777847844684656374464563435536313553154410 +123231515424162465563463554775765668488574776875475754584748666766764547366777353655635341211544214 +411120434335113656445353437475335766655568765658746576454466786655883677746675446244525421121551010 +402433144311354446525322467654675484475575676777445476666764865885847756657433654652532114452353133 +432424042535244564355554365363676576876745584585766548666774675753564565547462524462425114452201003 +102313425251124435654356367537734654638566846546448757455657555476436573637522622235212353325544024 +220311435524234432323565463344475534664484764648754486454485567435377667774553343222531144155033034 +231020004114553111534454422257546737666353776748664584464344633646464475423245232221444424255211432 +032121221243151355266464525344734574375363364375343377346676563457354333464322642455152553422410413 +224424204312344155546224253423664743337765335743365463543544433363437353323264646443514252343204010 +001321143122112341544253536644545554446436763477746674374776466344577623333354536412412241240000301 +012121423241523355214265352225525345574443757474534637556673367573322622226565351432424315423431120 +002001243100554231422332533535466634675453664437575544663735566344653636536633315425145230310333331 +111131341421342553234223552634336262456343575734674633673656737332422423254544335452112104141212220 +312222012123233525325553364332226636325523744473366567533732646636556526353413122533352224001243232 +222104333230343332423213113445433536266452535452556675623325555336233552464241241531142132324111100 +120033123241410113453233151144554262266263655622523332323624264526223446642241422415122401000243333 +300110321321220332412453221523552552665236344464424652635635223664335443125224432335044114323031213 +022320323221440340524544434152545623533526235366642566655434622263663532553353311521231430123330113 +002023331000112411134112432323353142332443352524442462645622663225223434341133154204330110021211033 +001030002322324022304135433132453151564563345632626222566526534531324522534413523110111000231032001 +021300020130201201401332235525125513213453326526662453442553331541555225115331120431314320302111303 +021321020201323321211302324413125143432142143524266333524534451124153311112131123343411412020232121 +202310321020334220421002031152143124243343112321122253451541422112235155133111203114013232211133211 +210131010322112330120423222122351343151544412545222452312115521551124142440421412131234310031213311 +200022120033030133130312000240311254212234133155524431352242335344153414002444032121311102101011000 +022201303110101303411200001233415242353252421341231234412423235311314133130301320201102330230131001 +200002110210232130434243031312421313244134152512112542334551242213212344113244124232001302221200010 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/sol.scm b/2022/08/sol.scm new file mode 100755 index 0000000..c74a77f --- /dev/null +++ b/2022/08/sol.scm @@ -0,0 +1,62 @@ +#!/usr/local/bin/chicken-csi -ss +(import (chicken string) + (chicken io)) + +(define (char->digit c) + (- (char->integer c) 48)) + +(define (lines->grid lines) + (let* ((vecs (map (lambda (l) (list->vector (map char->digit l))) + (map string->list lines)))) + (list->vector vecs))) + +(define (grid-get grid r c) + (vector-ref (vector-ref grid r) c)) + +(define (neg? x) (< x 0)) + +(define (max-in-dir? grid r c dr dc) + (let ((tree (grid-get grid r c)) + (n (vector-length grid))) + (let loop ((r r) (c c)) + (let ((nr (+ r dr)) (nc (+ c dc))) + (if (or (neg? nr) (neg? nc) + (>= nr n) (>= nc n)) + #t ; made it off the map + (and (> tree (grid-get grid nr nc)) + (loop nr nc))))))) + +(define (is-visible? grid r c) + ; (print "visiting " r " " c) + (let ((n (sub1 (vector-length grid)))) + (if (or (equal? r 0) (equal? c 0) + (equal? r n) (equal? c n)) + #t + (or (max-in-dir? grid r c 0 1) + (max-in-dir? grid r c 1 0) + (max-in-dir? grid r c -1 0) + (max-in-dir? grid r c 0 -1))))) + +(define (range n) + (if (equal? n 0) + '(0) + (append (range (sub1 n)) (list n)))) + +(define (main args) + (let* ((lines (read-lines)) + (grid (lines->grid lines)) + (n (sub1 (length lines)))) + (let ((visible (map + (lambda (r) + (map (lambda (c) (if (is-visible? grid r c) 1 0)) + (range n))) + (range n)))) + ; (for-each print visible) + (print (apply + (map (lambda (l) (apply + l)) visible)))))) + +;; for repl exploration +(define gd '#(#(3 0 3 7 3) + #(2 5 5 1 2) + #(6 5 3 3 2) + #(3 3 5 4 9) + #(3 5 3 9 0))) -- libgit2 1.8.1