From 9a250d6e6699f107a0f13041c2caa7a581281896 Mon Sep 17 00:00:00 2001 From: Natty Date: Sun, 3 Dec 2023 20:52:47 +0100 Subject: [PATCH] Completed day 03 --- day03a/.gitignore | 2 + day03a/day03a.cabal | 29 +++++++++ day03a/input.txt | 140 +++++++++++++++++++++++++++++++++++++++++ day03a/package.yaml | 35 +++++++++++ day03a/src/Main.hs | 28 +++++++++ day03a/stack.yaml | 6 ++ day03a/stack.yaml.lock | 12 ++++ day03a/test.txt | 10 +++ day03b/.gitignore | 2 + day03b/day03b.cabal | 29 +++++++++ day03b/input.txt | 140 +++++++++++++++++++++++++++++++++++++++++ day03b/package.yaml | 35 +++++++++++ day03b/src/Main.hs | 25 ++++++++ day03b/stack.yaml | 6 ++ day03b/stack.yaml.lock | 12 ++++ day03b/test.txt | 10 +++ 16 files changed, 521 insertions(+) create mode 100644 day03a/.gitignore create mode 100644 day03a/day03a.cabal create mode 100644 day03a/input.txt create mode 100644 day03a/package.yaml create mode 100644 day03a/src/Main.hs create mode 100644 day03a/stack.yaml create mode 100644 day03a/stack.yaml.lock create mode 100644 day03a/test.txt create mode 100644 day03b/.gitignore create mode 100644 day03b/day03b.cabal create mode 100644 day03b/input.txt create mode 100644 day03b/package.yaml create mode 100644 day03b/src/Main.hs create mode 100644 day03b/stack.yaml create mode 100644 day03b/stack.yaml.lock create mode 100644 day03b/test.txt diff --git a/day03a/.gitignore b/day03a/.gitignore new file mode 100644 index 0000000..c368d45 --- /dev/null +++ b/day03a/.gitignore @@ -0,0 +1,2 @@ +.stack-work/ +*~ \ No newline at end of file diff --git a/day03a/day03a.cabal b/day03a/day03a.cabal new file mode 100644 index 0000000..8d8651b --- /dev/null +++ b/day03a/day03a.cabal @@ -0,0 +1,29 @@ +cabal-version: 1.12 + +-- This file has been generated from package.yaml by hpack version 0.35.2. +-- +-- see: https://github.com/sol/hpack + +name: day03a +version: 0.1.0.0 +description: Please see the README at +author: Natty +maintainer: natty.sh.git@gmail.com +copyright: 2023 Natty +build-type: Simple + +executable day03a + main-is: Main.hs + other-modules: + Paths_day03a + hs-source-dirs: + src + ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N + build-depends: + base >=4.7 && <5 + , containers + , extra + , megaparsec + , pretty-simple + , text + default-language: Haskell2010 diff --git a/day03a/input.txt b/day03a/input.txt new file mode 100644 index 0000000..1584486 --- /dev/null +++ b/day03a/input.txt @@ -0,0 +1,140 @@ +...788.............................54.........501...........555.........270.................................521......893.................... +..../..*963........................*..860......................*....53...../.....................52.................&....347........428*522. +............*......41..481+.......462....$..187......678.......420....-....................&115.+...........................+............... +............707....&.........562...........*..........*.....................438....................&877..660....199..145.........71......... +.....210................356..*.........977.68.........38.......835.622.332.....*300.....131.422..............89..*.....+..........$......... +..............14..312......+..926.....*.......529..*............*...*....*.............*......%...310.......*...835..................885.... +...416../467..........................423.....*...143...132..955...356...124.........588..947....*.....512......................134&.*...... +.....................*688...=....../..........194..............................................148........*815.......................785.... +..673/.....957...............103..104............................../..&.....888......408*703.....................@......896..4..526......... +.......628....*..62.......15.....................885.............649.720.............................93........703..........#..*............ +...*....*....222..*.........*795..........%...+...#.....54.310.....................622....916.......=......./.........../.......493......956 +418..*..57........125...141.........965..382..177.......*.....-.......390.....801....*.....&...659....406....912........614.448............. +....926...................*.#.........%.................517......*......%........%.301............$........*.......694......$......&........ +..............$476....167....208..866...............86........818.200........588.......@....%.............534.....................324....... +...571.977...............*...........*551...................................%.....992..172...849.................578........814............. +......*........176.....705...../406...........42....226.........58....................................*621..........*......*....#54......... +........194.....&....................905.567...*...............*..........287...........303.........82.......600.....642....672............. +.........*...................*726......*....*.714...............341..530..+......415.../...............544....@............................. +..795.429...........$.....615........582.240.......*153...#.........-............&.........313..........*...........#.......*............... +..............443....471.......770..............651.....649.....&........*211..............*......548.235..........2.......898.............. +..........916..@..*...............*......966.................869......843......*........744..410..+..................777*....../...728...... +..976........*.....699.......763...893...*...462.......608*..................23.227.....................411.793..........984.683......*..985 +....*.2*....787........258......*.......878.........................923..............................*.....*........................674..... +.241....966...........%.........804...............589....554...307.+....#.505..........&....332...449.190......780........*322..........-540 +.................126.....837.@.........701.......%.......&......&.....581....*371.......753..-....................$....426.....=............ +...........%.....*.......&....264.......*...................127....................562.......................379*...........786...#.-....... +....376...596......................%...511.551.........868....*....................&............224..............992............817.309..... +.......-...............374....-..164........*............./..627...340..274..710...................*.......347.............................. +...535..........195.......#..278............495....................$.......*....@.....805.570......724.....#.........98..................... +...*...579*65.......................400*...........849......764@.........180..........&....+..576.............573...*....................... +...................648.162...../733.....462...........*.................................&......#....484..10$....*...............981......... +....712.....495.....*...$..........................932...............278..478.%728.....798.............*.......483.717@....593.....&........ +...........*.....738.......................474...........772..877*98....*.-.............................556................*................ +...870@..587.676............................*.......569..*............454.................=....832.249........369..264.........298*543...... +.............*....246.608*.305............227......*....429.....57..........889...745......648....*..........*.....*....................502. +............901.....*......*......................55..............*704.264*...&........................543...234....859..$.............#.... +..................693....661.....*641.................909.....800.......................779.811....789.*.................90...954$.......... +637.....60....................607.........112........*..........@.89.....................#.....*...*....601....227..................32...... +...#......+....132........410........%.....-..795*....702..........%..........495.............285.438.............*711..........346...*332.. +......237................&......925..366..........340.........658......+..282.....532...................252.............195....@............ +.13......*95...879............................................*......169..@.........%.....782....%..%..-......202.973...............765..... +................*......601.......864.....942.=195.=284........6.................351..../.#....618..409...........*...................*..965. +......673...../..862.....&.........*.....................................381.........777..................644............612.......852...... +.......*....823...............211+..228......292*....418..290............*.................................*...-89.......................... +...363..526.....77..../413................$......208...............*....80.......835/.........-...*......458..........5*214................. +..................*.&......206..........13.......................35..........338......%.......529..79..........718............&............. +........#..840..710..112..*....308...........&...618......................%.........130...917..................*......739..221.........106.. +.....205...................417....*.....891..634....@.........241.........21..............*.........465......524...8...............*48...... +.265...........*33.....431.......770.......*...........923.......%...........*.......449...61......*..............*.........$...409......... +.....403....202....226...&.............798..713....321................=...337..........*.........128.....826...=.33........655.......843.... +...................*............943..................*....449..576.835.............$...945................@..506.................88....*.... +........143...-.636...920......*...............=...580.......@...*.....729.........839...........................347...................608.. +........@...847.......*........167.546......485.................453....................365.905.643=.........692....*.........795............ +...................997.....919..........586..........374..............................*.....*.........529..*......34....660...&........573.. +..752...853......*............$...407*..............*...................928.......641.988..201...+......*........................$...+.=.... +....*......-..289.857.................403..........992......-.....206..&....../....*.............328.....28.........$..........957.552...... +.....155................506.761..............................902..+........202...578..*.............................700.................351. +.........................*...../...............909%......737..........*267...........907....981..........273............887.&............... +.135.945...............649..........319.......................413..797......468...............@................$.733.....*...155.904........ +........*510..508..964......*248...@.....68...........285.......*.......289....*.626+............76....#.....873.........723......*..800.... +.............*....&.......51.........244..*...754.....*....#.607........*....148.....................800...@...................895.....*.... +..863$....831..........................*.913.*.......326.215.............882...........774..................252....................#...929.. +................9..........438.......652........239.............................111...*......39.........497......./519.............743...... +..746.369...486.*....308......*...........736...*................495...............*...20.../............=..................%...........#... +.....*......*....786....*..224.......5.........205...397..........+.........456....833.........523*.............638......292....399..855.... +...........135.........383...../...%..&.................*....335...........*.......................669.684*.......+............&.........980 +...............974*185........680.987.........114.......117.&......755....596.../450.............-.........571.....................818..*... +..395.....................................823*......645...........*....#..............1...172...405....................*.....766....-..815.. +.....*....769.998........762.325*440.976.........................507..833.........604*...................282......$.926.668.#............... +.....280.*.....*..75...................#......314.....666.................533..........789.......863............138...........494*955....... +.........77.......*.....907....732..............-.....*...224............*....283$......*.............................................348... +.................161.......*.......................106.......@...&........339............634..513.......729..68..........890.746............ +............................551............-...368......&......21.................520............*.......#.....*........*.....+..4.......... +..................*548....&.........843..290.....=......532.......................*.....190.393.404.........240..535..830................... +......721$...............982..........*.............811..........................276......#..........753*...................715.202..375.... +..334..............................111..969.971........*..................199...............207......................792.......*.......*.... +......793-..640...........=...............@....+..763..591....542......................747...*...............220....@.............#....598.. +...........$..........&....723....72.652.........*..............#.....120...............*.....659.......%.....*...@...........435..41....... +....*875..........22..958.........*.............480.....................*............876..............44.....608...305..593...-............. +...........526...#...........329.208...@544..................885*751....756.74...........$139.=.........................*.........557....248 +950..570.....$.........&.......*...................=.......$..................$........-.......173..&801.........504..215................... +........*.......924....658......41....142*........31..937...189.....100...........&....447.840.............................804..654.....%... +.747......481...*..........849&...........836...........#............*...........447.-.....*..........862.....108....343.....*.-....41..91.. +...........*....783...........................................865..41.................154..520....367*.......*......*.....578........#...... +852...43....759.......516..66.....*....422.........978.93-...*.........@425.579..27..................../..751..326.347...................173 +.....*................*...*....204.627..$...........*..........57.287..............*........$755....356..........-.....77................... +..428..342...$335........296......................971..............%......294.968.721.546......................$...........630.............. +................................649...........624..........-835.......-...%.....$........*........384.........18..131............545.654.... +....36.114..106.............853....@.........=.....737...........*....517...414.....784...607.........411........*...........=.............. +226*....*...*..........508.....*....................*.........303................../....-........................887..173....349.179....343. +.....903.....473..............190.-702.734..........24..............734+................198.....191*402..810..........@..................... +.........712.....405......967.................257............822...........664...........................*....+.$...........441............. +....608...*.......*.........#..........796....*...../........................*................800.....747...939.356.45..325.........899..... +......*..707.......629........570..358....*.66.....56......302...............804....+...290*...*....................*...$.......756..&..815. +.....697......................-....*....357....878.........*......@...931/.......*.511.......................958..169.......772*.......+.... +...........777..369..............257.............@.567...334....785............171.........-....292...635...*......................@........ +347*..........*..*../........962....................................................968#...763..%....&....561.....136...327*777....159.708.. +....192....739..831.888.......*....304...................................715.........................................-...................... +...........................658...%.*....................71....762.......*.....&605...509.401...543..970..........761........382............. +.....948.502...520&............188.689...................*......*........538............*.........#........947..@...........*.....530*...... +........*.................................309+.172*744....481.29....@........-716...........................................227.......56.... +...........*.....930.347&........................................111..6.......................578*..........&.188.......304................. +......151...88...*.........697.........111-...546*.....663*249...........482*7..........327.......423....513...*........../.880............. +...../.........309........*....-985.........%....................619...=...................*.................335.....808....*............141 +........740............414............624..10.720*395........91...../..520......164......318...........397..........*.......560............. +........*..........27.................*.................736....*.+.............*....../...................@......344............361.....336. +.333..271..25.............958*839...991.................*.........82..........624.....286...........663.*...535...........383*.....=........ +....*......-..........368.........=....................945....693....692..........769...............-...153...*...$811........277........981 +...667...................*.........687....235.................*.....*......210.......*.834.....$.............73...........&.......71........ +............-.....129....647.300..........................%....568...611..........262...*......742...............-213..754.....73.*....208.. +..9.......19....................=.129........719$.....795..82...........................375...................................*....235.+.... +...*...............................*...348............*.............260...638........................479........165....151...846.-.......... +..257.524.......695.......787...403...*.......336......63....788......%......*.162..%.......................854*.......*.........492........ +.........*377...../......$..........852...681....*............/.........923.......=.198..996.718.....809............443...............415... +....267..............%.......................*...533......617...724........#...$...........&...&.......@.......700...........%418.....*..... +145*............401..447.....634....&..819....9................*.......362.....241.....102..............................412.........929.14.. +.........642/...*.............*....324....*....................394...#....+.........................661...................*.773.........*... +.....165........600.........492............253.918.......537.......140..+........*...................*.................773..*.....=.....433. +.....%.....10...................................*..83......$...........241.......576........*417....969...........852&.....848.821.......... +.........../...153...........25*317......628.578....*....*.........*17...................227........................................995..... +.....889*.........*.459..697............*........304......852...788....630....720*594............$...............910.801..296...939....*.... +223......235...607....*.*....802..249...233...............................$..............177/.734........828........*............/......830. +....................439..944..*...*...*...............=..482..&...............839....................349...*...$........./...........$...... +..................%...........326....3.66..........628....*...350......544-...........763*.......83.......142..792....974....#....663....... +...$...*....729.916...........................916......221................................689...............................456.........62.. +..621..436....*......#520..808........670....$......*..........121=...............780.........*844..........=..........827%......338...*.... +.............24............*............*........601.346...621......531.....452......*.....659.......-297..697..468................*.936.... +........265............905..64.&...589..960..................*.......*..........62..19.........152................*.......+.....243......962 +...........*..220......*.......757.#..............-270..697..588....461..263......*.......@373....*........464...244...688..............*... +.........272..........993.536..............................*............&......961....=..........490..198.....*................181.....236.. +......$....................*..406......................*....238..94..74............349......723............646..328............*............ +.......139......./...........*..........624.........996.784.....*...-.........61.............+........*........*............981...557..66... +..............313...776*....228...........*...749...........246.579...240.230..*.=611..........*....917..................+.......*.......... +.703..730.........................*961.764.......*......979*.........*....*...........=.....558.906...........58.875$..559.*274.549..819.... +...*..*.......637..............853............821.............65*676.47...272..=213....45.................321*.............................. +.661...964..............540.......................=....-.955............+...........$...........143.907..................1...10............. +...........660...........*..........109$.......415..353.....*...........572......878....77.........*.....................$.....*131..182/... +...........*.....963.....395..............871*............$..994...336.................................319.....88.620....................... +.......&...625......*.........7*121...........494......=...8......*....@..............................*..........*......998*973.......$..... +....691............614...795..........152............120...........238..496...........................477..........................994...... diff --git a/day03a/package.yaml b/day03a/package.yaml new file mode 100644 index 0000000..ffd945f --- /dev/null +++ b/day03a/package.yaml @@ -0,0 +1,35 @@ +name: day03a +version: 0.1.0.0 +author: "Natty" +maintainer: "natty.sh.git@gmail.com" +copyright: "2023 Natty" + +description: Please see the README at + +dependencies: +- base >= 4.7 && < 5 +- megaparsec +- containers +- text +- pretty-simple +- extra + +ghc-options: +- -Wall +- -Wcompat +- -Widentities +- -Wincomplete-record-updates +- -Wincomplete-uni-patterns +- -Wmissing-export-lists +- -Wmissing-home-modules +- -Wpartial-fields +- -Wredundant-constraints + +executables: + day03a: + main: Main.hs + source-dirs: src + ghc-options: + - -threaded + - -rtsopts + - -with-rtsopts=-N \ No newline at end of file diff --git a/day03a/src/Main.hs b/day03a/src/Main.hs new file mode 100644 index 0000000..eca43a9 --- /dev/null +++ b/day03a/src/Main.hs @@ -0,0 +1,28 @@ +{-# LANGUAGE ViewPatterns #-} + +module Main (main) where + +import Data.Bifunctor (Bifunctor (bimap)) +import Data.Char (isDigit) +import Data.List (singleton, transpose) +import Data.List.Extra (concatUnzip) + +mergeChunks :: [(Char, a)] -> (String, [a]) +mergeChunks = concatUnzip . fmap (bimap singleton singleton) + +numberChunks :: [(Char, a)] -> [(String, [a])] +numberChunks [] = [] +numberChunks ((isDigit -> False, _) : xs) = numberChunks xs +numberChunks items = (\(a, b) -> mergeChunks a : numberChunks b) $ span (isDigit . fst) items + +main :: IO () +main = do + lns <- lines <$> getContents + let len = length lns + let offsetX x = drop (max x 0) . (++ replicate (max x 0) '.') . take len . (replicate (max (negate x) 0) '.' ++) + let offsetY y = drop (max y 0) . (++ replicate (max y 0) (replicate len '.')) . take len . (replicate (max (negate y) 0) (replicate len '.') ++) + let transposed = (transpose <$>) $ ((transpose <$>) <$>) $ transpose $ transpose <$> [[offsetY y $ offsetX x <$> lns | x <- [-1 .. 1]] | y <- [-1 .. 1]] + let zipped = zipWith zip lns transposed + let chunks = concatMap (((\(a, b) -> (read a :: Integer, b)) <$>) . numberChunks) zipped + let filtered = filter (any (any (any (`notElem` ".01234567890"))) . snd) chunks + print . sum $ fst <$> filtered diff --git a/day03a/stack.yaml b/day03a/stack.yaml new file mode 100644 index 0000000..887f541 --- /dev/null +++ b/day03a/stack.yaml @@ -0,0 +1,6 @@ +resolver: lts-21.21 + +packages: +- . + +extra-deps: [] \ No newline at end of file diff --git a/day03a/stack.yaml.lock b/day03a/stack.yaml.lock new file mode 100644 index 0000000..433950c --- /dev/null +++ b/day03a/stack.yaml.lock @@ -0,0 +1,12 @@ +# This file was autogenerated by Stack. +# You should not edit this file by hand. +# For more information, please see the documentation at: +# https://docs.haskellstack.org/en/stable/lock_files + +packages: [] +snapshots: +- completed: + sha256: 7d4b649cf368f9076d8aa049aa44efe58950971d105892734e9957b2a26a2186 + size: 640060 + url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/21/21.yaml + original: lts-21.21 diff --git a/day03a/test.txt b/day03a/test.txt new file mode 100644 index 0000000..624ea4f --- /dev/null +++ b/day03a/test.txt @@ -0,0 +1,10 @@ +467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598.. \ No newline at end of file diff --git a/day03b/.gitignore b/day03b/.gitignore new file mode 100644 index 0000000..c368d45 --- /dev/null +++ b/day03b/.gitignore @@ -0,0 +1,2 @@ +.stack-work/ +*~ \ No newline at end of file diff --git a/day03b/day03b.cabal b/day03b/day03b.cabal new file mode 100644 index 0000000..dbf16a4 --- /dev/null +++ b/day03b/day03b.cabal @@ -0,0 +1,29 @@ +cabal-version: 1.12 + +-- This file has been generated from package.yaml by hpack version 0.35.2. +-- +-- see: https://github.com/sol/hpack + +name: day03b +version: 0.1.0.0 +description: Please see the README at +author: Natty +maintainer: natty.sh.git@gmail.com +copyright: 2023 Natty +build-type: Simple + +executable day03b + main-is: Main.hs + other-modules: + Paths_day03b + hs-source-dirs: + src + ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N + build-depends: + base >=4.7 && <5 + , containers + , extra + , megaparsec + , pretty-simple + , text + default-language: Haskell2010 diff --git a/day03b/input.txt b/day03b/input.txt new file mode 100644 index 0000000..1584486 --- /dev/null +++ b/day03b/input.txt @@ -0,0 +1,140 @@ +...788.............................54.........501...........555.........270.................................521......893.................... +..../..*963........................*..860......................*....53...../.....................52.................&....347........428*522. +............*......41..481+.......462....$..187......678.......420....-....................&115.+...........................+............... +............707....&.........562...........*..........*.....................438....................&877..660....199..145.........71......... +.....210................356..*.........977.68.........38.......835.622.332.....*300.....131.422..............89..*.....+..........$......... +..............14..312......+..926.....*.......529..*............*...*....*.............*......%...310.......*...835..................885.... +...416../467..........................423.....*...143...132..955...356...124.........588..947....*.....512......................134&.*...... +.....................*688...=....../..........194..............................................148........*815.......................785.... +..673/.....957...............103..104............................../..&.....888......408*703.....................@......896..4..526......... +.......628....*..62.......15.....................885.............649.720.............................93........703..........#..*............ +...*....*....222..*.........*795..........%...+...#.....54.310.....................622....916.......=......./.........../.......493......956 +418..*..57........125...141.........965..382..177.......*.....-.......390.....801....*.....&...659....406....912........614.448............. +....926...................*.#.........%.................517......*......%........%.301............$........*.......694......$......&........ +..............$476....167....208..866...............86........818.200........588.......@....%.............534.....................324....... +...571.977...............*...........*551...................................%.....992..172...849.................578........814............. +......*........176.....705...../406...........42....226.........58....................................*621..........*......*....#54......... +........194.....&....................905.567...*...............*..........287...........303.........82.......600.....642....672............. +.........*...................*726......*....*.714...............341..530..+......415.../...............544....@............................. +..795.429...........$.....615........582.240.......*153...#.........-............&.........313..........*...........#.......*............... +..............443....471.......770..............651.....649.....&........*211..............*......548.235..........2.......898.............. +..........916..@..*...............*......966.................869......843......*........744..410..+..................777*....../...728...... +..976........*.....699.......763...893...*...462.......608*..................23.227.....................411.793..........984.683......*..985 +....*.2*....787........258......*.......878.........................923..............................*.....*........................674..... +.241....966...........%.........804...............589....554...307.+....#.505..........&....332...449.190......780........*322..........-540 +.................126.....837.@.........701.......%.......&......&.....581....*371.......753..-....................$....426.....=............ +...........%.....*.......&....264.......*...................127....................562.......................379*...........786...#.-....... +....376...596......................%...511.551.........868....*....................&............224..............992............817.309..... +.......-...............374....-..164........*............./..627...340..274..710...................*.......347.............................. +...535..........195.......#..278............495....................$.......*....@.....805.570......724.....#.........98..................... +...*...579*65.......................400*...........849......764@.........180..........&....+..576.............573...*....................... +...................648.162...../733.....462...........*.................................&......#....484..10$....*...............981......... +....712.....495.....*...$..........................932...............278..478.%728.....798.............*.......483.717@....593.....&........ +...........*.....738.......................474...........772..877*98....*.-.............................556................*................ +...870@..587.676............................*.......569..*............454.................=....832.249........369..264.........298*543...... +.............*....246.608*.305............227......*....429.....57..........889...745......648....*..........*.....*....................502. +............901.....*......*......................55..............*704.264*...&........................543...234....859..$.............#.... +..................693....661.....*641.................909.....800.......................779.811....789.*.................90...954$.......... +637.....60....................607.........112........*..........@.89.....................#.....*...*....601....227..................32...... +...#......+....132........410........%.....-..795*....702..........%..........495.............285.438.............*711..........346...*332.. +......237................&......925..366..........340.........658......+..282.....532...................252.............195....@............ +.13......*95...879............................................*......169..@.........%.....782....%..%..-......202.973...............765..... +................*......601.......864.....942.=195.=284........6.................351..../.#....618..409...........*...................*..965. +......673...../..862.....&.........*.....................................381.........777..................644............612.......852...... +.......*....823...............211+..228......292*....418..290............*.................................*...-89.......................... +...363..526.....77..../413................$......208...............*....80.......835/.........-...*......458..........5*214................. +..................*.&......206..........13.......................35..........338......%.......529..79..........718............&............. +........#..840..710..112..*....308...........&...618......................%.........130...917..................*......739..221.........106.. +.....205...................417....*.....891..634....@.........241.........21..............*.........465......524...8...............*48...... +.265...........*33.....431.......770.......*...........923.......%...........*.......449...61......*..............*.........$...409......... +.....403....202....226...&.............798..713....321................=...337..........*.........128.....826...=.33........655.......843.... +...................*............943..................*....449..576.835.............$...945................@..506.................88....*.... +........143...-.636...920......*...............=...580.......@...*.....729.........839...........................347...................608.. +........@...847.......*........167.546......485.................453....................365.905.643=.........692....*.........795............ +...................997.....919..........586..........374..............................*.....*.........529..*......34....660...&........573.. +..752...853......*............$...407*..............*...................928.......641.988..201...+......*........................$...+.=.... +....*......-..289.857.................403..........992......-.....206..&....../....*.............328.....28.........$..........957.552...... +.....155................506.761..............................902..+........202...578..*.............................700.................351. +.........................*...../...............909%......737..........*267...........907....981..........273............887.&............... +.135.945...............649..........319.......................413..797......468...............@................$.733.....*...155.904........ +........*510..508..964......*248...@.....68...........285.......*.......289....*.626+............76....#.....873.........723......*..800.... +.............*....&.......51.........244..*...754.....*....#.607........*....148.....................800...@...................895.....*.... +..863$....831..........................*.913.*.......326.215.............882...........774..................252....................#...929.. +................9..........438.......652........239.............................111...*......39.........497......./519.............743...... +..746.369...486.*....308......*...........736...*................495...............*...20.../............=..................%...........#... +.....*......*....786....*..224.......5.........205...397..........+.........456....833.........523*.............638......292....399..855.... +...........135.........383...../...%..&.................*....335...........*.......................669.684*.......+............&.........980 +...............974*185........680.987.........114.......117.&......755....596.../450.............-.........571.....................818..*... +..395.....................................823*......645...........*....#..............1...172...405....................*.....766....-..815.. +.....*....769.998........762.325*440.976.........................507..833.........604*...................282......$.926.668.#............... +.....280.*.....*..75...................#......314.....666.................533..........789.......863............138...........494*955....... +.........77.......*.....907....732..............-.....*...224............*....283$......*.............................................348... +.................161.......*.......................106.......@...&........339............634..513.......729..68..........890.746............ +............................551............-...368......&......21.................520............*.......#.....*........*.....+..4.......... +..................*548....&.........843..290.....=......532.......................*.....190.393.404.........240..535..830................... +......721$...............982..........*.............811..........................276......#..........753*...................715.202..375.... +..334..............................111..969.971........*..................199...............207......................792.......*.......*.... +......793-..640...........=...............@....+..763..591....542......................747...*...............220....@.............#....598.. +...........$..........&....723....72.652.........*..............#.....120...............*.....659.......%.....*...@...........435..41....... +....*875..........22..958.........*.............480.....................*............876..............44.....608...305..593...-............. +...........526...#...........329.208...@544..................885*751....756.74...........$139.=.........................*.........557....248 +950..570.....$.........&.......*...................=.......$..................$........-.......173..&801.........504..215................... +........*.......924....658......41....142*........31..937...189.....100...........&....447.840.............................804..654.....%... +.747......481...*..........849&...........836...........#............*...........447.-.....*..........862.....108....343.....*.-....41..91.. +...........*....783...........................................865..41.................154..520....367*.......*......*.....578........#...... +852...43....759.......516..66.....*....422.........978.93-...*.........@425.579..27..................../..751..326.347...................173 +.....*................*...*....204.627..$...........*..........57.287..............*........$755....356..........-.....77................... +..428..342...$335........296......................971..............%......294.968.721.546......................$...........630.............. +................................649...........624..........-835.......-...%.....$........*........384.........18..131............545.654.... +....36.114..106.............853....@.........=.....737...........*....517...414.....784...607.........411........*...........=.............. +226*....*...*..........508.....*....................*.........303................../....-........................887..173....349.179....343. +.....903.....473..............190.-702.734..........24..............734+................198.....191*402..810..........@..................... +.........712.....405......967.................257............822...........664...........................*....+.$...........441............. +....608...*.......*.........#..........796....*...../........................*................800.....747...939.356.45..325.........899..... +......*..707.......629........570..358....*.66.....56......302...............804....+...290*...*....................*...$.......756..&..815. +.....697......................-....*....357....878.........*......@...931/.......*.511.......................958..169.......772*.......+.... +...........777..369..............257.............@.567...334....785............171.........-....292...635...*......................@........ +347*..........*..*../........962....................................................968#...763..%....&....561.....136...327*777....159.708.. +....192....739..831.888.......*....304...................................715.........................................-...................... +...........................658...%.*....................71....762.......*.....&605...509.401...543..970..........761........382............. +.....948.502...520&............188.689...................*......*........538............*.........#........947..@...........*.....530*...... +........*.................................309+.172*744....481.29....@........-716...........................................227.......56.... +...........*.....930.347&........................................111..6.......................578*..........&.188.......304................. +......151...88...*.........697.........111-...546*.....663*249...........482*7..........327.......423....513...*........../.880............. +...../.........309........*....-985.........%....................619...=...................*.................335.....808....*............141 +........740............414............624..10.720*395........91...../..520......164......318...........397..........*.......560............. +........*..........27.................*.................736....*.+.............*....../...................@......344............361.....336. +.333..271..25.............958*839...991.................*.........82..........624.....286...........663.*...535...........383*.....=........ +....*......-..........368.........=....................945....693....692..........769...............-...153...*...$811........277........981 +...667...................*.........687....235.................*.....*......210.......*.834.....$.............73...........&.......71........ +............-.....129....647.300..........................%....568...611..........262...*......742...............-213..754.....73.*....208.. +..9.......19....................=.129........719$.....795..82...........................375...................................*....235.+.... +...*...............................*...348............*.............260...638........................479........165....151...846.-.......... +..257.524.......695.......787...403...*.......336......63....788......%......*.162..%.......................854*.......*.........492........ +.........*377...../......$..........852...681....*............/.........923.......=.198..996.718.....809............443...............415... +....267..............%.......................*...533......617...724........#...$...........&...&.......@.......700...........%418.....*..... +145*............401..447.....634....&..819....9................*.......362.....241.....102..............................412.........929.14.. +.........642/...*.............*....324....*....................394...#....+.........................661...................*.773.........*... +.....165........600.........492............253.918.......537.......140..+........*...................*.................773..*.....=.....433. +.....%.....10...................................*..83......$...........241.......576........*417....969...........852&.....848.821.......... +.........../...153...........25*317......628.578....*....*.........*17...................227........................................995..... +.....889*.........*.459..697............*........304......852...788....630....720*594............$...............910.801..296...939....*.... +223......235...607....*.*....802..249...233...............................$..............177/.734........828........*............/......830. +....................439..944..*...*...*...............=..482..&...............839....................349...*...$........./...........$...... +..................%...........326....3.66..........628....*...350......544-...........763*.......83.......142..792....974....#....663....... +...$...*....729.916...........................916......221................................689...............................456.........62.. +..621..436....*......#520..808........670....$......*..........121=...............780.........*844..........=..........827%......338...*.... +.............24............*............*........601.346...621......531.....452......*.....659.......-297..697..468................*.936.... +........265............905..64.&...589..960..................*.......*..........62..19.........152................*.......+.....243......962 +...........*..220......*.......757.#..............-270..697..588....461..263......*.......@373....*........464...244...688..............*... +.........272..........993.536..............................*............&......961....=..........490..198.....*................181.....236.. +......$....................*..406......................*....238..94..74............349......723............646..328............*............ +.......139......./...........*..........624.........996.784.....*...-.........61.............+........*........*............981...557..66... +..............313...776*....228...........*...749...........246.579...240.230..*.=611..........*....917..................+.......*.......... +.703..730.........................*961.764.......*......979*.........*....*...........=.....558.906...........58.875$..559.*274.549..819.... +...*..*.......637..............853............821.............65*676.47...272..=213....45.................321*.............................. +.661...964..............540.......................=....-.955............+...........$...........143.907..................1...10............. +...........660...........*..........109$.......415..353.....*...........572......878....77.........*.....................$.....*131..182/... +...........*.....963.....395..............871*............$..994...336.................................319.....88.620....................... +.......&...625......*.........7*121...........494......=...8......*....@..............................*..........*......998*973.......$..... +....691............614...795..........152............120...........238..496...........................477..........................994...... diff --git a/day03b/package.yaml b/day03b/package.yaml new file mode 100644 index 0000000..0daf63a --- /dev/null +++ b/day03b/package.yaml @@ -0,0 +1,35 @@ +name: day03b +version: 0.1.0.0 +author: "Natty" +maintainer: "natty.sh.git@gmail.com" +copyright: "2023 Natty" + +description: Please see the README at + +dependencies: +- base >= 4.7 && < 5 +- megaparsec +- containers +- text +- pretty-simple +- extra + +ghc-options: +- -Wall +- -Wcompat +- -Widentities +- -Wincomplete-record-updates +- -Wincomplete-uni-patterns +- -Wmissing-export-lists +- -Wmissing-home-modules +- -Wpartial-fields +- -Wredundant-constraints + +executables: + day03b: + main: Main.hs + source-dirs: src + ghc-options: + - -threaded + - -rtsopts + - -with-rtsopts=-N \ No newline at end of file diff --git a/day03b/src/Main.hs b/day03b/src/Main.hs new file mode 100644 index 0000000..143869a --- /dev/null +++ b/day03b/src/Main.hs @@ -0,0 +1,25 @@ +{-# LANGUAGE ViewPatterns #-} + +module Main (main) where + +import Data.Char (isDigit) +import Data.List (elemIndices) + +numberChunks :: [Char] -> [(Integer, Int, Int)] +numberChunks [] = [] +numberChunks ((isDigit -> False) : xs) = (\(s, i, l) -> (s, i + 1, l)) <$> numberChunks xs +numberChunks items = (\(a, b) -> (read a :: Integer, 0, length a) : ((\(s, i, l) -> (s, i + length a, l)) <$> numberChunks b)) $ span isDigit items + +isTouching :: (Int, Int) -> (Int, (Integer, Int, Int)) -> Bool +isTouching (y, x) (cy, (_, cx, clen)) = abs (y - cy) <= 1 && any ((<= 1) . abs . (x -)) [cx .. cx + clen - 1] + +fstOf3 :: (a, b, c) -> a +fstOf3 (a, _, _) = a + +main :: IO () +main = do + lns <- lines <$> getContents + let chunks = concat [(,) i <$> it | (i, it) <- zip [0 ..] (numberChunks <$> lns)] + let asterisks = concat [(,) i <$> it | (i, it) <- zip [0 ..] $ elemIndices '*' <$> lns] + let gearRatios = ((fstOf3 . snd <$>) <$>) . filter ((== 2) . length) $ (`filter` chunks) . isTouching <$> asterisks + print $ sum $ product <$> gearRatios \ No newline at end of file diff --git a/day03b/stack.yaml b/day03b/stack.yaml new file mode 100644 index 0000000..887f541 --- /dev/null +++ b/day03b/stack.yaml @@ -0,0 +1,6 @@ +resolver: lts-21.21 + +packages: +- . + +extra-deps: [] \ No newline at end of file diff --git a/day03b/stack.yaml.lock b/day03b/stack.yaml.lock new file mode 100644 index 0000000..433950c --- /dev/null +++ b/day03b/stack.yaml.lock @@ -0,0 +1,12 @@ +# This file was autogenerated by Stack. +# You should not edit this file by hand. +# For more information, please see the documentation at: +# https://docs.haskellstack.org/en/stable/lock_files + +packages: [] +snapshots: +- completed: + sha256: 7d4b649cf368f9076d8aa049aa44efe58950971d105892734e9957b2a26a2186 + size: 640060 + url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/21/21.yaml + original: lts-21.21 diff --git a/day03b/test.txt b/day03b/test.txt new file mode 100644 index 0000000..624ea4f --- /dev/null +++ b/day03b/test.txt @@ -0,0 +1,10 @@ +467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598.. \ No newline at end of file