1
1
Fork 0
This commit is contained in:
g 2018-12-02 13:43:29 +08:00
parent c756cffce7
commit a08c86ded5
No known key found for this signature in database
GPG key ID: 60F3D48C2F3C0415
2 changed files with 45 additions and 6 deletions

View file

@ -1,28 +1,28 @@
defmodule DayOne do
def partOne(input) do
def part_one(input) do
String.split(input, "\n")
|> Enum.map(&DayOne.parseNum/1)
|> List.foldr(0, &(&1 + &2))
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
freq = head + acc
# IO.puts("Got frequency #{freq}")
if Enum.member?(frequencies, freq), do: freq, else: partTwo(tail ++ [head], freq, [freq | frequencies])
end
def parseNum("+" <> num) do
def parse_num("+" <> num) do
{intVal, ""} = Integer.parse(num)
intVal
end
def parseNum("-" <> num) do
def parse_num("-" <> num) do
{intVal, ""} = Integer.parse(num)
0 - intVal
end
def parseNum(num) do
def parse_num(num) do
IO.puts(num)
0
end

39
day2.exs Normal file
View 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