Solve 2023-03
This commit is contained in:
parent
1311230bbb
commit
ea76aecceb
7 changed files with 251 additions and 5 deletions
|
|
@ -63,7 +63,8 @@ library
|
|||
DerivingStrategies
|
||||
ghc-options: -Weverything -Wno-implicit-prelude -Wno-missing-export-lists -Wno-missing-import-lists -Wno-missing-kind-signatures -Wno-missed-specialisations -Wno-all-missed-specialisations -Wno-unsafe -Wno-safe -Wno-missing-safe-haskell-mode -Wno-missing-local-signatures -Wno-monomorphism-restriction -Wno-prepositive-qualified-module -fdefer-typed-holes -Wno-unused-packages
|
||||
build-depends:
|
||||
base
|
||||
array
|
||||
, base
|
||||
, containers
|
||||
, megaparsec
|
||||
, relude
|
||||
|
|
@ -89,6 +90,7 @@ executable aoc
|
|||
ghc-options: -Weverything -Wno-implicit-prelude -Wno-missing-export-lists -Wno-missing-import-lists -Wno-missing-kind-signatures -Wno-missed-specialisations -Wno-all-missed-specialisations -Wno-unsafe -Wno-safe -Wno-missing-safe-haskell-mode -Wno-missing-local-signatures -Wno-monomorphism-restriction -Wno-prepositive-qualified-module -fdefer-typed-holes -Wno-unused-packages
|
||||
build-depends:
|
||||
aoc
|
||||
, array
|
||||
, base
|
||||
, containers
|
||||
, megaparsec
|
||||
|
|
@ -117,7 +119,8 @@ test-suite doctest
|
|||
build-tool-depends:
|
||||
doctest:doctest
|
||||
build-depends:
|
||||
base
|
||||
array
|
||||
, base
|
||||
, containers
|
||||
, megaparsec
|
||||
, process
|
||||
|
|
@ -152,6 +155,7 @@ test-suite spec
|
|||
build-depends:
|
||||
QuickCheck
|
||||
, aoc
|
||||
, array
|
||||
, base
|
||||
, containers
|
||||
, hspec
|
||||
|
|
|
|||
10
data/Y2023/D03/example
Normal file
10
data/Y2023/D03/example
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
467..114..
|
||||
...*......
|
||||
..35..633.
|
||||
......#...
|
||||
617*......
|
||||
.....+.58.
|
||||
..592.....
|
||||
......755.
|
||||
...$.*....
|
||||
.664.598..
|
||||
|
|
@ -0,0 +1,140 @@
|
|||
.679.....662....71............................805..........862.680...................................................................687....
|
||||
............*....-..811..........846..855......*.............*..$........230.92@............................=.....................92........
|
||||
..........360..........#....664.....=.*...881...677...934.780.......426.*..........8......654.....*959.....539..........21.........*........
|
||||
.....................+.........*......379..*.........*.........=.........969........*........*.976..............872....*....../....579......
|
||||
.......566......652...809....482.394......492..303.650..../...38....%...............106...385..................#.....793..484.865...........
|
||||
......*...........*................*..347.......*.........220.....349...691...392*..................18..797.......................+.........
|
||||
.....11......890........870.......156............733.................../..........921.....................*......*.............203..........
|
||||
.............#...238.....*...........................188......294./58........408...............677.......778....104.903............%........
|
||||
.411+...828.......*.....706.....249..=.....638...848*...........%...............*948............*.....+......+...........917.......817...346
|
||||
................69.................*..310.*....@..........118*......428=..785...................931...217.....934....475...@.145........*...
|
||||
...844.885.........#66...........587.......74...773...........194......../.........&107................................%...............375..
|
||||
......*....826.190.......498........................960............24........212............................398...........474*925...........
|
||||
...........*...*......../......949.-...213./353.................................@...........853......33*164....+....998...............325...
|
||||
........640..823....957........&...746..*........*777.............134.......264.......904+..=........................*......./........%.....
|
||||
....43...............*....457............325..390...........618.........265....*670............=...#165........909..634.......810...........
|
||||
.....................503.*.....438..904*.............+.........*..563..*............345@......597.........758.*...................*154......
|
||||
...941...................612...@........691.....&...336.......837..*....489...............................*...462.689....995...106..........
|
||||
......*383..824...................911........*..467.................44....../.........&445...$...765.647..464.............&..........226....
|
||||
......................151.....116*........249..................929*.......500..140.........18.......*.........823.....454............*......
|
||||
..................968..@..........................118*.....300.................*....762.........541......59...*....35....*..........8.......
|
||||
.....304.........*.......620..@...573.878................................@...@.273.*......572...............661........527.971*.......259...
|
||||
....*.............83.....%...983.*......*.32..603.....201..............492.166.....339.....*...978$..............373...........340.....*....
|
||||
.761..758...934..................550.148.................................................25......................*.....*236.............624.
|
||||
........&......*...393..62*.................167/..716........$........910.767.......380............873*812.362.115..537......48.............
|
||||
.....#........697..*.......159.....................*.........522..............41.....*.........+............*...........812......13.........
|
||||
....529.............374......../...%.............................143...........*......109.......365..........952...........*....*...........
|
||||
...........122*...............555.937......674....................../...529.774..371.................#.38.........../.......304.750....715..
|
||||
..................873*....819..........165....*..928*345........................*..........77......478..*...440...743...................*...
|
||||
964...........417.....802..*......230..-....509..................573...........219...675....-.865.......13............25.62*4...980..145....
|
||||
....*556........&.............278*.................957.....85#.....*................*............=....#.....442........$.......*............
|
||||
.972.....................541...........91*614..485...............680..792...+........541.325..........486......&..653....#.....247..778.....
|
||||
.......680.................*.980*710............*...494...............*......88...........................825........#.629.............$....
|
||||
.399...*......439.........89...........430*....401.....*660....693......407&...........#883....#265......*.....438..........................
|
||||
........390....*.....388.........21........861............................................................798.-....#........................
|
||||
..417&.......551.102.$..............332............/804.....463*649..188.........992.....754.203....912..........732.490....................
|
||||
...................*................*......214.134......................*805.%.....*.....*....*.....#.....*..........*....302.....934..=....
|
||||
....835.........622..............8...541...*....*..861....442....874..........458...955...985..172.....497.310........341......$....*...644.
|
||||
....*......246......909....320.............893.150....*.....*....*........717................................................833.178........
|
||||
...622....*....309.+.........*.........681..........414......612..678....*...........336.................+.....715..........................
|
||||
...........407.......611.....42...........*159........................................*..97..............355...*..........*954..............
|
||||
.......967.....120..=...............................................................519....+.....382............974...................651...
|
||||
739.......*.....*........155.....539................132*.....702......@....923...............905*.........................971...............
|
||||
....453...149...717.400............*.........618........295.....#..644.......-...786....................*...287...........$.................
|
||||
...#..................*.............810........*.908$............................*......+........168...903.....*.....&.......@..............
|
||||
.................306...483..750.108..........570...................934......523..736..#..889......*.........557.......913....107.852..241...
|
||||
...............................*.......................899............*974..%........42............268...........807..............-...@.....
|
||||
..958.......610............/......................894.........................242.........300...........$............941..150...............
|
||||
...........#....131.907.....924...529.267............=.....-./.....692*505...*......265%............$...95.....538........#.................
|
||||
....137...............$...........*...*.........219.....347..268.............753....................12.....351.................955....875...
|
||||
......*....258.................854..253...15........................506....#.................559........&................&........*.........
|
||||
.....376...*.....892.466..160............*.......$................*....*..593.............../............682....721...750..10......187...540
|
||||
...........866...=..................$...437....994......730....404...703................507...&...........................*....126......@...
|
||||
....300...............301....@...315.....................*......................572........-...793.......309.518.437..182.310.*.....&.......
|
||||
...=.....-176.129*........882............................256...............249..*.....+.....................*.....%..+.........71...76..586.
|
||||
......54..........910............*......734....*..............-.......445.....@.....807.....75.907....477%..................................
|
||||
.....*........................247.855....%...647.....@.....558...407....#....................%...*................................447..129..
|
||||
......322..235.......821..............*...........767...............*35..........824.338.55....232...814................546*582.......*.....
|
||||
..............*........./...........509......270.......-33...673................/.......*..............*......-.....................257.....
|
||||
....793*831....805.871.....701..........503.....$............%.............710......37..../.........897...-....516.....330...371............
|
||||
....................*..324*............$............875........293.........*.........@..510..............332..............*..*....2*558.....
|
||||
.........../......147........373..........%926.........@......-......426...142...............%.......184.........#.....746..................
|
||||
.......589..755.......285............221+.................$.........*.................571.....539......*.....60...644.............333.......
|
||||
258....................*.....499............$.107......510.......257..........583.......@...........408.............................*.......
|
||||
.....................396......*.../..../..493.........................489*380....*........908.711............772..........561......24..559..
|
||||
.............................548.769.630..........504*........893................605.........*.........*..10................*..653.....*....
|
||||
........=418..........*576................-..............815.....*.389......481.........*.......859.180....................137..+....275....
|
||||
..77...........551.901.....535............683.............../.638............./......671.908.....@......360.....611....136..................
|
||||
...&...........*.......722...........................702.............514............................520.$.........*...&.....$315...#...147..
|
||||
.......830............*.............#377........39.....%.......384..*.........748..265......271.............885..882...............45...+...
|
||||
..329.-...........787.311.........................*........115...@..840.........&...*.......*......513..695..........515....................
|
||||
....*......666...*..............256*999......897.651.......*............457.........34..&...933...*......*......................156.........
|
||||
..627.......*....24...........@.........................177...22........*...775........668......685...947.....110................*..........
|
||||
.........172..........295*....540.856..76......................+...337.343.=.............................................18....914..449.....
|
||||
.............239..........163.........*......770....594.........../..............200.....824*513..........146............*..........-.......
|
||||
..............#................841...199......=............317..........97.*..........-.............165.....................................
|
||||
...570...../......................*.......122.......179#..*......138........647......376........65..............735.........................
|
||||
.....@.221..50..350...@....757.....88........*582.........893.....*.....230.................403...$......................865...927....892...
|
||||
..........*........#...130..*......................232.........709....&...........105......*..............926=.......966.*.......*...$......
|
||||
....200...884..............338....*155....822*609....&..84.........935..............#.....481...968.64................+..337.....152...254..
|
||||
.......*.........203............70......................%...................../................*....*........................627............
|
||||
.......491........*......321.......$...-.......282............................512.282........564.625........746*825..#..........*361........
|
||||
...................908...$........335.332.509$....*..../..........216.....-.......*...................617.............896...................
|
||||
......946...299............657..................255...724..../......*.....830..122..........487.........*.....645.................56.66.....
|
||||
..984..@...........@....15*.............*858...............150.....91..................498.........654..969.......+............&.....*......
|
||||
.........104.870.281.........$.......796.......................................................................853.............316..984.....
|
||||
.........*.....#......=...288.............86......376.804...515...............405..265*................................%....................
|
||||
.336....152..........671............545.....&......*.....*..............549...*........37.....-....313.#......110.....578..414*.............
|
||||
..............496.....................*...........536..20........471...*.......397..........437.....*..996...&....18...........200..........
|
||||
.................=..........308.....147...319.12................*.....465..............$992........71..............@........................
|
||||
............712.......*123.=..............#.....*629.......%..............................................#..........153..945...........36..
|
||||
.....+340.....-....850................922...%............812..259*577...#452...-........620.567-.784....985.934.427...@..*.........280......
|
||||
216............................394......*..589.........-.....................191....622./..........*........*.............124........*......
|
||||
......764......................*......663...............165...318.....................*....200.83...896..856................................
|
||||
.....*.......705........751..46...............................-....698....132.......-..81...........................264.....................
|
||||
...538.......*......170....*..............808........166..........+......$........130........428............852.......*.................902.
|
||||
..............803...........938..51....66.*...514.......*430..................................*......@..............=............/469.......
|
||||
.........26@.........90...................473..../..273..............13......656.......242$..829......682...........594...968...............
|
||||
...180............+.........872..131....................176...781...-.......*.....294.............990.............#..........=.@...../......
|
||||
....@..344.........184..........*.............................*.........42..153..#.................*.....577......32...........303..334.....
|
||||
........&..620..................821....172....32.....470....698..........%....................397..301.......................*..........569.
|
||||
.../........./.........325.@.............*...*.........*........$.....*....+......124...200......*.......827....*861........857.876.........
|
||||
..808.................*.....581..............644...3..190.607.175..76.712..724.......$........16.358......*...31.......270.........*915.....
|
||||
...............894...453...............845..........#.......*.....................#......................793..............*...914*..........
|
||||
..................&......................+...............99.596..597*813...958.831......261*95.....922..........&.......537.......302.414...
|
||||
...931*.................................................+..........................394......................65.679..........%.........*.....
|
||||
.......839....644......./262......461...424.....682........................954.............43*.............*.......778..*....464......822...
|
||||
.............#.......................=..*...........706....289*.....459......*..........-.....234........11..$.......@..373.................
|
||||
.............../620...536+....100*......907.....159..+.........998.*..........832.....806...........695.....516....................126......
|
||||
..................................268.............*....893.........12.86.................................................*..................
|
||||
......+....355.........998..630..............$.....815...*..............*.....168...554...........735............+......861....490..........
|
||||
.....176...*....513....%....*.................120......568....*314.404+.325..........*..662...855.....495.........467..........*............
|
||||
..........132......*......677..708.................424.....715.....................520..*..................................36.410...........
|
||||
..419...........454..............#...730...401&....*.....#............696..............267.=802...................761......*................
|
||||
............394.........................*........650...637....695......*.....................................586....-...735..154.....423*344
|
||||
........458..%.....*...728..+..........488.435......................241........795.552.....9.&...............................#..............
|
||||
...........*....86.981....*..633.........../................762.700.....70*.........../....*.837......-.........20..................%.......
|
||||
.519...+.652...*........614.......+....................924....*............663..591......192.....952.25.........*....600....#.*239..381.....
|
||||
...*.366.......845.512......@...200...-....280............*...207............................297...*......828...249.*.....408...............
|
||||
.189......5........*.....353........245.......#...153..680..............295..50...............+....296.../...........906.............+......
|
||||
......741*.........466....................710....=.........561*........*..............831............................................909....
|
||||
...........+.................421............*..........+.......661..103...............................922..752..................431.........
|
||||
181......218.707....*....273....*..130.$273.980.........656..............517..712..578.......&.524.....@....*.....996..............*....#709
|
||||
.................442.432..*....64.*......................................../..*....#.......534....*........498.............727.....434......
|
||||
233*559....................869.....316../..........&.....502..+...884.731.....503................239...499.....94..........*................
|
||||
........352.......231...................340.....177..584.....922.....*....57...........................*.........*349...222...212&...433.520
|
||||
...........-.........*981..479..637.....................*969.....*.........=..$.........601...=826...312....................................
|
||||
..............205.................*../...@.....213.............41.64..........80....889*.............................@....76.=702...........
|
||||
....353.......*..................114..97.867...*.....542.........................................721...395............816..............*112.
|
||||
....*.......367.*............478.............42..../..........947.................................*......@......956...........+...883.......
|
||||
.....870........460............*...................26...........*...........960......212...479....422...........*.......780..595..*.........
|
||||
....................978*368....921..760=..................416...665..$.........$.381*.......*............933.744....=..*...........792......
|
||||
....52..........300..........................................*........293.162%........................@...*........259..283.................
|
||||
......%..........%........119.......575.......#..%......./.748......................945...212&.....595.....248.286...........839..106/......
|
||||
..940........$...........%............%....880....539.659.............*.........353*.....................=.....*................*.......188.
|
||||
.....*....254...............794..550...............................701.797............500#................34..475...507..........763........
|
||||
..649............58..691&.....%............................................386..............@.$44.101.....................997..*............
|
||||
.........255............................376....732....-..............869......$...........473........*376....220.........@.....721...443....
|
||||
...........@.913.....168....=909..431......=......@..976.......+.......*..........155............................620.......250......@.......
|
||||
......806.....*....................*...........@................45.....475...724..*......&45.........+202..-576.....*.........*.............
|
||||
...............383...........................372..................................474...................................432.471......729....
|
||||
|
|
@ -19,6 +19,7 @@ dependencies:
|
|||
mixin:
|
||||
- (Relude as Prelude, Relude, Relude.Unsafe, Relude.Extra.Enum)
|
||||
- containers
|
||||
- array
|
||||
- text
|
||||
- megaparsec
|
||||
|
||||
|
|
|
|||
|
|
@ -29,4 +29,4 @@ handleError e = do
|
|||
hPutStrLn stderr $ "I ran into an issue: " <> show e
|
||||
|
||||
defaultMain :: IO ()
|
||||
defaultMain = Exception.catch (runAoC Y2023 D01 >>= print) handleError
|
||||
defaultMain = Exception.catch (runAoC Y2023 D03 >>= print) handleError
|
||||
|
|
|
|||
|
|
@ -1,6 +1,93 @@
|
|||
module AoC.Y2023.D03 (solve) where
|
||||
module AoC.Y2023.D03 where
|
||||
|
||||
import AoC.Riddle
|
||||
import Data.Array
|
||||
import Data.Char (isDigit)
|
||||
import Relude (catMaybes, fromIntegral, readMaybe)
|
||||
import System.Posix.Internals (const_fd_cloexec)
|
||||
|
||||
type Pos = (Int, Int)
|
||||
type Item = (Pos, Char)
|
||||
type Puzzle = Array Pos Char
|
||||
|
||||
toArray :: [[Char]] -> Puzzle
|
||||
toArray xxs =
|
||||
array
|
||||
((1, 1), (h, w))
|
||||
[ ((y, x), c)
|
||||
| (y, xs) <- zip [1 ..] xxs
|
||||
, (x, c) <- zip [1 ..] xs
|
||||
]
|
||||
where
|
||||
w = case xxs of
|
||||
[] -> 0
|
||||
xs : _ -> length xs
|
||||
h = length xxs
|
||||
|
||||
parse :: Text -> Puzzle
|
||||
parse = toArray . fmap toString . lines
|
||||
|
||||
symbols :: Puzzle -> [Item]
|
||||
symbols = filter (isSymbol <$> snd) . assocs
|
||||
where
|
||||
isSymbol :: Char -> Bool
|
||||
isSymbol c = not (isDigit c || c == '.')
|
||||
|
||||
neighborDeltas :: [Pos]
|
||||
neighborDeltas =
|
||||
[ (-1, -1)
|
||||
, (-1, 0)
|
||||
, (-1, 1)
|
||||
, (0, -1)
|
||||
, (0, 1)
|
||||
, (1, -1)
|
||||
, (1, 0)
|
||||
, (1, 1)
|
||||
]
|
||||
|
||||
move :: Pos -> Pos -> Pos
|
||||
move (x, y) (dx, dy) = (x + dx, y + dy)
|
||||
|
||||
item :: Puzzle -> Pos -> Maybe Item
|
||||
item arr p =
|
||||
if inRange grid p
|
||||
then Just (p, arr ! p)
|
||||
else Nothing
|
||||
where
|
||||
grid = bounds arr
|
||||
|
||||
neighbors :: Puzzle -> Pos -> [Item]
|
||||
neighbors arr pos = filter isPartNumber ns
|
||||
where
|
||||
ns = mapMaybe (item arr . move pos) neighborDeltas
|
||||
|
||||
isPartNumber :: Item -> Bool
|
||||
isPartNumber (_, c) = isDigit c
|
||||
|
||||
expand :: Puzzle -> (Char -> Bool) -> Pos -> [Item]
|
||||
expand arr f pos = ls <> rs
|
||||
where
|
||||
ls = reverse . drop 1 $ go pos (0, -1)
|
||||
rs = go pos (0, 1)
|
||||
go p dt = case item arr p of
|
||||
Nothing -> []
|
||||
Just i@(_, x) ->
|
||||
if f x
|
||||
then i : go (move p dt) dt
|
||||
else []
|
||||
|
||||
toPartNumber :: [Item] -> Maybe Integer
|
||||
toPartNumber = readMaybe . fmap snd
|
||||
|
||||
part1 :: Puzzle -> Integer
|
||||
part1 puzzle = sum partNumbers
|
||||
where
|
||||
partNumbers = mapMaybe toPartNumber (ordNub (concatMap (fmap (expand puzzle isDigit . fst) . neighbors puzzle . fst) syms))
|
||||
syms = symbols puzzle
|
||||
|
||||
solve :: (MonadIO m) => Text -> m (Either Text Solution)
|
||||
solve _ = pure $ Left "not yet implemented"
|
||||
solve input = do
|
||||
let
|
||||
puzzle = parse input
|
||||
p1 = part1 puzzle
|
||||
pure $ Right [part1 puzzle]
|
||||
|
|
|
|||
|
|
@ -12,3 +12,7 @@ spec = do
|
|||
describe "Day 2" do
|
||||
it "calculates correctly" do
|
||||
runAoC Y2023 D02 `shouldReturn` Right [2369, 66363]
|
||||
|
||||
describe "Day 3" do
|
||||
it "calculates correctly" do
|
||||
runAoC Y2023 D03 `shouldReturn` Right [528799]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue