AOC Day 2
This commit is contained in:
parent
130d334729
commit
ac9add9907
4 changed files with 1066 additions and 3 deletions
7
quests/AOC/year2024/quest02-1
Normal file
7
quests/AOC/year2024/quest02-1
Normal file
|
@ -0,0 +1,7 @@
|
|||
7 6 4 2 1
|
||||
1 2 7 8 9
|
||||
9 7 6 2 1
|
||||
1 3 2 4 5
|
||||
8 6 4 4 1
|
||||
1 3 6 7 9
|
||||
5 3 4 5 6
|
1000
quests/AOC/year2024/quest02-2
Normal file
1000
quests/AOC/year2024/quest02-2
Normal file
File diff suppressed because it is too large
Load diff
|
@ -1,7 +1,5 @@
|
|||
module AOC.Year2024.Quest01 where
|
||||
|
||||
import PCC.Lib
|
||||
|
||||
import Data.Array (transpose, zipWith)
|
||||
import Data.Array as Array
|
||||
import Data.CommutativeRing ((*))
|
||||
|
@ -18,6 +16,7 @@ import Data.Tuple (Tuple(..))
|
|||
import Data.Unit (Unit)
|
||||
import Effect (Effect)
|
||||
import Effect.Class.Console (log)
|
||||
import PCC.Lib (parseInt10)
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
-- Write your solutions here
|
||||
|
@ -36,7 +35,7 @@ part2 input = do
|
|||
let Tuple left right = case datums of
|
||||
[left, right] -> Tuple left right
|
||||
_ -> Tuple [] []
|
||||
let result = sum $ map (\n -> n * countMatching (_ == n) right) left
|
||||
let result = sum $ map (\n -> n * Array.length (Array.filter (_ == n) right)) left
|
||||
log $ "Part 2 ==> " <> show result
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
|
57
src/AOC/Year2024/Quest02.purs
Normal file
57
src/AOC/Year2024/Quest02.purs
Normal file
|
@ -0,0 +1,57 @@
|
|||
module AOC.Year2024.Quest02 where
|
||||
|
||||
|
||||
import Data.Array (any, (..), (:))
|
||||
import Data.Array as Array
|
||||
import Data.BooleanAlgebra ((&&))
|
||||
import Data.CommutativeRing ((+))
|
||||
import Data.Eq ((==))
|
||||
import Data.EuclideanRing ((-))
|
||||
import Data.Function (identity, ($), (<<<))
|
||||
import Data.Functor (map)
|
||||
import Data.Maybe (Maybe(..))
|
||||
import Data.Ord ((<=), (>), (>=))
|
||||
import Data.Semigroup ((<>))
|
||||
import Data.Show (show)
|
||||
import Data.String.Utils (lines, words)
|
||||
import Data.Unit (Unit)
|
||||
import Effect (Effect)
|
||||
import Effect.Class.Console (log)
|
||||
import PCC.Lib (groupBySeq, parseInt10)
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
-- Write your solutions here
|
||||
|
||||
part1 :: String -> Effect Unit
|
||||
part1 input = do
|
||||
let datums = Array.length $ Array.filter identity $ map (safe <<< Array.mapMaybe parseInt10 <<< words) $ lines input
|
||||
log $ "Part 1 ==> " <> show datums
|
||||
|
||||
safe :: Array Int -> Boolean
|
||||
safe arr = case Array.uncons arr of
|
||||
Just {head: x, tail} -> case Array.uncons tail of
|
||||
Just {head: y} -> safe' (x > y) arr
|
||||
_ -> false
|
||||
_ -> false
|
||||
|
||||
safe' :: Boolean -> Array Int -> Boolean
|
||||
safe' increasing arr = Array.length (groupBySeq (gradually increasing) arr) == 1
|
||||
|
||||
gradually :: Boolean -> Int -> Int -> Boolean
|
||||
gradually increasing x y = diff >= 1 && diff <= 3
|
||||
where
|
||||
diff = if increasing then x - y else y - x
|
||||
|
||||
part2 :: String -> Effect Unit
|
||||
part2 input = do
|
||||
let datums = Array.length $ Array.filter identity $ map (dampened <<< Array.mapMaybe parseInt10 <<< words) $ lines input
|
||||
log $ "Part 2 ==> " <> show datums
|
||||
|
||||
dampen :: Array Int -> Array (Array Int)
|
||||
dampen arr = arr : map (\i -> Array.take i arr <> Array.drop (i+1) arr) (0 .. (Array.length arr - 1))
|
||||
|
||||
dampened :: Array Int -> Boolean
|
||||
dampened arr = any safe (dampen arr)
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
Loading…
Reference in a new issue