diff --git a/day-14-haskell/.gitignore b/day-14-haskell/.gitignore new file mode 100644 index 0000000..c368d45 --- /dev/null +++ b/day-14-haskell/.gitignore @@ -0,0 +1,2 @@ +.stack-work/ +*~ \ No newline at end of file diff --git a/day-14-haskell/Setup.hs b/day-14-haskell/Setup.hs new file mode 100644 index 0000000..9a994af --- /dev/null +++ b/day-14-haskell/Setup.hs @@ -0,0 +1,2 @@ +import Distribution.Simple +main = defaultMain diff --git a/day-14-haskell/app/Main.hs b/day-14-haskell/app/Main.hs new file mode 100644 index 0000000..a7cfe06 --- /dev/null +++ b/day-14-haskell/app/Main.hs @@ -0,0 +1,18 @@ +module Main (main) where + +import System.Environment.Blank ( getArgs ) +import Lib (task1, task2) + +main :: IO () +main = do + args <- getArgs + case args of + ["1", file] -> do + input <- readFile file + result <- task1 input + print result + ["2", file] -> do + input <- readFile file + result <- task2 input + print result + _ -> error "Usage: <1|2> " diff --git a/day-14-haskell/day14-haskell.cabal b/day-14-haskell/day14-haskell.cabal new file mode 100644 index 0000000..cef3817 --- /dev/null +++ b/day-14-haskell/day14-haskell.cabal @@ -0,0 +1,52 @@ +cabal-version: 2.2 + +-- This file has been generated from package.yaml by hpack version 0.37.0. +-- +-- see: https://github.com/sol/hpack + +name: day14-haskell +version: 0.1.0.0 +homepage: https://github.com/githubuser/day14-haskell#readme +bug-reports: https://github.com/githubuser/day14-haskell/issues +author: Author name here +maintainer: example@example.com +copyright: 2024 Author name here +license: BSD-3-Clause +build-type: Simple + +source-repository head + type: git + location: https://github.com/githubuser/day14-haskell + +library + exposed-modules: + Lib + hs-source-dirs: + src + ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints + build-depends: + base >=4.7 && <5 + , containers + , flow + default-language: Haskell2010 + +executable day14-haskell-exe + main-is: Main.hs + hs-source-dirs: + app + ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N + build-depends: + base >=4.7 && <5 + , day14-haskell + default-language: Haskell2010 + +test-suite day14-haskell-test + type: exitcode-stdio-1.0 + main-is: Spec.hs + hs-source-dirs: + test + ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N + build-depends: + base >=4.7 && <5 + , day14-haskell + default-language: Haskell2010 diff --git a/day-14-haskell/package.yaml b/day-14-haskell/package.yaml new file mode 100644 index 0000000..4202a33 --- /dev/null +++ b/day-14-haskell/package.yaml @@ -0,0 +1,49 @@ +name: day14-haskell +version: 0.1.0.0 +github: "githubuser/day14-haskell" +license: BSD-3-Clause +author: "Author name here" +maintainer: "example@example.com" +copyright: "2024 Author name here" +spec-version: 0.36.0 + +dependencies: +- base >= 4.7 && < 5 + +ghc-options: +- -Wall +- -Wcompat +- -Widentities +- -Wincomplete-record-updates +- -Wincomplete-uni-patterns +- -Wmissing-home-modules +- -Wpartial-fields +- -Wredundant-constraints + +library: + source-dirs: src + dependencies: + - flow + - containers + +executables: + day14-haskell-exe: + main: Main.hs + source-dirs: app + ghc-options: + - -threaded + - -rtsopts + - -with-rtsopts=-N + dependencies: + - day14-haskell + +tests: + day14-haskell-test: + main: Spec.hs + source-dirs: test + ghc-options: + - -threaded + - -rtsopts + - -with-rtsopts=-N + dependencies: + - day14-haskell diff --git a/day-14-haskell/src/Lib.hs b/day-14-haskell/src/Lib.hs new file mode 100644 index 0000000..4f814a2 --- /dev/null +++ b/day-14-haskell/src/Lib.hs @@ -0,0 +1,138 @@ +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE TupleSections #-} + +module Lib where + +import Control.Monad (join) +import Data.Char +import qualified Data.Map.Strict as Map +import Data.Maybe (isJust) +import Text.ParserCombinators.ReadP +import Flow + +data Vec2 = Vec2 Integer Integer + deriving (Show, Eq, Ord) + +vAdd :: Vec2 -> Vec2 -> Vec2 +vAdd (Vec2 aX aY) (Vec2 bX bY) = Vec2 (aX + bX) (aY + bY) + +data Robot = Robot { rPos :: Vec2, rVel :: Vec2 } + deriving (Show) +type BotCountMap = Map.Map Vec2 Int + +task1 :: String -> IO Int +task1 input = + let area = Vec2 101 103 + robots = parseFile input + simulated = simulate area 100 robots + sorted = sortIntoQuadrants area simulated + in return $ product $ map length sorted + +task2 :: String -> IO Int +task2 input = + let area = Vec2 101 103 + robots = parseFile input + in step area 1 robots + where + step :: Vec2 -> Int -> [Robot] -> IO Int + step area counter robots = + let simulated = simulate area 1 robots + in if checkForImage simulated + then do + putStrLn $ showWorld area simulated + return counter + else step area (counter+1) simulated + + checkForImage :: [Robot] -> Bool + checkForImage robots = + let botMap = botsAtPos robots + in Map.keys botMap |> filter (hasAdjacent botMap) |> length |> (<) (Map.size botMap `div` 2) + + hasAdjacent :: BotCountMap -> Vec2 -> Bool + hasAdjacent botMap pos = + map (vAdd pos) [Vec2 (-1) 0, Vec2 0 (-1), Vec2 1 0, Vec2 0 1] |> any (\p -> Map.member p botMap) + +showWorld :: Vec2 -> [Robot] -> String +showWorld (Vec2 aX aY) robots = + let botMap = botsAtPos robots + in [0..(aY - 1)] |> map (line botMap) |> join + where + line :: BotCountMap -> Integer -> String + line botMap y = join [charFor (Vec2 x y) | x <- [0..(aX-1)]] ++ "\n" + where + charFor :: Vec2 -> String + charFor pos = case botMap Map.!? pos of + Nothing -> "." + Just num -> show num + +botsAtPos :: [Robot] -> BotCountMap +botsAtPos robots = map ((,1) . rPos) robots |> Map.fromListWith (+) + +sortIntoQuadrants :: Vec2 -> [Robot] -> [[Robot]] +sortIntoQuadrants (Vec2 aX aY) robots = map (\r -> (quadrant r, [r])) robots |> filter (\(q, _) -> isJust q) |> Map.fromListWith (++) |> Map.elems + where + quadrant :: Robot -> Maybe Int + quadrant Robot { rPos = (Vec2 pX pY) } = do + xOffset <- qOffset aX pX + yOffset <- fmap ((*) 2) $ qOffset aY pY + return $ xOffset + yOffset + + qOffset :: Integer -> Integer -> Maybe Int + qOffset a p = case a `divMod` 2 of + (half, 0) -> if p < half then Just 0 else Just 1 + (half, _) -> oddOffset + where + oddOffset + | p < half = Just 0 + | p > half = Just 1 + | otherwise = Nothing + +simulate :: Vec2 -> Integer -> [Robot] -> [Robot] +simulate (Vec2 aX aY) seconds robots = map simulateRobot robots + where + simulateRobot :: Robot -> Robot + simulateRobot Robot { rPos = (Vec2 pX pY), rVel = vel@(Vec2 vX vY) } = + let newX = simulateCoord aX vX pX + newY = simulateCoord aY vY pY + in Robot { rPos = (Vec2 newX newY), rVel = vel } + simulateCoord :: Integer -> Integer -> Integer -> Integer + simulateCoord a v p = (p + v * seconds) `mod` a + +parseFile :: String -> [Robot] +parseFile input = case readP_to_S parse input of + [] -> error $ "Failed to parse input: " ++ input + ((rs,_):_) -> rs + where + parse :: ReadP [Robot] + parse = do + robots <- parseRobots + _ <- eof + return robots + +parseRobots :: ReadP [Robot] +parseRobots = many1 parseRobot + +parseRobot :: ReadP Robot +parseRobot = do + _ <- string "p=" + pos <- parseVec + _ <- string " v=" + vel <- parseVec + _ <- munch1 isNewLine + return $ Robot { rPos = pos, rVel = vel } + +parseVec :: ReadP Vec2 +parseVec = do + x <- parseNumber + _ <- string "," + y <- parseNumber + return $ Vec2 x y + +parseNumber :: ReadP Integer +parseNumber = do + sign <- option ' ' $ satisfy ((==) '-') + digits <- munch1 isNumber + return $ read $ sign : digits + +isNewLine :: Char -> Bool +isNewLine = (==) '\n' diff --git a/day-14-haskell/stack.yaml b/day-14-haskell/stack.yaml new file mode 100644 index 0000000..1620b4f --- /dev/null +++ b/day-14-haskell/stack.yaml @@ -0,0 +1,67 @@ +# This file was automatically generated by 'stack init' +# +# Some commonly used options have been documented as comments in this file. +# For advanced use and comprehensive documentation of the format, please see: +# https://docs.haskellstack.org/en/stable/yaml_configuration/ + +# A 'specific' Stackage snapshot or a compiler version. +# A snapshot resolver dictates the compiler version and the set of packages +# to be used for project dependencies. For example: +# +# snapshot: lts-22.28 +# snapshot: nightly-2024-07-05 +# snapshot: ghc-9.6.6 +# +# The location of a snapshot can be provided as a file or url. Stack assumes +# a snapshot provided as a file might change, whereas a url resource does not. +# +# snapshot: ./custom-snapshot.yaml +# snapshot: https://example.com/snapshots/2024-01-01.yaml +snapshot: + url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/23/0.yaml + +# User packages to be built. +# Various formats can be used as shown in the example below. +# +# packages: +# - some-directory +# - https://example.com/foo/bar/baz-0.0.2.tar.gz +# subdirs: +# - auto-update +# - wai +packages: +- . +# Dependency packages to be pulled from upstream that are not in the snapshot. +# These entries can reference officially published versions as well as +# forks / in-progress versions pinned to a git hash. For example: +# +# extra-deps: +# - acme-missiles-0.3 +# - git: https://github.com/commercialhaskell/stack.git +# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a +# +# extra-deps: [] + +# Override default flag values for project packages and extra-deps +# flags: {} + +# Extra package databases containing global packages +# extra-package-dbs: [] + +# Control whether we use the GHC we find on the path +# system-ghc: true +# +# Require a specific version of Stack, using version ranges +# require-stack-version: -any # Default +# require-stack-version: ">=3.1" +# +# Override the architecture used by Stack, especially useful on Windows +# arch: i386 +# arch: x86_64 +# +# Extra directories used by Stack for building +# extra-include-dirs: [/path/to/dir] +# extra-lib-dirs: [/path/to/dir] +# +# Allow a newer minor version of GHC than the snapshot specifies +# compiler-check: newer-minor diff --git a/day-14-haskell/stack.yaml.lock b/day-14-haskell/stack.yaml.lock new file mode 100644 index 0000000..a4edf3e --- /dev/null +++ b/day-14-haskell/stack.yaml.lock @@ -0,0 +1,13 @@ +# 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: 9444fadfa30b67a93080254d53872478c087592ad64443e47c546cdcd13149ae + size: 678857 + url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/23/0.yaml + original: + url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/23/0.yaml diff --git a/day-14-haskell/test/Spec.hs b/day-14-haskell/test/Spec.hs new file mode 100644 index 0000000..cd4753f --- /dev/null +++ b/day-14-haskell/test/Spec.hs @@ -0,0 +1,2 @@ +main :: IO () +main = putStrLn "Test suite not yet implemented"