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

46
day2/day2.cabal Normal file
View File

@@ -0,0 +1,46 @@
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: day2
version: 0.1.0.0
description: Please see the README on GitHub at <https://github.com/githubuser/day2#readme>
homepage: https://github.com/githubuser/day2#readme
bug-reports: https://github.com/githubuser/day2/issues
author: Author name here
maintainer: example@example.com
copyright: 2024 Author name here
license: MIT
build-type: Simple
source-repository head
type: git
location: https://github.com/githubuser/day2
executable day2pt1
main-is: Pt1.hs
other-modules:
Paths_day2
autogen-modules:
Paths_day2
hs-source-dirs:
app
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
default-language: Haskell2010
executable day2pt2
main-is: Pt2.hs
other-modules:
Paths_day2
autogen-modules:
Paths_day2
hs-source-dirs:
app
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
default-language: Haskell2010