1
1
Fork 0

I guess it was time for formatting

This commit is contained in:
g 2018-12-03 20:23:00 +08:00
parent 365d30bb63
commit 65afc4b41b
No known key found for this signature in database
GPG key ID: 60F3D48C2F3C0415
3 changed files with 72 additions and 43 deletions

View file

@ -1,27 +1,32 @@
defmodule DayOne do defmodule DayOne do
def part_one(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 part_two(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 part_two(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 parse_num("+" <> num) do def parse_num("+" <> num) do
{intVal, ""} = Integer.parse(num) {intVal, ""} = Integer.parse(num)
intVal intVal
end end
def parse_num("-" <> num) do def parse_num("-" <> num) do
{intVal, ""} = Integer.parse(num) {intVal, ""} = Integer.parse(num)
0 - intVal 0 - intVal
end end
def parse_num(num) do def parse_num(num) do
IO.puts(num) IO.puts(num)
0 0

View file

@ -1,39 +1,47 @@
defmodule DayTwo do defmodule DayTwo do
def part_one(input) do def part_one(input) do
lines = String.split(input, "\n") |> Enum.map(&String.codepoints/1) 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)) two =
three = Enum.map(lines, fn string -> DayTwo.check_occurence(string, 3) end) Enum.map(lines, fn string -> DayTwo.check_occurence(string, 2) end)
|> Enum.filter(&(&1)) |> Enum.filter(& &1)
three =
Enum.map(lines, fn string -> DayTwo.check_occurence(string, 3) end)
|> Enum.filter(& &1)
length(two) * length(three) length(two) * length(three)
end end
def check_occurence(line, req) do def check_occurence(line, req) do
size = Enum.reduce(line, %{}, fn x, acc -> Map.update(acc, x, 1, &(&1 + 1)) end) size =
|> Enum.into([]) Enum.reduce(line, %{}, fn x, acc -> Map.update(acc, x, 1, &(&1 + 1)) end)
|> Enum.filter(fn {_, count} -> count == req end) |> Enum.into([])
|> length |> Enum.filter(fn {_, count} -> count == req end)
|> length
size > 0 size > 0
end end
def part_two(input) do def part_two(input) do
lines = String.split(input, "\n") |> Enum.map(&String.codepoints/1) lines = String.split(input, "\n") |> Enum.map(&String.codepoints/1)
Enum.map(lines, fn line -> Enum.map(lines, fn line ->
Enum.map(lines, fn linetwo -> Enum.map(lines, fn linetwo ->
diff = compare_strings(line, linetwo) diff = compare_strings(line, linetwo)
if diff == 1 do if diff == 1 do
IO.puts("#{line} #{linetwo}") IO.puts("#{line} #{linetwo}")
end end
end) end)
end) end)
nil nil
end end
def compare_strings(one, two) do def compare_strings(one, two) do
Enum.zip(one, two) Enum.zip(one, two)
|> Enum.filter(fn {one, two} -> one != two end) |> Enum.filter(fn {one, two} -> one != two end)
|> length |> length
end end
end end

View file

@ -1,47 +1,60 @@
defmodule DayThree do defmodule DayThree do
def parse_claim(claim) do def parse_claim(claim) do
pattern = ~r/#(\d+) @ (\d+),(\d+): (\d+)x(\d+)/ pattern = ~r/#(\d+) @ (\d+),(\d+): (\d+)x(\d+)/
[ [ _ | match ] ] = Regex.scan(pattern, claim) [[_ | match]] = Regex.scan(pattern, claim)
[id, x, y, xwidth, ywidth] = Enum.map(match, &(String.to_integer(&1))) [id, x, y, xwidth, ywidth] = Enum.map(match, &String.to_integer(&1))
{id, x, y, xwidth, ywidth} {id, x, y, xwidth, ywidth}
end end
def part_one(input) do def part_one(input) do
claims = String.split(input, "\n") claims =
String.split(input, "\n")
|> Enum.filter(&(&1 != "")) |> Enum.filter(&(&1 != ""))
|> Enum.map(&DayThree.parse_claim/1) |> Enum.map(&DayThree.parse_claim/1)
IO.puts "dupes"
dupes = new_doubles(claims) dupes = new_doubles(claims)
|> Enum.dedup
area_with = dupes area_with =
dupes
|> Enum.map(fn {_, {_, _, dx, dy}} -> dx * dy end) |> Enum.map(fn {_, {_, _, dx, dy}} -> dx * dy end)
|> List.foldl(0, &+/2) |> List.foldl(0, &+/2)
area_with area_with
end end
def new_doubles(claims) do def new_doubles(claims) do
Enum.map(claims, fn {id1, x1, y1, dx1, dy1} -> Enum.map(claims, fn {id1, x1, y1, dx1, dy1} ->
Enum.map(claims, fn {id2, x2, y2, dx2, dy2} -> Enum.map(claims, fn {id2, x2, y2, dx2, dy2} ->
{{:id, id1, id2}, intersect({x1, y1, dx1, dy1}, {x2, y2, dx2, dy2})} {{:id, id1, id2}, intersect({id1, x1, y1, dx1, dy1}, {id2, x2, y2, dx2, dy2})}
end) |> Enum.filter(fn {_, square} -> square != nil end) end)
end)
|> List.flatten
|> Enum.filter(fn {_, square} -> square != nil end) |> Enum.filter(fn {_, square} -> square != nil end)
end)
|> List.flatten()
|> Enum.filter(fn {_, square} -> square != nil end)
end end
def intersect({x1, y1, dx1, dy1}, {x2, y2, dx2, dy2}) do def intersect({id1, x1, y1, dx1, dy1}, {id2, x2, y2, dx2, dy2}) do
if {x1, y1, dx1, dy1} == {x2, y2, dx2, dy2} do if id1 == id2 do
nil
else
intersect({x1, y1, dx1, dy1}, {x2, y2, dx2, dy2}, true)
end
end
def intersect({x1, y1, dx1, dy1}, {x2, y2, dx2, dy2}, ignore \\ false) do
if {x1, y1, dx1, dy1} == {x2, y2, dx2, dy2} && not ignore do
nil nil
else else
xe1 = x1 + dx1 xe1 = x1 + dx1
xe2 = x2 + dx2 xe2 = x2 + dx2
ye1 = y1 + dy1 ye1 = y1 + dy1
if x2 <= xe1 && y2 <= ye1 && x2 >= x1 && y2 >= y1 do if x2 <= xe1 && y2 <= ye1 && x2 >= x1 && y2 >= y1 do
{y2, x2, xe1 - x2, ye1 - y2} {y2, x2, xe1 - x2, ye1 - y2}
else if xe2 >= x1 && y2 <= ye1 && x2 <= x1 && y2 >= y1 do else
{x1, y2, xe2 - x1, ye1 - y2} if xe2 >= x1 && y2 <= ye1 && x2 <= x1 && y2 >= y1 do
{x1, y2, xe2 - x1, ye1 - y2}
else else
nil nil
end end
@ -50,21 +63,24 @@ defmodule DayThree do
end end
def part_two(input) do def part_two(input) do
claims = String.split(input, "\n") claims =
String.split(input, "\n")
|> Enum.filter(&(&1 != "")) |> Enum.filter(&(&1 != ""))
|> Enum.map(&DayThree.parse_claim/1) |> Enum.map(&DayThree.parse_claim/1)
IO.puts "dupes"
dupes = new_doubles(claims) dupes =
|> Enum.dedup new_doubles(claims)
|> Enum.dedup()
|> Enum.map(fn {{:id, id1, id2}, _} -> [id1, id2] end) |> Enum.map(fn {{:id, id1, id2}, _} -> [id1, id2] end)
|> List.flatten |> List.flatten()
claim_id = Enum.map(claims, fn {id, _, _, _, _} -> id end) claim_id = Enum.map(claims, fn {id, _, _, _, _} -> id end)
[id] = Enum.filter(claim_id, &(not Enum.member?(dupes, &1))) [id] = Enum.filter(claim_id, &(not Enum.member?(dupes, &1)))
id id
end end
end end
{:ok, content} = File.read "input3.txt" {:ok, content} = File.read("input3.txt")
IO.puts "Part 1: #{DayThree.part_one(content)}" IO.puts("Part 1 (most likely incorrect): #{DayThree.part_one(content)}")
IO.puts "Part 2: #{DayThree.part_two(content)}" IO.puts("Part 2: #{DayThree.part_two(content)}")