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
103fd78
commit e3e3af3
Showing
3 changed files
with
54 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,9 @@ | ||
defmodule AdventOfCode.Day06 do | ||
def part1(_args) do | ||
def part1(inp) do | ||
Advent.Code6_1.execute(inp) | ||
end | ||
|
||
def part2(_args) do | ||
def part2(inp) do | ||
Advent.Code6_1.execute(inp) | ||
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,34 @@ | ||
defmodule Advent.Code6_1 do | ||
@sep "\n" | ||
defp parse_input(inp, sep) do | ||
[time, distance] = | ||
String.split(inp, sep, trim: true) | ||
|> Enum.map(fn el -> String.split(el, ~r/[(Time:)(Distance:)]/) end) | ||
|> Enum.map(fn sublist -> Enum.filter(sublist, fn el -> String.length(el) != 0 end) end) | ||
|> Enum.map(fn sublist -> | ||
Enum.map(sublist, fn el -> String.split(el, " ", trim: true) end) | ||
end) | ||
|> Enum.flat_map(fn el -> el end) | ||
|> Enum.map(fn sublist -> | ||
Enum.map(sublist, fn el -> My.Utils.string_to_num_or_false(el) end) |> Enum.with_index() | ||
end) | ||
|
||
time | ||
|> Enum.map(fn {t_el, idx} -> | ||
{d_el, idx} = Enum.at(distance, idx) | ||
%{time: t_el, distance: d_el, idx: idx} | ||
end) | ||
end | ||
|
||
defp calculate(row) do | ||
Enum.to_list(0..row[:time]) | ||
|> Enum.filter(fn el -> el * (row[:time] - el) > row[:distance] end) | ||
|> Enum.count() | ||
end | ||
|
||
def execute(inp) do | ||
parse_input(inp, @sep) | ||
|> Enum.map(fn el -> calculate(el) end) | ||
|> Enum.reduce(fn el, acc -> acc * el 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