This commit is contained in:
Joey Eamigh
2024-12-03 20:09:32 -05:00
parent 7ca9b1d3f6
commit b606826561
15 changed files with 1285 additions and 3 deletions

33
day2/app/Pt1.hs Normal file
View File

@@ -0,0 +1,33 @@
module Main (main) where
import Text.Printf (printf)
main :: IO ()
main = do
inputs <- readFile "./inputs/day2"
let columns = map (map (read :: String -> Int) . words) (lines inputs)
printf "safe: %d\n" $ length $ filter safe columns
safe :: (Ord a, Num a) => [a] -> Bool
safe x = distance x && ascOrDesc x
distance :: (Ord a, Num a) => [a] -> Bool
distance [] = True
distance [_] = True
distance (x : y : xs) = abs (x - y) <= 3 && abs (x - y) >= 1 && distance (y : xs)
ascOrDesc :: (Ord a, Num a) => [a] -> Bool
ascOrDesc [] = False
ascOrDesc [_] = False
ascOrDesc (x : y : xs) = (x < y && asc (x : y : xs)) || (x > y && desc (x : y : xs))
asc :: (Ord a, Num a) => [a] -> Bool
asc [] = True
asc [_] = True
asc (x : y : xs) = x < y && asc (y : xs)
desc :: (Ord a, Num a) => [a] -> Bool
desc [] = True
desc [_] = True
desc (x : y : xs) = x > y && desc (y : xs)

45
day2/app/Pt2.hs Normal file
View File

@@ -0,0 +1,45 @@
module Main (main) where
import Data.List (delete)
import Text.Printf (printf)
main :: IO ()
main = do
inputs <- readFile "./inputs/day2"
let columns = map (map (read :: String -> Int) . words) (lines inputs)
-- print $ filter safe columns
printf "safe w damper: %d\n" $ length $ filter safe columns
safe :: (Ord a, Num a) => [a] -> Bool
safe x = distance x x x && ascOrDesc x
distance :: (Ord a, Num a) => [a] -> [a] -> [a] -> Bool
distance full arr (x : y : xs)
| length arr < length full - 1 = False
| abs (x - y) <= 3 && abs (x - y) >= 1 = distance full arr (y : xs)
| otherwise = distance full (delete y arr) (x : xs)
distance full arr [_] = length arr >= length full - 1
distance _ _ _ = False
ascOrDesc :: (Ord a, Num a) => [a] -> Bool
ascOrDesc (x : y : xs)
| x < y = asc (x : y : xs) (x : y : xs) (x : y : xs)
| x > y = desc (x : y : xs) (x : y : xs) (x : y : xs)
ascOrDesc _ = False
asc :: (Ord a, Num a) => [a] -> [a] -> [a] -> Bool
asc full arr (x : y : xs)
| length arr < length full - 1 = False
| x < y = asc full arr (y : xs)
| x >= y = asc full (delete y arr) (x : xs)
asc full arr [_] = length arr >= length full - 1
asc _ _ _ = False
desc :: (Ord a, Num a) => [a] -> [a] -> [a] -> Bool
desc full arr (x : y : xs)
| length arr < length full - 1 = False
| x > y = desc full arr (y : xs)
| x <= y = desc full (delete y arr) (x : xs)
desc full arr [_] = length arr >= length full - 1
desc _ _ _ = False