Day 1 Part 2
This commit is contained in:
parent
24af28c443
commit
da27ab18f7
1 changed files with 39 additions and 6 deletions
|
|
@ -1,5 +1,13 @@
|
||||||
defmodule Day1 do
|
defmodule Day1 do
|
||||||
def all_pairs(list) do
|
def part1(input) do
|
||||||
|
lines = String.split(input)
|
||||||
|
ints = Enum.map(lines, fn line -> String.to_integer(line) end)
|
||||||
|
pairs = all_pairs(ints)
|
||||||
|
[{a, b}] = Enum.filter(pairs, fn {a, b} -> a + b == 2020 end)
|
||||||
|
a * b
|
||||||
|
end
|
||||||
|
|
||||||
|
defp all_pairs(list) do
|
||||||
Enum.reduce(0..(length(list) - 1), [], fn idx, pairs ->
|
Enum.reduce(0..(length(list) - 1), [], fn idx, pairs ->
|
||||||
item = Enum.at(list, idx)
|
item = Enum.at(list, idx)
|
||||||
other_items = Enum.slice(list, (idx + 1)..(length(list) - 1))
|
other_items = Enum.slice(list, (idx + 1)..(length(list) - 1))
|
||||||
|
|
@ -8,14 +16,39 @@ defmodule Day1 do
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
def part1(input) do
|
# Wow, I'm out of shape when it comes to making sets recursively!
|
||||||
|
|
||||||
|
def part2(input) do
|
||||||
lines = String.split(input)
|
lines = String.split(input)
|
||||||
ints = Enum.map(lines, fn line -> String.to_integer(line) end)
|
ints = Enum.map(lines, &String.to_integer/1)
|
||||||
pairs = Day1.all_pairs(ints)
|
pairs = all_triples(ints)
|
||||||
[{a, b}] = Enum.filter(pairs, fn {a, b} -> a + b == 2020 end)
|
[{a, b, c}] = Enum.filter(pairs, fn {a, b, c} -> a + b + c == 2020 end)
|
||||||
a * b
|
a * b * c
|
||||||
|
end
|
||||||
|
|
||||||
|
defp all_triples(list) do
|
||||||
|
Enum.reduce(0..(length(list) - 1), [], fn idx, triples ->
|
||||||
|
item = Enum.at(list, idx)
|
||||||
|
pairs = all_pairs(Enum.slice(list, (idx + 1)..(length(list) - 1)))
|
||||||
|
new_triples = Enum.map(pairs, fn {a, b} -> {item, a, b} end)
|
||||||
|
triples ++ new_triples
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp combinations(list, size) do
|
||||||
|
indices = 0..(length(list) - 1)
|
||||||
|
|
||||||
|
Enum.zip(for _ <- 1..size, do: indices)
|
||||||
|
|> IO.inspect()
|
||||||
|
|> Enum.filter(fn idxs -> length(Enum.uniq(Tuple.to_list(idxs))) == size end)
|
||||||
|
|> Enum.map(fn idxs ->
|
||||||
|
for i <- 0..(size - 1) do
|
||||||
|
elem(idxs, i)
|
||||||
|
end
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
input = File.read!("input.txt")
|
input = File.read!("input.txt")
|
||||||
IO.inspect(Day1.part1(input))
|
IO.inspect(Day1.part1(input))
|
||||||
|
IO.inspect(Day1.part2(input))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue