-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay18.elm
72 lines (53 loc) · 1.43 KB
/
Day18.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
module Day18.Day18 exposing (main)
import Html exposing (Html, div, text)
input : String
input =
"......^.^^.....^^^^^^^^^...^.^..^^.^^^..^.^..^.^^^.^^^^..^^.^.^.....^^^^^..^..^^^..^^.^.^..^^..^^^.."
parsedInput : List Int
parsedInput =
String.toList input
|> List.filterMap parse
parse : Char -> Maybe Int
parse chr =
if chr == '.' then
Just 1
else if chr == '^' then
Just 0
else
Nothing
main : Html msg
main =
div []
[ div [] [ text ("Part 1: " ++ (String.fromInt <| solve 40 0 1 parsedInput)) ]
, div [] [ text ("Part 2: " ++ (String.fromInt <| solve 400000 0 1 parsedInput)) ]
]
solve : Int -> Int -> Int -> List Int -> Int
solve max sum rows current =
let
newSum =
sum + List.sum current
nextRows =
rows + 1
in
if rows == max then
newSum
else
let
nextCurrent =
nextRow [] <| 1 :: current ++ [ 1 ]
in
solve max newSum nextRows nextCurrent
nextRow : List Int -> List Int -> List Int
nextRow result lastRow =
case lastRow of
lft :: _ :: rgt :: _ ->
let
newResult =
if lft == rgt then
result ++ [ 1 ]
else
result ++ [ 0 ]
in
nextRow newResult (List.drop 1 lastRow)
_ ->
result