Skip to content

Instantly share code, notes, and snippets.

@slavone
Last active December 3, 2017 17:08
Show Gist options
  • Save slavone/55563afbffb7c35e17e7ec2fa97c27bd to your computer and use it in GitHub Desktop.
Save slavone/55563afbffb7c35e17e7ec2fa97c27bd to your computer and use it in GitHub Desktop.
Advent of Code 2017 Day One
input = """
428122498997587283996116951397957933569136949848379417125362532269869461185743113733992331379856446362482129646556286611543756564275715359874924898113424472782974789464348626278532936228881786273586278886575828239366794429223317476722337424399239986153675275924113322561873814364451339186918813451685263192891627186769818128715595715444565444581514677521874935942913547121751851631373316122491471564697731298951989511917272684335463436218283261962158671266625299188764589814518793576375629163896349665312991285776595142146261792244475721782941364787968924537841698538288459355159783985638187254653851864874544584878999193242641611859756728634623853475638478923744471563845635468173824196684361934269459459124269196811512927442662761563824323621758785866391424778683599179447845595931928589255935953295111937431266815352781399967295389339626178664148415561175386725992469782888757942558362117938629369129439717427474416851628121191639355646394276451847131182652486561415942815818785884559193483878139351841633366398788657844396925423217662517356486193821341454889283266691224778723833397914224396722559593959125317175899594685524852419495793389481831354787287452367145661829287518771631939314683137722493531318181315216342994141683484111969476952946378314883421677952397588613562958741328987734565492378977396431481215983656814486518865642645612413945129485464979535991675776338786758997128124651311153182816188924935186361813797251997643992686294724699281969473142721116432968216434977684138184481963845141486793996476793954226225885432422654394439882842163295458549755137247614338991879966665925466545111899714943716571113326479432925939227996799951279485722836754457737668191845914566732285928453781818792236447816127492445993945894435692799839217467253986218213131249786833333936332257795191937942688668182629489191693154184177398186462481316834678733713614889439352976144726162214648922159719979143735815478633912633185334529484779322818611438194522292278787653763328944421516569181178517915745625295158611636365253948455727653672922299582352766484
"""
defmodule AOC2017.TaskOne do
def count(input) do
input
|> String.graphemes()
|> Enum.map(&String.to_integer/1)
|> do_count()
end
defp do_count([first | rest]) do
iterate rest, first, 0, first
end
defp iterate([], last, counter, first) when first == last do
counter + last
end
defp iterate([], _, counter, _), do: counter
defp iterate([curr | rest], prev, counter, first) when curr == prev do
iterate rest, curr, counter + prev, first
end
defp iterate([curr | rest], _, counter, first) do
iterate rest, curr, counter, first
end
end
IO.inspect AOC2017.TaskOne.count("1122"), label: "should be 3"
IO.inspect AOC2017.TaskOne.count("1111"), label: "should be 4"
IO.inspect AOC2017.TaskOne.count("1234"), label: "should be 0"
IO.inspect AOC2017.TaskOne.count("91212129"), label: "should be 9"
IO.inspect AOC2017.TaskOne.count(String.trim input), label: "final answer"
input = """
428122498997587283996116951397957933569136949848379417125362532269869461185743113733992331379856446362482129646556286611543756564275715359874924898113424472782974789464348626278532936228881786273586278886575828239366794429223317476722337424399239986153675275924113322561873814364451339186918813451685263192891627186769818128715595715444565444581514677521874935942913547121751851631373316122491471564697731298951989511917272684335463436218283261962158671266625299188764589814518793576375629163896349665312991285776595142146261792244475721782941364787968924537841698538288459355159783985638187254653851864874544584878999193242641611859756728634623853475638478923744471563845635468173824196684361934269459459124269196811512927442662761563824323621758785866391424778683599179447845595931928589255935953295111937431266815352781399967295389339626178664148415561175386725992469782888757942558362117938629369129439717427474416851628121191639355646394276451847131182652486561415942815818785884559193483878139351841633366398788657844396925423217662517356486193821341454889283266691224778723833397914224396722559593959125317175899594685524852419495793389481831354787287452367145661829287518771631939314683137722493531318181315216342994141683484111969476952946378314883421677952397588613562958741328987734565492378977396431481215983656814486518865642645612413945129485464979535991675776338786758997128124651311153182816188924935186361813797251997643992686294724699281969473142721116432968216434977684138184481963845141486793996476793954226225885432422654394439882842163295458549755137247614338991879966665925466545111899714943716571113326479432925939227996799951279485722836754457737668191845914566732285928453781818792236447816127492445993945894435692799839217467253986218213131249786833333936332257795191937942688668182629489191693154184177398186462481316834678733713614889439352976144726162214648922159719979143735815478633912633185334529484779322818611438194522292278787653763328944421516569181178517915745625295158611636365253948455727653672922299582352766484
"""
defmodule AOC2017.TaskTwo do
def count(input) do
input
|> String.graphemes()
|> Enum.map(&String.to_integer/1)
|> do_count()
end
defp do_count(list) do
list_size = length(list)
step = div(list_size, 2)
lookup_tuple = List.to_tuple(list)
list
|> Enum.with_index()
|> Enum.reduce(0, fn({curr, i}, counter) ->
next_index = rem(i + step, list_size)
if curr == elem(lookup_tuple, next_index) do
counter + curr
else
counter
end
end)
end
end
IO.inspect AOC2017.TaskTwo.count("1212"), label: "should be 6"
IO.inspect AOC2017.TaskTwo.count("1221"), label: "should be 0"
IO.inspect AOC2017.TaskTwo.count("123425"), label: "should be 4"
IO.inspect AOC2017.TaskTwo.count("123123"), label: "should be 12"
IO.inspect AOC2017.TaskTwo.count("12131415"), label: "should be 4"
IO.inspect AOC2017.TaskTwo.count(String.trim input), label: "final answer"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment