Day 1: add alternate part 2 solution
This commit is contained in:
parent
3db1e578f0
commit
aedc078ad7
|
@ -16,6 +16,18 @@ defmodule Day1 do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def first_repetition_stream(shifts) do
|
||||||
|
shifts
|
||||||
|
|> Stream.cycle()
|
||||||
|
|> Enum.reduce_while({0, [0]}, fn shift, {sum, seen} ->
|
||||||
|
sum = sum + shift
|
||||||
|
cond do
|
||||||
|
Enum.member?(seen, sum) -> {:halt, sum}
|
||||||
|
true -> {:cont, {sum, [sum | seen]}}
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
def parse_input() do
|
def parse_input() do
|
||||||
File.read!("lib/day1/input.txt")
|
File.read!("lib/day1/input.txt")
|
||||||
|> String.split("\n")
|
|> String.split("\n")
|
||||||
|
@ -32,6 +44,6 @@ defmodule Day1 do
|
||||||
|
|
||||||
def part2() do
|
def part2() do
|
||||||
parse_input()
|
parse_input()
|
||||||
|> first_repetition()
|
|> first_repetition_stream()
|
||||||
end
|
end
|
||||||
end
|
end
|
|
@ -16,4 +16,12 @@ defmodule Day1Test do
|
||||||
assert Day1.first_repetition([-6, 3, 8, 5, -6]) == 5
|
assert Day1.first_repetition([-6, 3, 8, 5, -6]) == 5
|
||||||
assert Day1.first_repetition([7, 7, -2, -7, -4]) == 14
|
assert Day1.first_repetition([7, 7, -2, -7, -4]) == 14
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "first repetition stream" do
|
||||||
|
assert Day1.first_repetition_stream([1, -2, 3, 1]) == 2
|
||||||
|
assert Day1.first_repetition_stream([1, -1]) == 0
|
||||||
|
assert Day1.first_repetition_stream([3, 3, 4, -2, -4]) == 10
|
||||||
|
assert Day1.first_repetition_stream([-6, 3, 8, 5, -6]) == 5
|
||||||
|
assert Day1.first_repetition_stream([7, 7, -2, -7, -4]) == 14
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue