1
1
Fork 0

Day 6 in pure elixir

Wow so proud of myself this seemed impossible when i started
This commit is contained in:
g 2018-12-06 14:09:00 +08:00
parent 391e4bbc8d
commit 7922c5add8
No known key found for this signature in database
GPG Key ID: 60F3D48C2F3C0415
3 changed files with 147 additions and 2 deletions

View File

@ -18,8 +18,9 @@ defmodule DayFive do
String.length(poly) - 1
end
def react([<<char1>>, <<char2>> | tail], rev) when char1 == char2 - 32 or char2 == char1 - 32,
do: react(tail, rev)
def react([<<char1>>, <<char2>> | tail], rev)
when char1 == char2 - 32 or char2 == char1 - 32,
do: react(tail, rev)
def react([<<char1>> | tail], [<<char2>> | rev])
when char1 == char2 - 32 or char2 == char1 - 32,

94
day6.exs Normal file
View File

@ -0,0 +1,94 @@
defmodule DaySix do
def part_one(input) do
coords = parse_input(input)
corners = find_corners(coords)
distances = distances(corners, coords)
legal = filter_infinite(distances, corners)
# IO.puts "Total: #{length distances} Legal: #{length legal}"
length = Enum.group_by(legal, fn {_, coord} -> coord end)
|> Enum.map(fn {key, list} -> length list end)
|> Enum.max
length
end
def part_two(input) do
coords = parse_input(input)
{maxx, maxy, minx, miny} = find_corners(coords)
field = gen_field({maxx * 2, maxy * 2, 0, 0})
filter_below(field, coords, 10000) |> length
end
def parse_input(input) do
lines = String.split(input, "\n")
|> Enum.filter(&(&1 != ""))
lines
|> Enum.map(&(String.split(&1, ", ")))
|> Enum.map(fn [x, y] -> {String.to_integer(x), String.to_integer(y)} end)
end
def find_corners(coords) do
{maxx, _} = Enum.max_by(coords, fn {x, _} -> x end)
{_, maxy} = Enum.max_by(coords, fn {_, y} -> y end)
{minx, _} = Enum.min_by(coords, fn {x, _} -> x end)
{_, miny} = Enum.min_by(coords, fn {_, y} -> y end)
{maxx, maxy, minx, miny}
end
def gen_field({maxx, maxy, minx, miny}) do
Enum.map(minx..maxx, fn i ->
Enum.map(miny..maxy, fn j ->
{i, j}
end)
end) |> List.flatten
end
def distances({maxx, maxy, minx, miny}, coords) do
Enum.map(minx..maxx, fn i ->
Enum.map(miny..maxy, fn j ->
distances = Enum.map(coords, fn {x, y} ->
{{x, y}, abs(i - x) + abs(j - y)}
end)
{{x, y}, distance} = Enum.min_by(distances, fn {_, distance} -> distance end)
if length(Enum.filter(distances, fn {_, d} -> distance == d end)) > 1 do
[]
else
{{i, j}, {x, y}}
end
end)
end)
|> List.flatten
# |> Map.new
end
def is_border({maxx, maxy, minx, miny}, {x, y}) when x == maxx or x == minx or y == maxy or y == miny, do: true
def is_border(_, _), do: false
def filter_infinite(distances, corners) do
is_infinite = Enum.map(distances, fn {{i, j}, {x, y}} -> {{x, y}, is_border(corners, {i, j})} end)
|> Enum.group_by(fn {coord, _} -> coord end)
|> Enum.map(fn {coord, list} -> {coord, Enum.map(list, fn {_, inf} -> inf end)} end)
# IO.inspect is_infinite
infinites = is_infinite
|> Enum.filter(fn {_, list} -> Enum.member?(list, true) end)
|> Enum.map(fn {coord, _} -> coord end)
# IO.puts "Infinites: #{length infinites}"
Enum.filter(distances, fn {_, coord} -> not Enum.member?(infinites, coord) end)
end
def total_distance({i, j}, points) do
Enum.map(points, fn {x, y} -> abs(x - i) + abs(y - j) end) |> List.foldl(0, &+/2)
end
def filter_below(coords, points, under) do
total_distances = Enum.map(coords, fn coord -> {coord, total_distance(coord, points)} end)
Enum.filter(total_distances, fn {_, dist} -> dist < under end)
end
end
{:ok, content} = File.read("input6.txt")
IO.puts("Part 1: #{DaySix.part_one(content)}")
IO.puts("Part 2: #{DaySix.part_two(content)}")

50
input6.txt Normal file
View File

@ -0,0 +1,50 @@
66, 204
55, 226
231, 196
69, 211
69, 335
133, 146
321, 136
220, 229
148, 138
42, 319
304, 181
101, 329
72, 244
242, 117
83, 237
169, 225
311, 212
348, 330
233, 268
99, 301
142, 293
239, 288
200, 216
44, 215
353, 289
54, 73
73, 317
55, 216
305, 134
343, 233
227, 75
139, 285
264, 179
349, 263
48, 116
223, 60
247, 148
320, 232
60, 230
292, 78
247, 342
59, 326
333, 210
186, 291
218, 146
205, 246
124, 204
76, 121
333, 137
117, 68