generated from mhanberg/advent-of-code-elixir-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
83b951c
commit f393e33
Showing
4 changed files
with
88 additions
and
8 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,16 @@ | ||
defmodule AdventOfCode.Day09 do | ||
def part1(_args) do | ||
alias Advent.D09_01 | ||
alias Advent.D09_02 | ||
|
||
def part1(inp) do | ||
D09_01.parse_input(inp) | ||
|> Enum.reduce(0, fn el, acc -> acc + (List.last(el) + D09_01.extrapolate(el, 0)) end) | ||
end | ||
|
||
def part2(_args) do | ||
def part2(inp) do | ||
D09_01.parse_input(inp) | ||
|> Enum.reduce(0, fn el, acc -> | ||
acc + (List.first(el) - D09_02.get_left_val(D09_02.extrapolate(el, []), 0)) | ||
end) | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
defmodule Advent.D09_01 do | ||
defp calc_next([], next), do: next | ||
|
||
defp calc_next(curr, next) do | ||
if(length(curr) > 1) do | ||
[h | t] = curr | ||
[h2 | _] = t | ||
calc_next(t, [h - h2 | next]) | ||
else | ||
next | ||
end | ||
end | ||
|
||
def extrapolate(line, acc) do | ||
if(Enum.all?(line, fn el -> el == 0 end)) do | ||
acc | ||
else | ||
next = calc_next(Enum.reverse(line), []) | ||
extrapolate(next, acc + List.last(next)) | ||
end | ||
end | ||
|
||
@sep "\n" | ||
def parse_input(inp) do | ||
inp | ||
|> String.split(@sep, trim: true) | ||
|> Enum.map(fn sublist -> | ||
Enum.map(String.split(sublist, " ", trim: true), fn el -> | ||
My.Utils.string_to_num_or_false(el) | ||
end) | ||
end) | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
defmodule Advent.D09_02 do | ||
defp calc_next([], next), do: next | ||
|
||
defp calc_next(curr, next) do | ||
if(length(curr) > 1) do | ||
[h | t] = curr | ||
[h2 | _] = t | ||
calc_next(t, [h2 - h | next]) | ||
else | ||
next | ||
end | ||
end | ||
|
||
def extrapolate(line, res) do | ||
if(Enum.all?(line, fn el -> el == 0 end)) do | ||
res | ||
else | ||
next = Enum.reverse(calc_next(line, [])) | ||
extrapolate(next, [List.first(next)] ++ res) | ||
end | ||
end | ||
|
||
def get_left_val([], acc), do: acc | ||
|
||
def get_left_val(vals, acc) do | ||
if(length(vals) > 0) do | ||
[f | values] = vals | ||
get_left_val(values, f - acc) | ||
else | ||
acc | ||
end | ||
end | ||
end |
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