Advent of Code (2016) : Day 3
-- Problem --
You can find Part 1 of the problem statement
here. Part 2 is unlocked on successful completion of Part 1.
-- Solution in Elixir --
defmodule Aoc.Day3 do
import String, only: [to_integer: 1]
def is_valid_triangle(a,b,c) do
cond do
a >= b+c or b >= c+a or c >= a+b -> 0
true -> 1
end
end
def parse_input() do
{:ok, input} = File.read("day3_input.txt")
String.split(input, "\n")
|> List.delete_at(-1)
|> Stream.map(&String.split/1)
|> Enum.map(fn([a,b,c]) ->
[to_integer(a), to_integer(b), to_integer(c)]
end)
end
def output_1() do
parse_input()
|> Enum.map(fn([a,b,c]) -> is_valid_triangle(a,b,c) end)
|> Enum.sum
end
def output_2() do
parse_input()
|> calculate_output_2(0)
end
def calculate_output_2([], partial_result), do: partial_result
def calculate_output_2(input_list, partial_result) do
[[a1,b1,c1], [a2,b2,c2], [a3,b3,c3] | tail] = input_list
result_delta = is_valid_triangle(a1,a2,a3) + is_valid_triangle(b1,b2,b3) + is_valid_triangle(c1,c2,c3)
calculate_output_2(tail, partial_result + result_delta)
end
end
You can find all my Advent of Code (2016) solutions here.