trunk
HeNine 2 years ago
parent 07ade6f13d
commit 1a402dbeaf

@ -223,3 +223,29 @@ test-suite day07aoctest
, parsec
ghc-options: -threaded -rtsopts -with-rtsopts=-N
default-language: Haskell2010
-- Day 8
test-suite day08basictest
type: exitcode-stdio-1.0
hs-source-dirs: src/day08/test, src/day08
main-is: Basic.hs
other-modules:
Day8Lib
build-depends: base
, HUnit
, parsec
ghc-options: -threaded -rtsopts -with-rtsopts=-N
default-language: Haskell2010
test-suite day08aoctest
type: exitcode-stdio-1.0
hs-source-dirs: src/day08/test, src/day08
main-is: AoCTest.hs
other-modules:
Day8Lib
build-depends: base
, HUnit
, parsec
ghc-options: -threaded -rtsopts -with-rtsopts=-N
default-language: Haskell2010

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

@ -0,0 +1,99 @@
000110211310120301121312200301013204213433321324454125534120411314221421432204213123312010120220212
020012220322312203320423330100141123432232555552131513413333243201041414120414404222300212031212210
012220223030033030244430211234220045555352514254335343332514243345202201013120303123003031322301122
122212123203002004034001203313232515112543525251445353434224414533524014020242242322320223010131111
221102333103333221411324203034235135343455415425134251221521352512445343444242410343133031022213111
012230110220232130043321130142424543415542352534155145434324432553532253510101043422110311302133322
121211332110331222432012225134422432341112144421535153351545122134242232152130110334114400332212301
021003020222222304114022552315552411255554221162455263643351112333322235214353424140101110301013303
211101331332303134320025235254523341134225246335554356533426415514344521433235223100024100322012213
333121033001133202211331144545334545442426462335662255342342626524313432432255113203322322002312200
010322322141333044304115552115413253563353625666244464226452656243424545525555553333333441121311231
032330304041422420432124324244236264255242536445626642464646454324353143112245143242140134012131113
113222323414023431235314252222246225364232452564335525523344525664226653522112244131121334130320322
121002342323322351314412423235433523643565644552335525522462425253643354453342113152132444223043001
303334322134140123154241513636446642663346536533667552624355222224332425555525121255313000110224301
220002230431013514545542433426226364623335746773567356566543235364324426533313525143244100121033121
000021230222223532342154546526322562455734556753354636566447345465225625536633243334425112234211423
100240131421551532143146364665353436655665676767645757767765365733656436442643144352144332403323231
231322213332442222125662224633422364634335777574764763343776757435645252662432644231412254011230044
120320134105242421434423353553635756666465645446464476746533434474737333355643266531333324433212400
040301032133333254244335264252755376435754666436667554445363634375377335452223443455551432333010144
122213043542434415443363356663454534757746355453573654756377543673443533255466446625122132310014334
404243133333412213345434336533364653576735676667576876848337555643553537554265552455111211141430132
443341301142235415336322424546477554637688787745866488887577434344636437734332223546335121555322312
010101032453544354452355447644336446437444776676584487868688446743777537543333245666541212524431033
130342114125121245655353346445355733446776587588784546588474685553536367574445523434254125522412333
201242125553223436463223777745535566487874456667845657466784588644655657455355352552555444112241410
234243354255336642454256767534444878687668756444875858587854778474484437455656353656253411335452400
314411514514125254223353563444477554768588585448654748855456656667558676453446723446433551542333323
320425133145356325256655577477467888556845884576957768675647786647464447677575756555366251552252023
402024545131232242524557435777674845786545558766585956759687647846545584454443736545244555225513404
221122211244626363325464534767668748756767978988996968588875755867566656577763547532353323245443324
241532435425235552535674564444576465866975867987658575886697775645646467874734565535246546512244511
003324252512442566553463345787864877859757886699855979689556865965885747456343365533654353244413223
212315155352232566544737454548674574998798699555866589956966599795788486474536636336336342425134412
415441242523246256336374766665674457785867567956675565789686897696764587674446666642422423644424325
003554554162525244354667375865755578679858897996697678587867758755877644755534673566366364215111142
125233324364265264545556378688676958976975966767698999896779575577774547466453456456443363332524521
133515233335323373765654467667486986756665797876767877886899897979969475847765767637544446523515551
423321431443554355375376748446757899656688998798978687676986796699688855776746674753723255464344423
135533214666522656775554775567665797957679899988997898976779768787598768878445373375735633533152423
424233114545442654745646856857767575855668667669968786879669768579688844877458655735443652256255431
122311356255465443775736754747756597959866677969777897778796669799996864878444747533472262262432453
444412515423634374457557568574698568569796689667999968899788697787968667768664344766546653236143151
152311513646633365333568844555559779697686687699777998989779788768756594585758837374575424224211225
432411444656462746443756865787956975798986999787977978996987778795768586656655473453364365634145515
512553242524235344467755668868885977997876879978998878787888776866887566586456466377353633526612112
115351124324666765477575484768566785899876668997898988879879687896986577677786744336653645225533134
521325356522544377445774755666867759866698988779997998778687879779588576866784766376455535443334445
433451166554445354353554886675567598968878879979777887878988976979689787756548837435743345242455141
251112342434644676347767747746557587886869778877788877789787767787779598548856756754774536542212331
255533535452264557543768667886686886969966689777797988888776799775755889645756557644656535253352415
324425154344254736457678866646677886667896679787789878898879666995998757877665757356457366362624453
133124434355236446553476678549686757888986787979777777777688968798699766877778645555763233323653111
222452162524456534544666745659767986569977869888798889797967869896588579678645466757742223536551125
424113153224246464437444666655586769888767899997999998888899787785789988565667547344466324245444121
342222535222265477673374467445978889589796696777887788896778866766575958886864746654456432465143345
343222443646553344666565758455597897568767997897989877768698886656896987444854633767335332566315312
031554353342263455343465844688566996587977697767668887776787776985778754456646747553435562222553224
433344426223464443364358688754655656655796979887796696799797776855755885854676557736723432655514323
013424132332422364434378758577758958968878668787687777668989797955778846555856344635566224523432451
425553255655633236466566556555858699675969687978968799698978595977567446657483654746364253324544543
432514124535636275635556647685849565689989686678799779666769657669586678877883365364425535653142315
234213331454244233774547658465845955685598696976697898886596997865578648447647767565654326321345225
033142335256632253667547655784646566787758855877679696866998778855965474867574633636266434253231343
433312355254243222736457466486484557878576698897857857878667659897748755645557367664665624322214245
414352553244643424464634565857556876595979575859559697897855757678648446474443347476454343621315123
142445323134524455344767565745845466776955777775867689676669795765547875554364474423523255433452550
420122443552465225677645453567777645666955697588578797786675779777688864467444476525625534353143351
013443533555256553426677376778475746848576769888786588999657647784787744733675443533555552342334414
041143134325455654456573473653567744748677959966765568889765584875786654736746477425243323345412331
432143141143446546334633656476476747668864878778688989957788768655568457357565474424624643255141123
433102225554544652566677347757675544755546776854548575485655786465456345347635444252452425412221021
323443424115135364655624436464533765445864646766688454775574785855547635756475456566323553413444404
040133323452356462366536677466667655766665575477656846545868855587636377637445452653223313533353431
112321424255322325434562636744647576545484488884676746485886457756657434733352222326531315442212112
414000041225332224365336457576353475444476854684468466666787764643346356664452563552325454125532334
333211225554233356326636546733667664766764488678465547455847554453346674333644366565122142144303010
201332104431213354534342624674574463634343535468776645455555777363557644444632234452452514524001241
221121011452151314652546563557737463367364573534653347663557476557753774362253653622553112413343020
301020124355152323443536262535766773343757377576554465333456475773466764562232455333212154220142002
322244112312155113454636553224336764436375336773533577673334677767346562553664626122341142230310021
023103242244551224134442622342463557736536665447475654776476664656746264566536561531213151202033312
000241244142514451335326646524645362354365656733573556434643567466354436652444155224421243124420312
211134103233321112311333366243566524426453645455335474755773645624422266326533321441535122300041202
010100412012005141235135226623364644564632374554665337675444336522352336344211133431343203034321031
120100431404011341421515543345452262445624345326474644544443332326425454424431513314250240023131303
302230432233042151414231553363365325522333634334265345542443426352642445554145124343411121104121203
303233311431323211311335332253563563355242335445366433543356662343656654554132224132234403131422203
113031120441440344151555331254253525425556245652343434526624622453624343111341315343030233140303323
113030023221043232305142142324135442534246224522233522334666345254334253143442354430224400423233033
031102021123340100023323433221314345163424223524353422623255522232122453215412122243144210131021220
123100111112040244104314311535251111141234335442354234236235354514311422435123332031213231120131013
221222103112033302010413133424335241315253243263446652241133155434215112225523013040443233102021312
000012122130221202130324443534215451343511144224113442315342555214421524454224402343214132100123030
112012230223002044203111222225321532514541153222214115443212344354545231533442344002011210202230120
221200232013222231032340233141433354514311213113224441111332422254143133440204220101431230200002121
211212213121303211020441232042142451254444244521121152121224435112515234204202321124222332312210022
202120032200122003044340422223002041534554215334252122413441432141223402244034330212233311001312221

@ -0,0 +1,52 @@
module Day8Lib
( lVisible,
rScore,
--
processInput1,
processInput2,
)
where
import Data.List
import Debug.Trace
lVisible :: [Int] -> [Bool]
lVisible = lVisible' (-1)
lVisible' :: Int -> [Int] -> [Bool]
lVisible' m [] = []
lVisible' m (e : r) = (e > m) : lVisible' (max e m) r
rScore :: [Int] -> [Int]
rScore [] = []
rScore (a : r) = rVisible a r : rScore r
rVisible :: Int -> [Int] -> Int
rVisible h (a : r)
| a >= h = 1
| a < h = 1 + rVisible h r
| otherwise = error "what are numbers"
rVisible _ [] = 0
-- ####################
processInput1 :: String -> Int
processInput1 input =
let trees = map (map (read . (: []))) . lines $ input :: [[Int]]
hVisible = findVisible trees
tTrees = transpose trees
vVisible = findVisible tTrees
visible = zipWith (zipWith (||)) hVisible (transpose vVisible)
in sum . map (length . filter id) $ visible
findVisible = map (\x -> zipWith (||) (lVisible x) (reverse . lVisible . reverse $ x))
processInput2 :: String -> Int
processInput2 input =
let trees = map (map (read . (: []))) . lines $ input :: [[Int]]
hScores = hScore trees
vScores = transpose . hScore . transpose $ trees
scores = zipWith (zipWith (*)) hScores vScores
in maximum . concat $ scores
hScore = map (\x -> zipWith (*) (rScore x) (reverse . rScore . reverse $ x))

@ -0,0 +1,41 @@
import Day8Lib (processInput1, processInput2)
import System.IO
import Test.HUnit
testCases1 =
[ ("data/input080.txt", 21),
("data/input081.txt", 1779)
]
testCase1 (file, result) = do
withFile
file
ReadMode
( \handle -> do
contents <- hGetContents handle
assertEqual "input test" result $ processInput1 contents
)
testCases2 =
[ ("data/input080.txt", 8),
("data/input081.txt", 172224)
]
testCase2 (file, result) = do
withFile
file
ReadMode
( \handle -> do
contents <- hGetContents handle
assertEqual ("input test: " ++ file) result $ processInput2 contents
)
tests =
TestList $
[TestCase (testCase1 c) | c <- testCases1]
++ [TestCase (testCase2 c) | c <- testCases2]
main :: IO ()
main = do
runTestTT tests
return ()

@ -0,0 +1,67 @@
import Day8Lib
import Test.HUnit
tVisible1 =
TestCase $
assertEqual
"visible 1"
[True, False, False, True, False]
(lVisible [3, 0, 3, 7, 3])
tVisible2 =
TestCase $
assertEqual
"visible 2"
[True, False, False, False, False]
(lVisible [6, 5, 3, 3, 2])
tVisible3 =
TestCase $
assertEqual
"visible 3"
[True, False, True, False, True]
(lVisible [3, 3, 5, 4, 9])
tRscore1 =
TestCase $
assertEqual
"rScore 1"
[0, 1, 1, 3, 1, 1, 6]
(reverse . rScore . reverse $ [0, 0, 0, 1, 1, 0, 2])
tRscore2 =
TestCase $
assertEqual
"rscore 2"
[2, 1, 1, 1, 0]
(rScore [3, 0, 3, 7, 3])
tRscore3 =
TestCase $
assertEqual
"rscore 3"
[4, 3, 1, 1, 0]
(rScore [6, 5, 3, 3, 2])
tRscore4 =
TestCase $
assertEqual
"rscore 4"
[1, 1, 2, 1, 0]
(rScore [3, 3, 5, 4, 9])
tests =
TestList $
[ tVisible1,
tVisible2,
tVisible3,
tRscore1,
tRscore2,
tRscore3,
tRscore4
]
main :: IO ()
main = do
runTestTT tests
return ()
Loading…
Cancel
Save