From 65afc4b41bccb10190a04c1ccedd16b0702766b8 Mon Sep 17 00:00:00 2001 From: Gerd Sattler Date: Mon, 3 Dec 2018 20:23:00 +0800 Subject: [PATCH] I guess it was time for formatting --- day1.exs | 17 ++++++++++------ day2.exs | 36 +++++++++++++++++++------------- day3.exs | 62 +++++++++++++++++++++++++++++++++++--------------------- 3 files changed, 72 insertions(+), 43 deletions(-) diff --git a/day1.exs b/day1.exs index a24074f..4cedef1 100644 --- a/day1.exs +++ b/day1.exs @@ -1,27 +1,32 @@ defmodule DayOne do def part_one(input) do String.split(input, "\n") - |> Enum.map(&DayOne.parseNum/1) - |> List.foldr(0, &(&1 + &2)) + |> Enum.map(&DayOne.parseNum/1) + |> List.foldr(0, &(&1 + &2)) 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 freq = head + acc # 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 - def parse_num("+" <> num) do + def parse_num("+" <> num) do {intVal, ""} = Integer.parse(num) intVal end + def parse_num("-" <> num) do {intVal, ""} = Integer.parse(num) 0 - intVal end + def parse_num(num) do IO.puts(num) 0 diff --git a/day2.exs b/day2.exs index 2675d42..46aaee2 100644 --- a/day2.exs +++ b/day2.exs @@ -1,39 +1,47 @@ 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)) + + 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 + 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 -> + 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 + |> Enum.filter(fn {one, two} -> one != two end) + |> length end - end diff --git a/day3.exs b/day3.exs index 319d54b..271264f 100644 --- a/day3.exs +++ b/day3.exs @@ -1,47 +1,60 @@ defmodule DayThree do - def parse_claim(claim) do pattern = ~r/#(\d+) @ (\d+),(\d+): (\d+)x(\d+)/ - [ [ _ | match ] ] = Regex.scan(pattern, claim) - [id, x, y, xwidth, ywidth] = Enum.map(match, &(String.to_integer(&1))) + [[_ | match]] = Regex.scan(pattern, claim) + [id, x, y, xwidth, ywidth] = Enum.map(match, &String.to_integer(&1)) {id, x, y, xwidth, ywidth} end def part_one(input) do - claims = String.split(input, "\n") + claims = + String.split(input, "\n") |> Enum.filter(&(&1 != "")) |> Enum.map(&DayThree.parse_claim/1) - IO.puts "dupes" + dupes = new_doubles(claims) - |> Enum.dedup - area_with = dupes + + area_with = + dupes |> Enum.map(fn {_, {_, _, dx, dy}} -> dx * dy end) |> List.foldl(0, &+/2) + area_with end def new_doubles(claims) do Enum.map(claims, fn {id1, x1, y1, dx1, dy1} -> Enum.map(claims, fn {id2, x2, y2, dx2, dy2} -> - {{:id, id1, id2}, intersect({x1, y1, dx1, dy1}, {x2, y2, dx2, dy2})} - end) |> Enum.filter(fn {_, square} -> square != nil end) - end) - |> List.flatten + {{:id, id1, id2}, intersect({id1, x1, y1, dx1, dy1}, {id2, x2, y2, dx2, dy2})} + end) |> Enum.filter(fn {_, square} -> square != nil end) + end) + |> List.flatten() + |> Enum.filter(fn {_, square} -> square != nil end) end - def intersect({x1, y1, dx1, dy1}, {x2, y2, dx2, dy2}) do - if {x1, y1, dx1, dy1} == {x2, y2, dx2, dy2} do + def intersect({id1, x1, y1, dx1, dy1}, {id2, 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 else xe1 = x1 + dx1 xe2 = x2 + dx2 ye1 = y1 + dy1 + if x2 <= xe1 && y2 <= ye1 && x2 >= x1 && y2 >= y1 do {y2, x2, xe1 - x2, ye1 - y2} - else if xe2 >= x1 && y2 <= ye1 && x2 <= x1 && y2 >= y1 do - {x1, y2, xe2 - x1, ye1 - y2} + else + if xe2 >= x1 && y2 <= ye1 && x2 <= x1 && y2 >= y1 do + {x1, y2, xe2 - x1, ye1 - y2} else nil end @@ -50,21 +63,24 @@ defmodule DayThree do end def part_two(input) do - claims = String.split(input, "\n") + claims = + String.split(input, "\n") |> Enum.filter(&(&1 != "")) |> Enum.map(&DayThree.parse_claim/1) - IO.puts "dupes" - dupes = new_doubles(claims) - |> Enum.dedup + + dupes = + new_doubles(claims) + |> Enum.dedup() |> Enum.map(fn {{:id, id1, id2}, _} -> [id1, id2] end) - |> List.flatten + |> List.flatten() + claim_id = Enum.map(claims, fn {id, _, _, _, _} -> id end) [id] = Enum.filter(claim_id, &(not Enum.member?(dupes, &1))) id 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 2: #{DayThree.part_two(content)}" +IO.puts("Part 1 (most likely incorrect): #{DayThree.part_one(content)}") +IO.puts("Part 2: #{DayThree.part_two(content)}")