Skip to content

Instantly share code, notes, and snippets.

@jpierson
Last active December 8, 2017 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpierson/e5e1c13694f9ca1415429d346950f088 to your computer and use it in GitHub Desktop.
Save jpierson/e5e1c13694f9ca1415429d346950f088 to your computer and use it in GitHub Desktop.
Code solution for Advent of Code 2017 Day 1 (http://adventofcode.com/2017/day/1)
module Main exposing (main)
import Html exposing (Html, li, text, ul)
import Html.Attributes exposing (style)
-- Advent of Code 2017 - Day 1 (http://adventofcode.com/2017/day/1)
-- https://ellie-app.com/hBMTrVXyna1/1
main : Html msg
main =
ul []
(List.map
(\c ->
let
result =
dayOneFacade (Tuple.first c)
in
result
|> toString
|> (\s ->
"Expected "
++ toString (Tuple.second c)
++ " got "
++ s
|> text
|> (\t ->
li
[ style
[ ( "background"
, if result == Tuple.second c then
"green"
else
"red"
)
]
]
[ t ]
)
)
)
cases
)
cases =
[ ( "1122", 3 ), ( "1111", 4 ), ( "1234", 0 ), ( "91212129", 9 ) ]
dayOneFacade : String -> Int
dayOneFacade digits =
let
dayOne : List Int -> Int -> Int
dayOne list sum =
case list of
a :: b :: tail ->
dayOne (b :: tail)
(if a == b then
sum + b
else
sum
)
_ ->
sum
list =
String.split "" digits
|> List.map (String.toInt >> Result.toMaybe >> Maybe.withDefault 0)
|> (\ints -> ints ++ List.take 1 ints)
in
dayOne list 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment