Skip to content

Commit 862d3c4

Browse files
feat(day8): solve day 8
1 parent 3ef082d commit 862d3c4

File tree

5 files changed

+197
-7
lines changed

5 files changed

+197
-7
lines changed

.inputs/8.txt

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
101002200302221033023303143134003020403131311534441424251301311211314032120023113231033031121022011
2+
000000201330102222312340313324430424413214435242113342242434514432041413213343304030120232213020102
3+
010000203232213031433114342021403232235333514333553111322134215441434143114041342040010222003200011
4+
201120030122210332140433403113122131244442241135435342113515341543341343142312131142111133212030212
5+
102222201221120143442131002141522315514253432414212415252133341422341353034001030100033222300111111
6+
121102223123133443003222043412143224143335142344513313134225453143435255411141013220230131131103220
7+
001113200123030444213411141214335523335333534154243113221355254515244442254124002044421400233210010
8+
110103213102123332202242154123512313351111234166565435351154333252135532152130134301110311011211302
9+
202210200232024303040121311534141553542265444265636563223452433241214155443145244322333331220130021
10+
311321020104411241422115232552455355223632332662654636255644333321432523545314210123114112022121202
11+
222002132341041021001451442245143233564442235333245652526562422434515153555514555434223101320132320
12+
012330123123141204422541513243454542553522546663646433355336545446455311231412535553131322133211112
13+
221020042122012412514131344114346532626463563556224633256642443366425455121512441453010231033211133
14+
332033424242124441354515425455263244464236244225445544325265663643264335515552445211224403010100310
15+
030311420043214251151555343264543446453446552646733662422253465425563224633342525545450000211134232
16+
133130242134014131114123322632555364662426753737446646475562363245344344452314544451115100103124211
17+
010241031411233332141421664544623522355767756747554554747757666264354245633431233234531112142010000
18+
032200414320445341121222366366536655363775663475543654367763436632366656252654425223554320030244023
19+
100312204312544442231224422656636343643666666675437535763634446467552325246643265513544155011313414
20+
224200324014345533246524254552547477646677554437567554337355755446755334623236365331343231024211422
21+
033313133343534535442526235566556757756376445547773754345356657367535536454223645252453342101312232
22+
014214311423213321245644363264673667435545337543647756777577353345733666342643335553134311523334310
23+
433012115243133522264542435556563337454353635845778758545734666374637354763652326265532341134234010
24+
122104225233125212546633243745466643765468465558687657485774737446654743732535366535234121125424032
25+
204422031225321235542565463563676654365748455655554855875447656357557557553564355522251545122120414
26+
104030155235414325532425576445546567668547857674866766557768587675345755454524663452432145531304002
27+
433201242123126354333256673757544348684646787646657846685878846678477736547744545443362515111430002
28+
421041533111312262353644534644765667864485488887876654464654665486755377755356532333562554413540432
29+
342132214445526355546434337565344654655444658854445646855468648464874347744635352463235535235421003
30+
222432351445342522443766645456676656776867887889896877875754784787546635745633765324264433333551223
31+
341212355142466246464534667453446875455885986995966865976564675747584763576337373532355453154423412
32+
414423452431252544553637745358445755855869665699958857986985775584484587767664443655566454352331120
33+
331215421413552422267473475374764764685968687855585566867657585477656745667376557663644634513533254
34+
141521331136665233544534743877458854586759789577787777878566877976454647447747733666555522535352251
35+
402144545565566445336477735645686455987955977976986897797959998564448556455676776442342252552521451
36+
322442131336445645474737578875588748678578675776975869885577996779444458486775646674445543542124334
37+
003241123534254267466637376887768457676676697687767866888676895686968878848835646576663666545432111
38+
234333144534365657733346788857655988779985696798896978698598888876655458486736676576434555325322325
39+
033552244452442344756445574878458788978579767688788877779988976889865655445866355444755456364252412
40+
045212224265223274466546745678787779868966769796779996688776788997588766768566653733542466332343212
41+
022345335335622575577377665548779887775887668866967689898976679598597977564865565676552665532252433
42+
124424326262234276464756765868756565759887879677666689797988796776968864654566663767726322324124335
43+
333235323262262774735557685448988678797797668789888699889789979858656855774878653676666634655531341
44+
424333343566654656357438457485875989579778669668898998866878867779978654788886774367636526533252225
45+
415125222443226566336488555748579996699698777779888777798979989765676998674856437637353652266454354
46+
352445544426323443664578765568777867798698798797898998876679686986759578786745736574633332255424325
47+
234414146454465754353766676787665695889898688889799998979699988898879958587847734733456456546445213
48+
253423343266262636346545784545955997786868689787777888877989878667585988554475745545464666246345255
49+
223344452632624554335577685647698768667799967878779989777676967798899578558744854574747232646433315
50+
323143554223367457566474647879967975786699778989879879989898896767757786678775547767465565642312542
51+
453333425665457354663758546689578757986968687979879987979786897878578778558764873445465456444554251
52+
155241123442465734343774685765569588776788977988999898777967666866598756658647635644333635325524324
53+
133324323353235556775356684478588589688899969988787797978967799889667778488858574745533345265415432
54+
133433465463254577643484465878597856769979997797877989877976896969996799855585466476573354552411331
55+
322142442345334767374787688576777897796868768989777997787767697699997566868745535447346265543441455
56+
334123333556324657343574547669589758777898966977799899896987686797899798466557773536632423622355555
57+
252411345622435455733376748766597586798899988899787799866969666997598966844867765636372634226515431
58+
151451422566464676476636547768757596689778699796978997868699677765865767487847755454345544253541243
59+
241522424344532733447477666686899766556969789978797986887979976685588974646546376573463425453144442
60+
414153442333535675773778676465857568699869669979798878799699987555578544654866655766355464256143434
61+
254535114546225443653565564468588956985676686799687868668969677759799544785465437745366532254235544
62+
242354325525625246745576475877646586859558986979977676788989969868686685585574645776626326624152314
63+
311534354442664567445645456546887875578685886896868888778995555587868655487466666734642465562551342
64+
002211144542443356445646675767857767788697589869678789696659999887964585784844754653622362431441151
65+
321444222136434263753573364864445668989879955758697979559578965668845546758456377667636425635512435
66+
414532112334525645777645364755584566557779787975679695558987865866785684465356447773463436335142124
67+
031344233134526256667673555464868564656878988979998868666676959687487848847374753332446352421433230
68+
033414314236345666365347334576457447479979876959976796697989878876845844877346675432432545345335111
69+
223234514233244546235744434764588774445585595867867967979958876756686776747444433756462224514313130
70+
034354325554623636456367743544848648668869978989756578576878558576448675345736576332464452532335404
71+
333314415414336544645473564754765775745445998599878869766776785445665883745666543445236422534254343
72+
010124352335143245623535734775575868768457445896887988785744846678545657565366664436462333352114420
73+
241133243154345423423255773635648554845867764484448485764485687547646634676644634466423212541145312
74+
144323242255213222655427634775767456477656885488677655667865776854846364533353433622362243343123130
75+
300121513332133346652524546544554657846788487754577484485476445856537556455366326344443253531244041
76+
214312313412252446224434245656574644854775458585756885886757548546376564453624436535431411344442441
77+
344413254252322432546665356466536346455488854547546677556747685543743366537445433643315232412504023
78+
030420125114122234256653334466366437674664466777764545468744766663566333665553255266154254345444421
79+
201022041343111332264235342277534444744733675444554744888635745554563756363264366244234521125431143
80+
310101422412112412564453556665535356375565736473373764446357574757477663532553444551433143140032240
81+
012314020152242145455646464446435574447466754663444663635667635447474466354626635343323553204203443
82+
301431002405324343435445225364363665573647453443457376344534655734744645626433554455413541101031210
83+
220402234402321244123522323565664673457656366554777543476673677437665464342236224222241525244012343
84+
220413243141534321333134422233522646445365544774576465633777457645452462264364422521511214321414141
85+
132141004144343445425341422356543254466645567446756557773334777235533435444561522153115533032340122
86+
103320433201024354323412453646353445225544357655543544744544433555545632335324333523314131010244103
87+
120000103420114333412252226262424253356432654542437554352625536654452456453415255241241141213210301
88+
230111133023321345242332554223645255622442243546353532232263224265253656423424442512332241113030012
89+
012002204104404423142222435245362563453526446562334552352242626533532663145221123423411140404113320
90+
133132301104434132213122523451216645666342442525664663356233663355434224331132321242424042020300010
91+
300231011102122014443143353352244166655656365456622444622563466362355333112242211003402201113312131
92+
131113313033421421004254454232251141536335223326242246444432526552154551545414220312100320203333130
93+
003102322130011220131211452522433142255236443652565643264342211255545111112515532343240204112100111
94+
123133331310024423412433454313542233234224542223452553553512331141534342314224343121244143312101102
95+
122232320002101404113223133225112114533212125134253312412213451454333424514233200042430112130222232
96+
011003112002012034340041324422224354112355545251413535512312452115342144411141020000324121133331211
97+
110222203303002103314434412231554151352351455233215445523441215221313240230030321334321131320033200
98+
110202122210001303002442001413413542523211141353543531115315334551133023131104242032323233333112222
99+
110120101211020201230001300132340142214214554553131253415234133311121310214320100143302113213302021

advent-of-haskell.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ library solutions
9090

9191
build-depends:
9292
, aoc
93+
, array ^>=0.5.4.0
9394
, base ^>=4.16.3.0
9495
, containers ^>=0.6.5
9596
, megaparsec ^>=9.3.0

data/examples/8/part1.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- Example 1
2+
30373
3+
25512
4+
65332
5+
33549
6+
35390
7+
==
8+
21

data/examples/8/part2.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- Example 1
2+
30373
3+
25512
4+
65332
5+
33549
6+
35390
7+
==
8+
8

solutions/Days/Day08.hs

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,90 @@
11
module Days.Day08 (day08) where
22

3-
import AOC (Solution (..))
3+
import AOC (Solution(..))
4+
5+
import Control.Monad (foldM)
6+
import Control.Monad.Trans.State.Strict
7+
import Data.Array (Array)
8+
import Data.Char (digitToInt)
9+
import Data.List (foldl')
10+
import Data.Text (Text)
11+
12+
import qualified Data.Array as A
13+
import qualified Data.Map.Strict as M
414
import qualified Data.Text as T
515

16+
-- TODO whole day needs a refactor and a performance check. Part 1
17+
-- seems fine, but part 2 is entirely bruteforce. Still runs fast on
18+
-- my machine, but there has to be a better way.
19+
620
day08 :: Solution
721
day08 = Solution parseInput part1 part2
822

9-
parseInput :: T.Text -> a
10-
parseInput = error "parseInput not defined for day 08"
23+
type Index = (Int, Int)
24+
type Grid = Array Index Int
25+
26+
type VisibleMap = M.Map Index Bool
27+
28+
parseInput :: Text -> Grid
29+
parseInput input = A.listArray is $ digitToInt <$> T.unpack (T.concat lns)
30+
where
31+
lns = T.lines input
32+
rows = length lns
33+
cols = T.length (head lns)
34+
is = ((0, 0), (cols-1, rows-1))
35+
36+
part1 :: Grid -> Int
37+
part1 g = M.size . M.filter id . bottom . top . right . left $ M.empty
38+
where
39+
(_, (rows, cols)) = A.bounds g
40+
41+
lefts = [[(i, j) | j <- [0..cols]] | i <- [0..rows]]
42+
rights = [[(i, j) | j <- [cols, cols-1..0]] | i <- [0..rows]]
43+
tops = [[(i, j) | i <- [0..rows]] | j <- [0..cols]]
44+
bottoms = [[(i, j) | i <- [rows, rows-1..0]] | j <- [0..cols]]
45+
46+
left m = foldl' computeVisible m lefts
47+
right m = foldl' computeVisible m rights
48+
top m = foldl' computeVisible m tops
49+
bottom m = foldl' computeVisible m bottoms
50+
51+
computeVisible :: VisibleMap -> [Index] -> VisibleMap
52+
computeVisible vm = flip evalState (-1) . foldM go vm
53+
where
54+
go :: VisibleMap -> Index -> State Int VisibleMap
55+
go m i = do
56+
mx <- get
57+
58+
let v = g A.! i
59+
visible = M.findWithDefault False i m
60+
61+
if v > mx
62+
then put v >> return (M.insert i True m)
63+
else return (M.insert i visible m)
64+
65+
-- TODO find a way to not bruteforce this
66+
part2 :: Grid -> Int
67+
part2 g = maximum $ scenic <$> A.indices g
68+
where
69+
(_, (rows, cols)) = A.bounds g
70+
71+
scenic :: Index -> Int
72+
scenic x@(i, j) = lefts * rights * tops * bottoms
73+
where
74+
lefts = cnt + if cnt < j && j > 0 then 1 else 0
75+
where cnt = countScenic x [(i, j') | j' <- [j-1, j-2..0]]
76+
77+
rights = cnt + if cnt < (cols-j) && j < cols then 1 else 0
78+
where cnt = countScenic x [(i, j') | j' <- [j+1..cols]]
79+
80+
tops = cnt + if cnt < i && i > 0 then 1 else 0
81+
where cnt = countScenic x [(i', j) | i' <- [i-1, i-2..0]]
82+
83+
bottoms = cnt + if cnt < (rows-i) && i < rows then 1 else 0
84+
where cnt = countScenic x [(i', j) | i' <- [i+1..rows]]
1185

12-
part1 :: a -> Int
13-
part1 = error "part1 not defined for day 08"
86+
countScenic :: Index -> [Index] -> Int
87+
countScenic ind = length . takeWhile ((< val) . (g A.!))
88+
where
89+
val = g A.! ind
1490

15-
part2 :: a -> Int
16-
part2 = error "part2 not defined for day 08"

0 commit comments

Comments
 (0)