solve: day 2, part 1
This commit is contained in:
parent
95d2a1392c
commit
59293af2d7
4 changed files with 1044 additions and 3 deletions
5
2020/02/README.md
Normal file
5
2020/02/README.md
Normal 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
1000
2020/02/input.txt
Normal file
File diff suppressed because it is too large
Load diff
35
2020/02/part1.hs
Executable file
35
2020/02/part1.hs
Executable 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
|
|
@ -12,13 +12,14 @@ asdf install
|
||||||
|
|
||||||
## Table of Contents
|
## Table of Contents
|
||||||
|
|
||||||
| 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 | | |
|
||||||
|
|
||||||
[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
|
||||||
|
|
Loading…
Reference in a new issue