Created
December 12, 2017 01:41
-
-
Save danj3/05239ab75b0f7f9d0ace6b57bc8e370e to your computer and use it in GitHub Desktop.
Advent of Code 2017 Day 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Day2 do | |
def parser( input ) do | |
String.split( input, "\n" ) | |
|> Enum.map( fn k -> | |
String.split( k, "\t") | |
|> Enum.map( &String.to_integer/1 ) | |
end ) | |
end | |
def start1( input ) do | |
input | |
|> parser | |
|> step1( 0 ) | |
end | |
def step1( [ row | rest ], acc ) do | |
step1( rest, acc + step1a( row ) ) | |
end | |
def step1( [], acc ), do: acc | |
def step1a( [ e | rest ] ), do: step1a( rest, { e, e } ) | |
def step1a( [ e | rest ], { low, hi } ) do | |
cond do | |
e > hi -> step1a( rest, {low, e } ) | |
e < low -> step1a( rest, { e, hi } ) | |
true -> step1a( rest, { low, hi } ) | |
end | |
end | |
def step1a( [], { low, hi } ), do: hi - low | |
def start2( input ) do | |
input | |
|> parser | |
|> step2( 0 ) | |
end | |
def step2( [ row | rest ], acc ), do: step2( rest, acc + step2a( row ) ) | |
def step2( [], acc ), do: acc | |
def step2a( [ a | row ] ) do | |
step2b( a, row ) | |
|> case do | |
nil -> step2a( row ) | |
val -> val | |
end | |
end | |
def step2a( [] ) do | |
IO.puts( "no divisible pair") | |
0 | |
end | |
def step2b( a, [ b | row ] ) do | |
cond do | |
Integer.mod( a, b ) == 0 -> Integer.floor_div( a, b ) | |
Integer.mod( b, a ) == 0 -> Integer.floor_div( b, a ) | |
true -> step2b( a, row ) | |
end | |
end | |
def step2b( a, [] ) do | |
nil | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment