From b3a89bb2f720c65da9c9866219a47914c2e3dd06 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Sun, 3 Dec 2023 10:54:11 +0000 Subject: [PATCH] Initial version --- advent-of-code23.cabal | 5 ++ advent03/Main.hs | 103 ++++++++++++++++++++++++++ data/advent03.txt | 140 ++++++++++++++++++++++++++++++++++++ data/advent03a.txt | 10 +++ problems/day03.html | 159 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 417 insertions(+) create mode 100644 advent03/Main.hs create mode 100644 data/advent03.txt create mode 100644 data/advent03a.txt create mode 100644 problems/day03.html diff --git a/advent-of-code23.cabal b/advent-of-code23.cabal index d2f2d16..7316f4c 100644 --- a/advent-of-code23.cabal +++ b/advent-of-code23.cabal @@ -105,4 +105,9 @@ executable advent02 import: common-extensions, build-directives main-is: advent02/Main.hs build-depends: text, attoparsec + +executable advent03 + import: common-extensions, build-directives + main-is: advent03/Main.hs + build-depends: linear, array \ No newline at end of file diff --git a/advent03/Main.hs b/advent03/Main.hs new file mode 100644 index 0000000..f164bad --- /dev/null +++ b/advent03/Main.hs @@ -0,0 +1,103 @@ +-- Writeup at https://work.njae.me.uk/2022/12/14/advent-of-code-2022-day-12/ + +import AoC + + +import Data.Char +import Data.List +import Linear (V2(..), (^+^)) +import Data.Array.IArray + +type Position = V2 Int -- r, c +type Engine = Array Position Char +type NumPos = [Position] + +data NumberSeek = + NumberSeek { inNumber :: Bool + , positions :: NumPos + , foundNumbers :: [NumPos] + } deriving (Show) + + +main :: IO () +main = + do dataFileName <- getDataFileName + text <- readFile dataFileName + let engine = mkEngine text + let allNums = findNumbers engine + let partNums = filter (numTouchingSymbols engine) allNums + -- print engine + -- print $ findNumbers engine + print $ part1 engine partNums + print $ part2 engine partNums + + +part1, part2 :: Engine -> [NumPos] -> Int +part1 engine partNums = sum partNumValues + where partNumValues = map (readNumber engine) partNums + +part2 engine partNums = sum $ fmap product gearRatios + where sts = stars engine + touchingStars = fmap (adjacentNumbers partNums) sts + gears = filter ((==) 2 . length) touchingStars + gearRatios = [[readNumber engine n | n <- pNs] | pNs <- gears] + +mkEngine :: String -> Engine +mkEngine text = grid + where rows = lines text + r = length rows - 1 + c = (length $ head rows) - 1 + grid = listArray ((V2 0 0), (V2 r c)) $ concat rows + +isEngineSymbol :: Char -> Bool +isEngineSymbol c = (not $ isDigit c) && (c /= '.') + +findNumbers :: Engine -> [NumPos] +findNumbers engine = numbers + where ((V2 r1 _), (V2 r2 _)) = bounds engine + rows = [r1..r2] + numbers = concatMap (foundNumbers . (findNumbersInRow engine)) rows + +findNumbersInRow :: Engine -> Int -> NumberSeek +findNumbersInRow engine r + | inNumber finalSeek = NumberSeek False [] ((reverse $ positions finalSeek):foundNumbers finalSeek) + | otherwise = finalSeek + where finalSeek = foldl' (buildNumber engine) + (NumberSeek False [] []) + $ range $ rowBounds engine r + +buildNumber :: Engine -> NumberSeek -> Position -> NumberSeek +buildNumber engine NumberSeek{..} p + | inNumber && isDigit c = NumberSeek True (p:positions) foundNumbers + | inNumber && not (isDigit c) = NumberSeek False [] ((reverse positions):foundNumbers) + | not inNumber && isDigit c = NumberSeek True [p] foundNumbers + | otherwise = NumberSeek False [] foundNumbers + where c = engine ! p + +rowBounds :: Engine -> Int -> (V2 Int, V2 Int) +rowBounds engine r = (V2 r c1, V2 r c2) + where ((V2 _ c1), (V2 _ c2)) = bounds engine + +neighbours :: Position -> [Position] +neighbours p = [p ^+^ V2 dr dc | dr <- [-1..1], dc <- [-1..1] + , (dr, dc) /= (0, 0) ] + +touchingSymbol :: Engine -> Position -> Bool +touchingSymbol engine p = any (isEngineSymbol . (engine !)) nbrs + where nbrs = filter (inRange (bounds engine)) $ neighbours p + +numTouchingSymbols :: Engine -> NumPos -> Bool +numTouchingSymbols engine ps = any (touchingSymbol engine) ps + +readNumber :: Engine -> NumPos -> Int +readNumber engine ps = read $ map (engine !) ps + +stars :: Engine -> [Position] +stars engine = filter ((==) '*' . (engine !)) $ indices engine + +starTouchesNumber :: Position -> NumPos -> Bool +starTouchesNumber star ps = not $ null $ intersect ps halo + where halo = neighbours star + +adjacentNumbers :: [NumPos] -> Position -> [NumPos] +adjacentNumbers nums star = filter (starTouchesNumber star) nums diff --git a/data/advent03.txt b/data/advent03.txt new file mode 100644 index 0000000..4c9a7f3 --- /dev/null +++ b/data/advent03.txt @@ -0,0 +1,140 @@ +48.................501....33.....622..............763.........331.................161.683......................................980.......... +...491.842.....948*..................338.....*......=...........-...309.......633*....*....................*990...706...452......*..+....... +...*...*....................426........*..408.........................*............659...............250.........&.......*.....767...403.... +363.....961...959#.508*223......843.406..........690................%..479.....................398.../....*............458.................. +......=.........................................*....@946........767.........907=.................@....158.850..+670..............10*790.... +.......39.306...679.%113...............665....874....................597...................861.........................840...855............ +.................*.........864.....154*....#..............1*......4..........341..................731*..........783.....#.......*204........ +..919...........706.....*......891........840..%473.........379....*149........-..115..519............668..........*423..................... +...*.................398.190......$.............................#.......=.%530......$..*..............................................475... +.543...@.............................834..729.&.........146......789.189........511...17........455*308..662.............530................ +.....142....437....138*755..&53.....*..........783........./.......................*......................................./....674..315.... +..........-./.......................826...%.....................752........414......480.................276.952...................-...*..... +.......131.......*654..590.................819..........$..........-.347*..................294.....776...&...*........741.....830....230.... +..586%.....968...........+..........319..............628......516............933..........&........+..........600.....*........*............ +............*...................-..*.........................%........................726...876..........772..........99......145......785.. +...........710...............352...842..941...802.....133............474.&235.................*.............*561..........#...........*..... +.......................................=...............*........=...................642.......774......*.........67..@921.374.......165..... +..250........................8..........................968...375........224...#.......*............497.833........-........................ +...$.....................584..*978..107&....471...............................486....408......................................@241.-555..... +......662$........%......&.....................*..#186...550........=309............................76.......399............................ +................281.........808....666.......207.........-...615.................59=....569.....197*...42...%...........572....+231...%..... +....758.$777................%.....*...............433................646.834.............@.............*.........543...................769.. +....*................995...........628...............*..................*..............=..........341...583.112+...*.......74..=341......... +.....775.................................744...278.128.......................126...940.839....=.....*............385.499...*............302. +..........*177...574.............+.......+......*................561#...590.*......$.........231...586....................212...........#... +.......418....................954..433.......484...........526...........*..643......-......................&..778...............701........ +.414..............493.....566.....*......=..................*....236...748..........543...593...*745.......57.........&.........*........... +....*................*.....*....46....659..%.&980..........66....................36...........87...................994.......218...443...... +226.241.893%..........257..312............69........792............/...........+...................$257..................790........./...184 +................................................854*.............909.139..*276.680..&.........432............580....248.................*... +....136*755...@.950......................173.............95..........+...............2.877.......*668......+..+.........918*.......62....571 +............333.*....812...$.428.................*496..................903@...258&.............-........650.................281......+...... +......500.......6....*...719.+......285.......955.....712*12..561.497..............667...&.843..777.................427................@.... +.....&............506..................=..359................#....................*.....24.../.........544.436..........492=........272..... +.........377..........=............41-......+...........229........761...540....391..............*565.....*......449..........665.......#... +.887....*.....906..141.....357...$.................%......*.................#.............-..874......811...9.....*....$113......*.967...628 +......406............../.......637.551....223....162.......251...........................702.*......%......*....&.905.........747........... +....*...........321.....801............=.....-.#...............390........845..12............399...126....283.566.....124*102......&........ +..447..+....935...-..............41.68.87.92...105....178=....*..........*.......*..92..............................................21...... +......505...................................*................614....948..237..512......108.58..162...454@...........395.......5............. +...........454..10.../............101........885.......................*..................*...*....................*........................ +..........%....*....534......114.*................../.95......427.210..674.......371.903......81....................922.....231...346....... +...525........887...........#........162....211..166..@......*.....*.......281......*....................835.635............*.....*.....828. +.....*..............571...............+........-.............933.629./25..*...........977........*............*...........178.....749..*.... +..993.........-................516/................394...56................477....545.*.......251.858......499....435.........&.........170. +.......967..73.....&.....35.........656....842.....*......*........................*...749............%..........#...........622............ +......+...........366.86..*...........#.....=.....162...332.............-..........859............102..488.........................717...... +347.........32........#....393.................*.................252...262....951.......823...................784...358.603.......*.....515. +......917..*.....$..........................642.394..195...*..................*....$......*.448.519................*......*....302..870.*... +...-..*...674.....780.547....287....................*....504...................803.688.831....*..*...........617......./..476........*...9.. +.286..738................#.......-511..11.....581..268.....................439.............523..459...............69..373............887.... +......................*...................555*.........45....638...964.......-...$..........................................88*............. +.........802...+805.77.648.....66.676........................&..../....26......708...+.-981...239%........953..................672.......... +......./.....#.............11.+......*....=...........756.............*.....-......298..................=...*..751.......433.........727.... +....553.......659....907...@......@..530.218.............*......259..821...716.980..........89..851...793.614.......943....*.....961*....... +........*339........$..........373...............................*...............$.....667..$......*..........149.....+..613................ +..987..................*...................670...105.....347....383.605..................@....447...124..........*737...........562..15*445. +........-.....595*7..215.839.781.............*..*...........$.............810....*............*...........350....................*.......... +.........929.............*......*...362@...890...975....#..........2....../...252.576.......78...........+........685.240......695.......... +.................475......598.152........................841.&880..#......................&....693....=.............=.....28........&384.... +............472..*.......................$..320.412*...................304...=.-.........395......=.113....601.............................. +..324.738.........968...................755..*......101.......476..........90..342........................*.....638..182/...........704..139 +.........*..610*......601...................356.../.......633*....971..........................806.....#...240...*...................../.... +........683.....326.....*..@........749.........390.........................163@....800..483..*........792.....94........................... +...600................957..741...../....376............817..899.......194........50.....@....96.368..................@.......+600....-...... +..........338%..........................*...............#....#.........*....304..+....*...........*...................236............284.... +..423........................&763.............947.................371.187..*.......356.441......201.......................285............... +............75................................*....................*......696........................#...............................670.... +...*..........*.........426.......635..-....449.758*.......314.......*..............................246.........*................58..*...... +574.322........937.......*........#...116...........40.......*.@752..400........180....@......918.........195.49......983....559*.....82.... +...........*.........527..842.............................817...................../.270...445..*............*............................... +........622.235..869*..........922*...............125*610..............*459...............&....455...........21.......964................... +...................................465..%542.........................47......272............+..........775.............*.....328.....@...... +.................732*........51.&..........................853.222.........................672....148....+.........572..499.+....*....12.... +....125..............581....*...909.787.......720*263......*....$..881%....623*891..515..........=..................*.........726.78........ +.....%..........715......935.........*....650............874.......................*.........127....................923..................... +................$.............330*..444....$....653............%....345..915.....960.395...........823.706..734.341...............295...342. +.............................................99..-...........465...&......*.............*416......*..........@....*...@......630.....*...... +.....@.........356.......................399.+......+.....................349..................542..............853.375.......=.......132... +....77...=719.@.......*...../662...........*...233...781..............................&687................%................................. +.....................791..................933.*..........931.....884*...........-...................../...112..%601..........857............ +583.....*292...................................438...................424......603.416.........484...104.....................*.......706..... +.............................452.......549.............24...997....................*............*.........767...379..........192............ +........413.................&.....#......*....*.......*.........523.........443.412.....546......859..138*........&..66.576................. +..................../...........348....816.106.391......................447.........../...*..895.......................*................595. +...........#......771.675..................................673............*........176...856..=...627*308..696...............658............ +....489..51...174......%..676......$..........*........332.*.............373.350................-..........*.................*.....*........ +181.*........*...............*822.136..888.642.202.......*..65....958........*........358........510.....*..504..747...993....339..764...... +...........540........................................308...........@.895..942.506......*....791.......166......#...../..../...........668.. +...................452............-....631..............................*........&.....253..../............897......*....602................ +..748....249.24............21...847...&.....194...@...............@...865................................=....*.....550..................... +....*.......*.....658...................573*.......679.....760/..431.................481..........907....130.912...............240...998.... +.605...............*.......25.......#..................................726..646....#..........607..*................698..........*...+...... +...........301&.572........#.....101...................148........55...........*.122........./........953..........-.....602.321.929.....998 +511.870...............@....................808.........*........*............257.......206............@........685..........*............... +.............903...398.....588......114.......*...990...677.....182.10$...........%.........%.................%........................2*... +................*...........*...490...........987....=......803.........98......485.493.868..945.805......-........89............=944....296 +......640.......669.........630..*.......................-....&..582.......341.........*.........*.........939......*....................... +.........*34....................557.....130....265.....21........*...625..$...............*796..690................997...489....293......... +....917.......916............@.................*.................948..&...........148...67..................833........./..........-...943.. +....$........*......849...4$..300..217........58..........................643.170........................................................... +.........511..853......*..............*...........355.............35-.....-....@..636.....=...595..131...496..........................=687.. +............%........&..476......452...435.....%...*.....700..........391........*.....418.........*.........39*.&.......................... +.16...=.............500.....................426...469.....*.............&.....#..522.............939..............395.660................... +...*.696.....*869........995.....413...................864.................991.......598......42.........................@..806..386........ +337.......983.....610........104*.......100*......................-............$79....@...461*....*685...270................*...*........240 +......................207...........311.....741..................337.........*.................381.......*........979.....712.374.879....... +.......@.....554.........*.....485.....................853.............*..276.230...........7...........740...755*..................-....... +....927.../.....#...#...46........*466...........=.................=.531...........593......*..%....................961.....@............... +..........65......294..........................30..339..856.....966...........904..........791..841..........438.......#...747.............. +..419...................614................972.......*...............131.......=.......................716..........21...................... +.....=.79..............................&....*......76.....243..............115.......757...364..870....&...890......+...840..........55..... +....................383....*48...571...124...749............*................-..&.....*......*...*........@.....................201./....... +......752.....470.......+..........*........................964.......452........776...413...9..791...257.......687.....84......*........... +.........*502.*......873..850..573..729....@.....197....................*...798.........................@......*........&.....65...200..357. +...............307.............*..........597.....*...473.....=.......332....*..311..861*399...................472..................*.../... +.820.....................65....754.............713......*.....739...........622...............768/..................................967..... +....*.......180..........*...*.........218...............260......................519...933..........271.....36....132.....147.............. +.935..4......@..254...815..58.689.......*..........................159..............$...*..........*....&...$.......*......../.414*164...... +.......-.388.......%....................284...........833...770...*...........705.....697.......466.239.......332....489.................... +....#.....*...........@.......288............972.........-.*......723.....*....*................................*............=..........592. +...535..........*.267..193.......*..367.....*..............132............509..776.790..@....466..........715.758...*115.....608............ +.............979..*..........8.353.....*...329...@544.913..........................*.....759.*......704..%............................712... +...................52..170...*......459................/...#.......161........813.510.........207......@..........581..................*.... +............#...........*..978...........581.9*815..........450...............*.......................................604.......308...725... +............16.........269............../.............195........217*216......890......................534.....84......-......-............. +....339...........................459.......644..........*.........................636.....113...%154...*....................127.....#348... +349...*.............402...735......@................182.121...134........%255.276...%...&.../............460....#......79................... +....503....22.............#.....$............38.....+...........*....-11.......*......326...../....*853.........216......*.../180.....16.... +..........*......92.............456..619......*..-...........=...103..........190..........353..982......25*........465.209................. +...485.350..........949...994.......*....379..3.22........972.........713.734.........262...........$.......846.......*.............333-.... +....................*..../....*121.729..+............325.............*.......*..........*........374..............+..769.....811............ +......-..............788...488....................................851....679......*......487.....................368.........*........*..... +....612..528...255.................364.....................130.................317..251...............................665.206..972.957.184.. +..........*..../.......337..475*......&..391...347...795....*.........................*......722..666...............@....................... +........823.$.....+.../.........716......*........*....*.....247......329...........697......*.....*....%.....168....624.........592........ +...98*.......916..915......245............277..&..353...719..........$.....................846.601.37...47......................*......519.. +......91..............720..*........$985......976......................834...........461.........*...........................266....#...*... +68....................*....45..............&...........79*888.250*461.*.......%................574..........3*....408..380........383.192... +...........836......383...........557.....672..........................764.....944............................827........................... \ No newline at end of file diff --git a/data/advent03a.txt b/data/advent03a.txt new file mode 100644 index 0000000..624ea4f --- /dev/null +++ b/data/advent03a.txt @@ -0,0 +1,10 @@ +467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598.. \ No newline at end of file diff --git a/problems/day03.html b/problems/day03.html new file mode 100644 index 0000000..6b784bf --- /dev/null +++ b/problems/day03.html @@ -0,0 +1,159 @@ + + + + +Day 3 - Advent of Code 2023 + + + + + + +

Advent of Code

Neil Smith (AoC++) 6*

  {:year 2023}

+ + + +
+

--- Day 3: Gear Ratios ---

You and the Elf eventually reach a gondola lift station; he says the gondola lift will take you up to the water source, but this is as far as he can bring you. You go inside.

+

It doesn't take long to find the gondolas, but there seems to be a problem: they're not moving.

+

"Aaah!"

+

You turn around to see a slightly-greasy Elf with a wrench and a look of surprise. "Sorry, I wasn't expecting anyone! The gondola lift isn't working right now; it'll still be a while before I can fix it." You offer to help.

+

The engineer explains that an engine part seems to be missing from the engine, but nobody can figure out which one. If you can add up all the part numbers in the engine schematic, it should be easy to work out which part is missing.

+

The engine schematic (your puzzle input) consists of a visual representation of the engine. There are lots of numbers and symbols you don't really understand, but apparently any number adjacent to a symbol, even diagonally, is a "part number" and should be included in your sum. (Periods (.) do not count as a symbol.)

+

Here is an example engine schematic:

+
467..114..
+...*......
+..35..633.
+......#...
+617*......
+.....+.58.
+..592.....
+......755.
+...$.*....
+.664.598..
+
+

In this schematic, two numbers are not part numbers because they are not adjacent to a symbol: 114 (top right) and 58 (middle right). Every other number is adjacent to a symbol and so is a part number; their sum is 4361.

+

Of course, the actual engine schematic is much larger. What is the sum of all of the part numbers in the engine schematic?

+
+

Your puzzle answer was 521601.

--- Part Two ---

The engineer finds the missing part and installs it in the engine! As the engine springs to life, you jump in the closest gondola, finally ready to ascend to the water source.

+

You don't seem to be going very fast, though. Maybe something is still wrong? Fortunately, the gondola has a phone labeled "help", so you pick it up and the engineer answers.

+

Before you can explain the situation, she suggests that you look out the window. There stands the engineer, holding a phone in one hand and waving with the other. You're going so slowly that you haven't even left the station. You exit the gondola.

+

The missing part wasn't the only issue - one of the gears in the engine is wrong. A gear is any * symbol that is adjacent to exactly two part numbers. Its gear ratio is the result of multiplying those two numbers together.

+

This time, you need to find the gear ratio of every gear and add them all up so that the engineer can figure out which gear needs to be replaced.

+

Consider the same engine schematic again:

+
467..114..
+...*......
+..35..633.
+......#...
+617*......
+.....+.58.
+..592.....
+......755.
+...$.*....
+.664.598..
+
+

In this schematic, there are two gears. The first is in the top left; it has part numbers 467 and 35, so its gear ratio is 16345. The second gear is in the lower right; its gear ratio is 451490. (The * adjacent to 617 is not a gear because it is only adjacent to one part number.) Adding up all of the gear ratios produces 467835.

+

What is the sum of all of the gear ratios in your engine schematic?

+
+

Your puzzle answer was 80694070.

Both parts of this puzzle are complete! They provide two gold stars: **

+

At this point, you should return to your Advent calendar and try another puzzle.

+

If you still want to see it, you can get your puzzle input.

+

You can also this puzzle.

+
+ + + + + + \ No newline at end of file -- 2.34.1