Day Two
This commit is contained in:
parent
c756cffce7
commit
a08c86ded5
2 changed files with 45 additions and 6 deletions
12
day1.exs
12
day1.exs
|
@ -1,28 +1,28 @@
|
||||||
defmodule DayOne do
|
defmodule DayOne do
|
||||||
def partOne(input) do
|
def part_one(input) do
|
||||||
String.split(input, "\n")
|
String.split(input, "\n")
|
||||||
|> Enum.map(&DayOne.parseNum/1)
|
|> Enum.map(&DayOne.parseNum/1)
|
||||||
|> List.foldr(0, &(&1 + &2))
|
|> List.foldr(0, &(&1 + &2))
|
||||||
end
|
end
|
||||||
|
|
||||||
def partTwo(input), do: partTwo(String.split(input, "\n") |> Enum.map(&DayOne.parseNum/1), 0, [])
|
def part_two(input), do: partTwo(String.split(input, "\n") |> Enum.map(&DayOne.parseNum/1), 0, [])
|
||||||
|
|
||||||
def partTwo(input, acc , frequencies) do
|
def part_two(input, acc , frequencies) do
|
||||||
[head | tail] = input
|
[head | tail] = input
|
||||||
freq = head + acc
|
freq = head + acc
|
||||||
# IO.puts("Got frequency #{freq}")
|
# IO.puts("Got frequency #{freq}")
|
||||||
if Enum.member?(frequencies, freq), do: freq, else: partTwo(tail ++ [head], freq, [freq | frequencies])
|
if Enum.member?(frequencies, freq), do: freq, else: partTwo(tail ++ [head], freq, [freq | frequencies])
|
||||||
end
|
end
|
||||||
|
|
||||||
def parseNum("+" <> num) do
|
def parse_num("+" <> num) do
|
||||||
{intVal, ""} = Integer.parse(num)
|
{intVal, ""} = Integer.parse(num)
|
||||||
intVal
|
intVal
|
||||||
end
|
end
|
||||||
def parseNum("-" <> num) do
|
def parse_num("-" <> num) do
|
||||||
{intVal, ""} = Integer.parse(num)
|
{intVal, ""} = Integer.parse(num)
|
||||||
0 - intVal
|
0 - intVal
|
||||||
end
|
end
|
||||||
def parseNum(num) do
|
def parse_num(num) do
|
||||||
IO.puts(num)
|
IO.puts(num)
|
||||||
0
|
0
|
||||||
end
|
end
|
||||||
|
|
39
day2.exs
Normal file
39
day2.exs
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
defmodule DayTwo do
|
||||||
|
|
||||||
|
def part_one(input) do
|
||||||
|
lines = String.split(input, "\n") |> Enum.map(&String.codepoints/1)
|
||||||
|
two = Enum.map(lines, fn string -> DayTwo.check_occurence(string, 2) end)
|
||||||
|
|> Enum.filter(&(&1))
|
||||||
|
three = Enum.map(lines, fn string -> DayTwo.check_occurence(string, 3) end)
|
||||||
|
|> Enum.filter(&(&1))
|
||||||
|
length(two) * length(three)
|
||||||
|
end
|
||||||
|
|
||||||
|
def check_occurence(line, req) do
|
||||||
|
size = Enum.reduce(line, %{}, fn x, acc -> Map.update(acc, x, 1, &(&1 + 1)) end)
|
||||||
|
|> Enum.into([])
|
||||||
|
|> Enum.filter(fn {_, count} -> count == req end)
|
||||||
|
|> length
|
||||||
|
size > 0
|
||||||
|
end
|
||||||
|
|
||||||
|
def part_two(input) do
|
||||||
|
lines = String.split(input, "\n") |> Enum.map(&String.codepoints/1)
|
||||||
|
Enum.map(lines, fn line ->
|
||||||
|
Enum.map(lines, fn linetwo ->
|
||||||
|
diff = compare_strings(line, linetwo)
|
||||||
|
if diff == 1 do
|
||||||
|
IO.puts("#{line} #{linetwo}")
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def compare_strings(one, two) do
|
||||||
|
Enum.zip(one, two)
|
||||||
|
|> Enum.filter(fn {one, two} -> one != two end)
|
||||||
|
|> length
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
Loading…
Reference in a new issue