this post was submitted on 04 Dec 2024
17 points (94.7% liked)

Advent Of Code

920 readers
2 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2024

Solution Threads

M T W T F S S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS
 

Day 4: Ceres Search

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[โ€“] VegOwOtenks 4 points 1 month ago

I struggled a lot more when doing list slices that I would've liked to

Haskell


import Data.List qualified as List

collectDiagonal :: [String] -> Int -> Int -> String
collectDiagonal c y x
        | length c > y && length (c !! y) > x = c !! y !! x : collectDiagonal c (y+1) (x+1)
        | otherwise = []

part1 c = do
        let forwardXMAS  = map (length . filter (List.isPrefixOf "XMAS") . List.tails) $ c
        let backwardXMAS = map (length . filter (List.isPrefixOf "XMAS") . List.tails . reverse) $ c
        let downwardXMAS  = map (length . filter (List.isPrefixOf "XMAS") . List.tails ) . List.transpose $ c
        let upwardXMAS = map (length . filter (List.isPrefixOf "XMAS") . List.tails . reverse ) . List.transpose $ c
        let leftSideDiagonals = map (\ y -> collectDiagonal c y 0) [0..length c]
        let leftTopDiagonals = map (\ x -> collectDiagonal c 0 x) [1..(length . List.head $ c)]
        let leftDiagonals = leftSideDiagonals ++ leftTopDiagonals
        let rightSideDiagonals = map (\ y -> collectDiagonal (map List.reverse c) y 0) [0..length c]
        let rightTopDiagonals = map (\ x -> collectDiagonal (map List.reverse c) 0 x) [1..(length . List.head $ c)]
        let rightDiagonals = rightSideDiagonals ++ rightTopDiagonals
        let diagonals = leftDiagonals ++ rightDiagonals

        let diagonalXMAS = map (length . filter (List.isPrefixOf "XMAS") . List.tails) $ diagonals
        let reverseDiagonalXMAS = map (length . filter (List.isPrefixOf "XMAS") . List.tails . reverse) $ diagonals

        print . sum $ [sum forwardXMAS, sum backwardXMAS, sum downwardXMAS, sum upwardXMAS, sum diagonalXMAS, sum reverseDiagonalXMAS]
        return ()

getBlock h w c y x = map (take w . drop x) . take h . drop y $ c

isXBlock b = do
        let diagonal1 = collectDiagonal b 0 0
        let diagonal2 = collectDiagonal (map List.reverse b) 0 0

        diagonal1 `elem` ["SAM", "MAS"] && diagonal2 `elem` ["SAM", "MAS"]

part2 c = do
        
        let lineBlocks = List.map (getBlock 3 3 c) [0..length c - 1]
        let groupedBlocks = List.map (flip List.map [0..(length . head $ c) - 1]) lineBlocks

        print . sum . map (length . filter isXBlock) $ groupedBlocks

        return ()

main = do
        c <- lines <$> getContents

        part1 c
        part2 c

        return ()