Mercurial > lbo > hg > aoc22
changeset 12:ecf80e49d919
Day 08
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sat, 10 Dec 2022 21:41:58 +0100 |
parents | e53d5f34cf17 |
children | dfed6e6ea69b |
files | 08/08.jl 08/input.txt |
diffstat | 2 files changed, 189 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/08/08.jl Sat Dec 10 21:41:58 2022 +0100 @@ -0,0 +1,90 @@ + +const input = "08/input.txt" + +const short = "30373 +25512 +65332 +33549 +35390"; + +function parse_trees(lines::Vector{S})::Matrix{Int} where {S <: AbstractString} + m = zeros(length(lines), length(lines[1])); + for (i, line) in enumerate(lines) + for (j, c) in enumerate(line) + m[i,j] = parse(Int, c); + end + end + m +end + +function visibility(row_or_col::AbstractVector{Int})::Vector{Bool} + v = zeros(Bool, length(row_or_col)); + highest = row_or_col[begin]; + v[begin] = true; + for i in eachindex(row_or_col) + if row_or_col[i] > highest + v[i] = true; + highest = row_or_col[i]; + end + end + v +end + +function is_visible(m::Matrix{Int})::Matrix{Bool} + rows, cols = size(m); + v = zeros(Bool, size(m)); + + # Row-wise + for row in 1:rows + v_forward = @views visibility(m[row,:]); + v_backward = @views visibility(m[row,end:-1:begin]); + reverse!(v_backward); + v[row,:] .= v_forward .| v_backward; + end + # Col-wise + for col in 1:cols + v_down = @views visibility(m[:,col]); + v_up = @views visibility(m[end:-1:begin,col]); + reverse!(v_up); + v[:,col] .|= v_down .| v_up; + end + + v +end + +function find_scenic_distance(v::AbstractVector{Int})::Int + ref = v[1]; + for i = 2:length(v) + if v[i] >= ref + return i-1; + end + end + return length(v)-1; +end + +function scenic_score(m::Matrix{Int}, row::Int, col::Int)::Int + v = m[row, col]; + + l = find_scenic_distance(@view m[row, col:-1:begin]); + r = find_scenic_distance(@view m[row, col:end]); + u = find_scenic_distance(@view m[row:-1:begin, col]); + d = find_scenic_distance(@view m[row:end, col]); + l*r*u*d +end + +function scenic_scores(m::Matrix{Int})::Matrix{Int} + s = zeros(Int, size(m)); + for ix in CartesianIndices(size(m)) + s[ix] = scenic_score(m, Tuple(ix)...); + end + s +end + +println("Real challenge, part 1:"); +inp_lines = readlines(input); +trees = parse_trees(inp_lines); +println(sum(is_visible(trees))); + +println("Part two:"); +ss = scenic_scores(trees); +println("max scenic score: ", maximum(ss));
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/08/input.txt Sat Dec 10 21:41:58 2022 +0100 @@ -0,0 +1,99 @@ +230302102312303431324124303141114045300001501242156105363242032123135210143324224140132332112100221 +322200120140130323143433402101444233512140224062422011111403231052521053542252401012232443102111032 +220300201042003221454531141345330151415344324656555610153343240503352304331551230452141112120303320 +131301034134041313145104311104431531104002136262204021050462633364005504230105333250310334430012231 +021000431324243205221240551231546346005546113055526350502461501303260444304552521404504240012202100 +130032042140100501120530551053341401664420533042546256013026351632616214431331333233341222140143402 +102213021202104044154525451632332223320015014365014110000262152226451306654102512123512023131343201 +331321033401405043214153225511265312435604105124767542760400104362304616510410004451235001241134031 +234010022341252215202325124644011146300614531262461555623242615510132104506502155353245030411321214 +304102101231325220244422554336626240225366453655233535214545356712401516453566460554521045401410034 +124222310241022025221634021203456261377536264451547554432421672616632511164231351545110100421124041 +231432431351552412035636562534314116212363155763515352233177566413166344310511112324524355103343213 +103031123324141030040550165506212465546735652123674755314542271633361443500442335114204031255241224 +320113443131100311503426244335257671411253247761166615472155473411374661621210160254602143000503002 +003031503150544521324165216521624371212157515435232724117746343274255223574322040541222535240125320 +431234101124435354234422257673575575175341283683836588272273446224536761435621240216142511140302323 +123130200251405600130545412766211145442246588283778878426386645127335561311156623533260243441410122 +432402514341626355125037245134531317588423262388855625822687585244364351651321623121561560010312113 +312341305215255100304471152277172523637784345263433358277226635245516164123212336452605424200110430 +001434330142034560665237211325533756327872855833882354736845633764265764254723135136216531341302425 +345110412256633232261137637156368637757634446735753883783375577533786454271246611655350133245255344 +130515311233246602623742771612278266785844368233445235428485736334867477631346361563230603123034134 +330040046512436325746335744443684727248746257893478978476233834562664674872164576161101306065422041 +222154026412500467311716243862854643768585738339383839693837627262625554423155651645532206206134414 +542550455123342216235327358832483365754856794969568679863575688486863754828773355515541016335040545 +404234533623052522751165344644483278684968464647937735374953694986883822877673474565215510426220344 +314254353400344713147333655472357537587356967673598874457668767889935746887521663427411521635661241 +250301522140303127537224263455464893375697787574446387685355687386368545878447447117247625662212301 +531245634640354662654558667774728435789865389774334746464789766764944468568554351264634206163162541 +254210565205047575772722425336239545866857335764867899689375964484886366575237326255114412442436305 +254440240052645344117878758858477694934868449869846797495773435673485582766622343237654674230614650 +302350056253421457757382352837655457348549979584868566587675763536549455773244366226575120110432202 +524251410101643732555863746673768898357598988876769465556757759865678788677845447537546514520246465 +412424366507365464723435562846447777556788564867748886999988659539789863738827284322243654235650222 +302260630341726247764652638469957786688964699758599945666799457875988357894464748565724572166261234 +215150213244237121847528247786444449998976749466844877974565875546766745893426378444445742710223525 +424160233533557213425484474439489658786978977985856797555685676569553489768586885887425626363046446 +153322066741372433344452464455559968984567799888687677756697744545783483874576867684647332571001005 +145420124453223247785888737894569596698676567955697655669665679864796955354676474465373675660155614 +033506450161333145373824444348436889577445897969987595896979686466678689467597644746532527513131131 +225012522655144432763537973395798658496758867787886779885975689947696977368784268458336146373115143 +216342537614133655675625959443677654855666875698895757965779667678777945996749485327555574145424460 +166106344353256563624325443477776845578965867968867569996565856754694668864655545773383227234335414 +150522031675635233237668486584959896545865765899786958699768689485564988376498275457676373151605464 +412336103615667832572493349365459748765865988788966796676798558958658889874394465442751254315016610 +352326617457732528434753466738454444568858687988678798897989789856878957499594873552874152661460523 +062415331267566426437243673895878674897896558788889897878987955994767455333837536424584567627336053 +411350266636525852438469567638886987958577976996868967687585597767547999698594634538245463464543005 +052464462614766832722735867947598567878869668679877788879769878876988899643845375267672315514353000 +543545564473648376567477376798669977686956967668898889969875757785956549649485643782434576251353131 +634033674455736672452884665689654778778759896979877869977686568577555776433947564287374612267436364 +620541245674573376723897469464969646966776677799787978979857977798869788359344972235473531452736105 +602620347126374653254296764775476547797557857869988876878957998886476566934646536357683276776151421 +453313013613244386884696349435758974966876787969887886879899685595858499393337785854825724737215563 +633162016753461244685737989459664898569667899679799786798979988797998958559548885735471347141136630 +000101426742225562735894769979656676989556887676998666978968879767489645833678358457422477754521142 +620262606411662643533249376858748847498879757666997687879595676878844467633967768755461265735510400 +360023137544654575733228376896988578467698566656988978959969695576685749457685864437734765717026064 +453004005155344764583728835857979544659758957678779579869558958494954483997436423583442547122611144 +542364512554772785834745874957646879945679759867877799887755596454644564849589474263221373377352410 +002404034225236156254239388474876685554666797767767967895789558759867549659597464467365225556245635 +245410135477125426663788797639838547747878866579559887665897566567957973945395387477415314463463430 +134134265157751574644752634743354666668958565798799857969998998868959355953856752485414215452524346 +134233063522723113742886693543696949958945798696986777765986578999899884698774475872617234240653423 +244361001066345154366465584498936779955869489578987799745999756687935697875536775754117227145563503 +424615513054521426726444456756894679964484855474988695686689467459399699543485425832215323565610231 +010511022361177421578425884488933866869996559974758675556944646898573447548734866775441423552334113 +455000614233131572534883424588896457684489858997764475858957676964979965647378323342574224433221410 +554234433657562455545554335673953366894875796478846544767476975987878676385863745315342113644542314 +334234343615275457652768335759749448996379787584585465788795884334949898848654742611353314261636503 +333304555356366151638427333872998755488366686597689567687856687846596683534345663212543346305030250 +221202454261633375664542356764698437549939857548989949987335436749598666277762261737227146546505534 +240412550420563537276688628732843367868467736695759997434973759746799657483248636225432651522613045 +234101415413036337546543888388563596433499988836578844689689497975764628525732325317653041241353030 +431410263125326725415234754275746658476797893535668937455785755994425345326575247342241464251000421 +235305105630506142127546572374764553754685586586673543349859396362874755756655416517334460144522501 +053230360255455453432625173284836864837934635996948769536785973725832747476731215716516416145320150 +220341023145561653434447455522778482225563875986777563368797985846552555226462623761223514365411344 +555505021215644010564473315638562438843876455985785666379866472746877227331266714741141010601131000 +453335252144616255521312353556677472322787423527747647554222386736627248134362414253412412531431213 +042151015320560413551651234262565787675225582872838475557456742335545484137617276444245016450453552 +014404334344615462565743465273746656366752555484464325685467246827267515345457117051354205241444340 +213041551013263163616741653367152326275633684378845888827238722532735171772265542620411543050510551 +224455113205603560061536164734146772822552432636277537568582283454552565546556622004610432104440414 +120034300413036442405642132721761733145582724757284464865848374626544672733422446132110450322334043 +321102323253550034451665134657546626247721386824348475375862731112516436572563504423615532222151203 +423413023253053651012440232734663275166577433646527244777525111735773767313620631626060044333201413 +310311122105544263022025236566616265144255413423733523634375771573412213343520014446112203251501244 +242112304444105230054262221514232727536351532732445557376642241447573270443566533260102401044113312 +300401122201443130504625604224103567247721576334262357161547725313757534624304442441502400005401420 +312140241542522534423315456561105236441214136262754317657256235334116150340065011324403154142414212 +144233324321121441233255150514400352347655162673141155544742475145632635002103305025150134340421444 +111103103110050242122311312046534446626061741554364774752533752234163656421540312341011215100222430 +303431234002202314522440214134041163045124514037641775350115015005306206502562310105314100032000400 +022101131200322543535350013343206260166164021153334624135541245000601615213233143310354543221432032 +233233001110344441512334010360062644015111403065346201116113310635016323603102000435213440203331333 +221234200414024043305553013402501300310163441031202131631042043410330561032554522352320341422042103 +321123401142123425525550311314256215506650120142506242614512364236453401432552531430304012423410223 +122333244113420243051112411113522042223653563261365250113056645225530145055223432541231304233333203