Day 2
This commit is contained in:
parent
619bbd0770
commit
3e425b56f5
@ -7,3 +7,4 @@ My solutions to advent of code problems in 2024
|
||||
Below are a list of the programming languages used to solve each day:
|
||||
|
||||
Day 1 - Haskell
|
||||
Day 2 - Haskell
|
||||
|
24
day2/Lib.hs
Normal file
24
day2/Lib.hs
Normal file
@ -0,0 +1,24 @@
|
||||
module Lib where
|
||||
|
||||
import Data.List (delete)
|
||||
|
||||
parsedFileString fileString = [[read word :: Int | word <- words line] | line <- lines fileString]
|
||||
|
||||
getDiffs :: [Int] -> [Int]
|
||||
getDiffs report = [xiplus1 - xi | (xiplus1, xi) <- zip report (tail report)]
|
||||
|
||||
checkReport :: [Int] -> Bool
|
||||
checkReport reports =
|
||||
(all (> 0) diffs || all (< 0) diffs)
|
||||
&& all (\x -> let absX = abs x in absX > 0 && absX < 4) diffs
|
||||
where
|
||||
diffs = getDiffs reports
|
||||
|
||||
-- This is a non-ideal brute-force approach but don't have time to improve
|
||||
checkReportDampened :: [Int] -> Bool
|
||||
checkReportDampened reports =
|
||||
checkReport reports
|
||||
|| let reportSubsets = [checkReport (take n reports ++ drop (n + 1) reports) | n <- [0 .. (length reports)]]
|
||||
in or reportSubsets
|
||||
|
||||
getSafeReportCount checkFun = foldr ((+) . fromEnum . checkFun) 0
|
13
day2/README.md
Normal file
13
day2/README.md
Normal file
@ -0,0 +1,13 @@
|
||||
# Day 1
|
||||
|
||||
## Instructions
|
||||
|
||||
From this directory open a terminal and execute:
|
||||
|
||||
```sh
|
||||
runhaskell day2.hs
|
||||
```
|
||||
|
||||
The solution for each section will be printed to the terminal output.
|
||||
|
||||
## Notes
|
11
day2/day2.hs
Normal file
11
day2/day2.hs
Normal file
@ -0,0 +1,11 @@
|
||||
import Lib (checkReport, checkReportDampened, getSafeReportCount, parsedFileString)
|
||||
|
||||
main = do
|
||||
inputs <- readFile "inputs.txt"
|
||||
let parsedInputs = parsedFileString inputs
|
||||
let count = getSafeReportCount checkReport parsedInputs
|
||||
putStrLn "The solution to part 1 is: "
|
||||
print count
|
||||
let count = getSafeReportCount checkReportDampened parsedInputs
|
||||
putStrLn "The solution to part 2 is: "
|
||||
print count
|
1000
day2/inputs.txt
Normal file
1000
day2/inputs.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user