Skip to content

Instantly share code, notes, and snippets.

@jpierson
Created December 8, 2017 05:20
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/f1b3b47d0ea3620ce5da63cbf512ed43 to your computer and use it in GitHub Desktop.
Save jpierson/f1b3b47d0ea3620ce5da63cbf512ed43 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 Part 2 (http://adventofcode.com/2017/day/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 =
[ ( "1212", 6 ),
( "1221", 0 ),
( "123425", 4 ),
( "123123", 12 ),
( "12131415", 4 ),
( "181445682966897848665963472661939865313976877194312684993521259486517527961396717561854825453963181134379574918373213732184697746668399631642622373684425326112585283946462323363991753895647177797691214784149215198715986947573668987188746878678399624533792551651335979847131975965677957755571358934665327487287312467771187981424785514785421781781976477326712674311994735947987383516699897916595433228294198759715959469578766739518475118771755787196238772345762941477359483456641194685333528329581113788599843621326313592354167846466415943566183192946217689936174884493199368681514958669615226362538622898367728662941275658917124167353496334664239539753835439929664552886538885727235662548783529353611441231681613535447417941911479391558481443933134283852879511395429489152435996669232681215627723723565872291296878528334773391626672491878762288953597499218397146685679387438634857358552943964839321464529237533868734473777756775687759355878519113426969197211824325893376812556798483325994128743242544899625215765851923959798197562831313891371735973761384464685316273343541852758525318144681364492173465174512856618292785483181956548813344752352933634979165667651165776587656468598791994573513652324764687515345959621493346623821965554755615219855842969932269414839446887613738174567989512857785566352285988991946436148652839391593178736624957214917527759574235133666461988355855613377789115472297915429318142824465141688559333787512328799783539285826471818279818457674417354335454395644435889386297695625378256613558911695145397779576526397241795181294322797687168326696497256684943829666672341162656479563522892141714998477865114944671225898297338685958644728534192317628618817551492975251364233974374724968483637518876583946828819994321129556511537619253381981544394112184655586964655164192552352534626295996968762388827294873362719636616182786976922445125551927969267591395292198155775434997827738862786341543524544822321112131815475829945625787561369956264826651461575948462782869972654343749617939132353399334744265286151177931594514857563664329299713436914721119746932159456287267887878779218815883191236858656959258484139254446341", 2)
]
dayOneFacade : String -> Int
dayOneFacade digits =
let
dayOne : List (Int, Int) -> Int -> Int
dayOne list sum =
case list of
(a, b) :: tail ->
dayOne (tail)
(if a == b then
sum + a
else
sum
)
_ ->
sum
half = (String.length digits) // 2
list =
String.split "" digits
|> List.map (String.toInt >> Result.toMaybe >> Maybe.withDefault 0)
|> \numbers -> List.map2 (\x y -> (x, y)) numbers (List.drop half numbers ++ List.take half numbers)
in
dayOne list 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment