1
1
Fork 0
This commit is contained in:
g 2018-12-07 21:39:04 +08:00
parent 8e4fbf44bc
commit e8c1cb07f1
No known key found for this signature in database
GPG key ID: 60F3D48C2F3C0415
3 changed files with 1022 additions and 6 deletions

View file

@ -1,12 +1,12 @@
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.filter(&(&1 != ""))
|> Enum.map(&DayOne.parseNum/1) |> Enum.map(&DayOne.parse_num/1)
|> List.foldr(0, &(&1 + &2)) |> List.foldr(0, &(&1 + &2))
end end
def part_two(input), def part_two(input),
do: partTwo(String.split(input, "\n") |> Enum.map(&DayOne.parseNum/1), 0, []) do: part_two(String.split(input, "\n") |> Enum.filter(&(&1 != "")) |> Enum.map(&DayOne.parse_num/1), 0, [])
def part_two(input, acc, frequencies) do def part_two(input, acc, frequencies) do
[head | tail] = input [head | tail] = input
@ -14,7 +14,7 @@ defmodule DayOne do
# IO.puts("Got frequency #{freq}") # IO.puts("Got frequency #{freq}")
if Enum.member?(frequencies, freq), if Enum.member?(frequencies, freq),
do: freq, do: freq,
else: partTwo(tail ++ [head], freq, [freq | frequencies]) else: part_two(tail ++ [head], freq, [freq | frequencies])
end end
def parse_num("+" <> num) do def parse_num("+" <> num) do
@ -32,3 +32,8 @@ defmodule DayOne do
0 0
end end
end end
{:ok, content} = File.read("input1.txt")
IO.puts("Part 1: #{DayOne.part_one(content)}")
IO.puts("Part 2: #{DayOne.part_two(content)}")

View file

@ -6,14 +6,14 @@ defmodule DaySix do
legal = filter_infinite(distances, corners) legal = filter_infinite(distances, corners)
# IO.puts "Total: #{length distances} Legal: #{length legal}" # IO.puts "Total: #{length distances} Legal: #{length legal}"
length = Enum.group_by(legal, fn {_, coord} -> coord end) length = Enum.group_by(legal, fn {_, coord} -> coord end)
|> Enum.map(fn {key, list} -> length list end) |> Enum.map(fn {_key, list} -> length list end)
|> Enum.max |> Enum.max
length length
end end
def part_two(input) do def part_two(input) do
coords = parse_input(input) coords = parse_input(input)
{maxx, maxy, minx, miny} = find_corners(coords) {maxx, maxy, _minx, _miny} = find_corners(coords)
field = gen_field({maxx * 2, maxy * 2, 0, 0}) field = gen_field({maxx * 2, maxy * 2, 0, 0})
filter_below(field, coords, 10000) |> length filter_below(field, coords, 10000) |> length
end end

1011
input1.txt Normal file

File diff suppressed because it is too large Load diff