1
0
Fork 0

solve: day 2, part 1

This commit is contained in:
Sloane Perrault 2022-09-21 09:19:52 -04:00
parent 95d2a1392c
commit 59293af2d7
4 changed files with 1044 additions and 3 deletions

5
2020/02/README.md Normal file
View file

@ -0,0 +1,5 @@
# Day 2
## Part 1
Spent some time reading up on how to define a custom instance of `Read`. Pretty simple otherwise. Surprised `countElem` isn't in Prelude.

1000
2020/02/input.txt Normal file

File diff suppressed because it is too large Load diff

35
2020/02/part1.hs Executable file
View file

@ -0,0 +1,35 @@
#!/usr/bin/env runghc
import Data.Char
data PasswordEntry = PasswordEntry { min :: Int, max :: Int, character :: Char, password :: String }
deriving (Show)
-- This could be a lot better...
instance Read PasswordEntry where
readsPrec _ input =
let (min', rest) = span isDigit input
min = read min' :: Int
(_, rest') = splitAt 1 rest
(max', rest'') = span isDigit rest'
max = read max' :: Int
(_:char:_:_:password) = rest''
in
[(PasswordEntry min max char password, "")]
main = interact solve
solve = show . length . filter (isValidPasswordEntry . readPasswordEntry) . lines
readPasswordEntry :: String -> PasswordEntry
readPasswordEntry = read
isValidPasswordEntry (PasswordEntry min max char pass) =
let countedElem = countElem char pass
in min <= countedElem && max >= countedElem
countElem _ [] = 0
countElem a (x:xs)
| a == x = 1 + countElem a xs
countElem a (_:xs) = countElem a xs

View file

@ -14,7 +14,7 @@ asdf install
| S | M | T | W | T | F | S | | S | M | T | W | T | F | S |
| :-: | :-: | :-: | :-: | :-: | :-: | :-: | | :-: | :-: | :-: | :-: | :-: | :-: | :-: |
| | | [1] | 2 | 3 | 4 | 5 | | | | [1] | [2] | 3 | 4 | 5 |
| 6 | 7 | 8 | 9 | 10 | 11 | 12 | | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 | 17 | 18 | 19 | | 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| 21 | 22 | 23 | 24 | 25 | | | | 21 | 22 | 23 | 24 | 25 | | |
@ -22,3 +22,4 @@ asdf install
[asdf]: https://asdf-vm.com/#/ [asdf]: https://asdf-vm.com/#/
[haskell]: https://www.haskell.org [haskell]: https://www.haskell.org
[1]: ./01 [1]: ./01
[2]: ./02