Skip to content

Commit 476dbb9

Browse files
committed
Remove input macro from 2020 solutions
1 parent ad5682a commit 476dbb9

40 files changed

+239
-266
lines changed

lib/2020/day_01.ex

+10-12
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,26 @@ defmodule AdventOfCode.Y2020.Day01 do
33
--- Day 1: Report Repair ---
44
Problem Link: https://adventofcode.com/2020/day/1
55
"""
6-
use AdventOfCode.Helpers.InputReader, year: 2020, day: 1
6+
alias AdventOfCode.Helpers.{InputReader, Transformers}
77

8-
def run_1, do: input!() |> parse() |> two_entries()
9-
def run_2, do: input!() |> parse() |> three_entries()
8+
def input, do: InputReader.read_from_file(2020, 1)
109

11-
def parse(input) do
12-
input
13-
|> String.split("\n", trim: true)
14-
|> Enum.map(&String.to_integer(&1))
10+
def run(input \\ input()) do
11+
input = Transformers.int_lines(input)
12+
{two_entries(input), three_entries(input)}
1513
end
1614

17-
def two_entries(xs), do: do_two_entries(xs, MapSet.new(xs))
15+
def two_entries(xs), do: two_entries(xs, MapSet.new(xs))
1816

19-
def three_entries(xs), do: do_three_entries(xs, MapSet.new(xs))
20-
21-
defp do_two_entries(xs, set) do
17+
def two_entries(xs, set) do
2218
Enum.reduce_while(xs, nil, fn x, _ ->
2319
halt_and_get(set, x, x)
2420
end)
2521
end
2622

27-
defp do_three_entries([_ | ys] = xs, set) do
23+
def three_entries(xs), do: three_entries(xs, MapSet.new(xs))
24+
25+
def three_entries([_ | ys] = xs, set) do
2826
Enum.reduce_while(xs, nil, fn x, _ ->
2927
Enum.reduce_while(ys, nil, fn y, _ ->
3028
halt_and_get(set, x + y, x * y)

lib/2020/day_02.ex

+9-4
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ defmodule AdventOfCode.Y2020.Day02 do
33
--- Day 2: Password Philosophy ---
44
Problem Link: https://adventofcode.com/2020/day/2
55
"""
6-
use AdventOfCode.Helpers.InputReader, year: 2020, day: 2
6+
alias AdventOfCode.Helpers.{InputReader, Transformers}
77

8-
def run_1, do: input!() |> parse() |> solve()
9-
def run_2, do: input!() |> parse() |> solve_corrected()
8+
def input, do: InputReader.read_from_file(2020, 2)
9+
10+
def run(input \\ input()) do
11+
input = parse(input)
12+
13+
{solve(input), solve_corrected(input)}
14+
end
1015

1116
def parse(input) do
1217
input
13-
|> String.split("\n")
18+
|> Transformers.lines()
1419
|> Enum.map(&parse_line/1)
1520
end
1621

lib/2020/day_03.ex

+14-31
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,22 @@ defmodule AdventOfCode.Y2020.Day03 do
33
--- Day 3: Toboggan Trajectory ---
44
Problem Link: https://adventofcode.com/2020/day/3
55
"""
6-
use AdventOfCode.Helpers.InputReader, year: 2020, day: 3
7-
8-
alias AdventOfCode.Helpers.Transformers
6+
alias AdventOfCode.Helpers.{InputReader, Transformers}
97

108
@default_slope {3, 1}
119
@slopes [{1, 1}, {3, 1}, {5, 1}, {7, 1}, {1, 2}]
1210

13-
def run_1, do: input!() |> parse() |> traverse()
11+
def input, do: InputReader.read_from_file(2020, 3)
12+
13+
def run(input \\ input()) do
14+
input = parse(input)
15+
{run_1(input), run_2(input)}
16+
end
17+
18+
def run_1(input), do: traverse(input)
1419

15-
def run_2 do
16-
for result <- Enum.map(@slopes, &traverse(parse(input!()), &1)),
17-
reduce: 1 do
20+
def run_2(input) do
21+
for result <- Enum.map(@slopes, &traverse(input, &1)), reduce: 1 do
1822
acc -> acc * result
1923
end
2024
end
@@ -27,30 +31,9 @@ defmodule AdventOfCode.Y2020.Day03 do
2731
end
2832

2933
defp traverse(xy, slope \\ @default_slope), do: traverse(xy, 0, 0, 0, slope)
34+
defp traverse({_, _, cols}, r, _, res, _) when r > cols, do: res
3035

31-
defp traverse(
32-
{_, _, col_size},
33-
row,
34-
_,
35-
result,
36-
_
37-
)
38-
when row > col_size,
39-
do: result
40-
41-
defp traverse(
42-
{xy, row_size, _} = world,
43-
row,
44-
col,
45-
result,
46-
{right, down} = slope
47-
) do
48-
traverse(
49-
world,
50-
row + down,
51-
rem(col + right, row_size),
52-
result + ((xy[{row, col}] == "#" && 1) || 0),
53-
slope
54-
)
36+
defp traverse({yx, rows, _} = world, y, x, res, {right, down} = slope) do
37+
traverse(world, y + down, rem(x + right, rows), res + ((yx[{y, x}] == "#" && 1) || 0), slope)
5538
end
5639
end

lib/2020/day_04.ex

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ defmodule AdventOfCode.Y2020.Day04 do
33
--- Day 4: Passport Processing ---
44
Problem Link: https://adventofcode.com/2020/day/4
55
"""
6-
use AdventOfCode.Helpers.InputReader, year: 2020, day: 4
6+
alias AdventOfCode.Helpers.InputReader
77

8-
def run_1, do: input!() |> process() |> Enum.filter(&ok?(:k, &1)) |> length()
9-
def run_2, do: input!() |> process() |> Enum.filter(&ok?(:v, &1)) |> length()
8+
def input, do: InputReader.read_from_file(2020, 4)
109

11-
def process(data) do
10+
def run(input \\ input()) do
11+
input = parse(input)
12+
{Enum.count(input, &ok?(:k, &1)), Enum.count(input, &ok?(:v, &1))}
13+
end
14+
15+
def parse(data) do
1216
data
1317
|> String.split("\n")
1418
|> Enum.chunk_by(&(&1 == ""))

lib/2020/day_05.ex

+8-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ defmodule AdventOfCode.Y2020.Day05 do
33
--- Day 5: Binary Boarding ---
44
Problem Link: https://adventofcode.com/2020/day/5
55
"""
6-
use AdventOfCode.Helpers.InputReader, year: 2020, day: 5
6+
alias AdventOfCode.Helpers.{InputReader, Transformers}
77

8-
def run_1, do: input!() |> parse() |> Enum.max()
9-
def run_2, do: input!() |> parse() |> my_id()
8+
def input, do: InputReader.read_from_file(2020, 5)
109

11-
def parse(input), do: String.split(input, "\n") |> ids()
10+
def run(input \\ input()) do
11+
input = ids(Transformers.lines(input))
12+
{Enum.max(input), my_id(input)}
13+
end
1214

1315
def walk("F" <> pass, lo, hi), do: walk(pass, lo, mid(lo, hi))
1416
def walk("B" <> pass, lo, hi), do: walk(pass, mid(lo, hi) + 1, hi)
@@ -18,11 +20,11 @@ defmodule AdventOfCode.Y2020.Day05 do
1820
def walk("R" <> pass, lo, hi), do: walk(pass, mid(lo, hi) + 1, hi)
1921
def walk("", column, column), do: column
2022

23+
defp ids(data), do: Enum.map(data, &uid(walk(&1, 0, 127)))
24+
2125
defp mid(lo, hi), do: div(lo + hi, 2)
2226
defp uid({row, col}), do: row * 8 + col
2327

24-
defp ids(data), do: Enum.map(data, &uid(walk(&1, 0, 127)))
25-
2628
defp my_id(ids) do
2729
sorted_ids = Enum.sort(ids)
2830

lib/2020/day_06.ex

+9-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ defmodule AdventOfCode.Y2020.Day06 do
33
--- Day 6: Custom Customs ---
44
Problem Link: https://adventofcode.com/2020/day/6
55
"""
6-
use AdventOfCode.Helpers.InputReader, year: 2020, day: 6
6+
alias AdventOfCode.Helpers.InputReader
77

8-
def run_1, do: input!() |> parse() |> Enum.map(&answers/1) |> Enum.sum()
9-
def run_2, do: input!() |> parse() |> Enum.map(&unanimous_answers/1) |> Enum.sum()
8+
def input, do: InputReader.read_from_file(2020, 6)
9+
10+
def run(input \\ input()) do
11+
input = parse(input)
12+
{reducer(input, &answers/1), reducer(input, &unanimous_answers/1)}
13+
end
14+
15+
def reducer(input, mapper), do: Enum.reduce(input, 0, &(mapper.(&1) + &2))
1016

1117
def parse(input) do
1218
input

lib/2020/day_07.ex

+9-7
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ defmodule AdventOfCode.Y2020.Day07 do
33
--- Day 7: Handy Haversacks ---
44
Problem Link: https://adventofcode.com/2020/day/7
55
"""
6-
use AdventOfCode.Helpers.InputReader, year: 2020, day: 7
6+
alias AdventOfCode.Helpers.{InputReader, Transformers}
77

8-
def run_1, do: input!() |> parse() |> graph() |> ancestor_count("shiny gold")
9-
def run_2, do: (input!() |> parse() |> descendant_count("shiny gold")) - 1
8+
def input, do: InputReader.read_from_file(2020, 7)
109

11-
def parse(input), do: for(i <- String.split(input, "\n"), into: %{}, do: parse_rule(i))
10+
def run(input \\ input()) do
11+
input = Map.new(Transformers.lines(input), &parse_rule/1)
12+
{ancestor(graph(input)), descendant(input) - 1}
13+
end
1214

1315
def parse_rule(rule) do
1416
line = Regex.named_captures(~r/(?<src>.+) bags contain (?<bags>.+)\./, rule)
@@ -31,8 +33,8 @@ defmodule AdventOfCode.Y2020.Day07 do
3133
g
3234
end
3335

34-
defp ancestor_count(g, v), do: length(:digraph_utils.reaching([v], g)) - 1
36+
defp ancestor(g, v \\ "shiny gold"), do: length(:digraph_utils.reaching([v], g)) - 1
3537

36-
def descendant_count(g, v),
37-
do: Enum.reduce(g[v] || [], 1, fn {n, v}, num -> num + n * descendant_count(g, v) end)
38+
defp descendant(g, v \\ "shiny gold"),
39+
do: Enum.reduce(g[v] || [], 1, fn {n, v}, num -> num + n * descendant(g, v) end)
3840
end

lib/2020/day_08.ex

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ defmodule AdventOfCode.Y2020.Day08 do
33
--- Day 8: Handheld Halting ---
44
Problem Link: https://adventofcode.com/2020/day/8
55
"""
6-
use AdventOfCode.Helpers.InputReader, year: 2020, day: 8
6+
alias AdventOfCode.Helpers.{InputReader, Transformers}
77

8-
def run_1, do: input!() |> parse() |> exec() |> elem(1)
9-
def run_2, do: input!() |> parse() |> fix()
8+
def input, do: InputReader.read_from_file(2020, 8)
109

11-
def parse(input), do: Enum.map(String.split(input, "\n"), &parse_command/1)
10+
def run(input \\ input()) do
11+
input = input |> Transformers.lines() |> Enum.map(&parse_command/1)
12+
{elem(exec(input), 1), fix(input)}
13+
end
1214

1315
defp exec(prog), do: exec(prog, 0, 0, %{})
1416
defp exec(_, cur, acc, hist) when is_map_key(hist, cur), do: {:cont, acc}
@@ -22,9 +24,8 @@ defmodule AdventOfCode.Y2020.Day08 do
2224
end
2325
end
2426

25-
@rule ~r/(?<cmd>.+) (?<val>[+-]\d+)/
2627
defp parse_command(cmd) do
27-
@rule
28+
~r/(?<cmd>.+) (?<val>[+-]\d+)/
2829
|> Regex.named_captures(cmd)
2930
|> then(&{String.to_existing_atom(&1["cmd"]), String.to_integer(&1["val"])})
3031
end

lib/2020/day_09.ex

+7-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ defmodule AdventOfCode.Y2020.Day09 do
33
--- Day 9: Encoding Error ---
44
Problem Link: https://adventofcode.com/2020/day/9
55
"""
6-
use AdventOfCode.Helpers.InputReader, year: 2020, day: 9
6+
alias AdventOfCode.Helpers.{InputReader, Transformers}
77

8-
def run_1, do: input!() |> parse() |> find_invalid()
9-
def run_2, do: input!() |> parse() |> contiguous_list()
10-
def parse(input), do: Enum.map(String.split(input, "\n"), &String.to_integer/1)
8+
def input, do: InputReader.read_from_file(2020, 9)
9+
10+
def run(input \\ input()) do
11+
input = Transformers.int_lines(input)
12+
{find_invalid(input), contiguous_list(input)}
13+
end
1114

1215
defp find_invalid(data) do
1316
{frame, [v | _] = next} = Enum.split(data, 25)

lib/2020/day_10.ex

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ defmodule AdventOfCode.Y2020.Day10 do
33
--- Day 10: Adapter Array ---
44
Problem Link: https://adventofcode.com/2020/day/10
55
"""
6-
use AdventOfCode.Helpers.InputReader, year: 2020, day: 10
6+
alias AdventOfCode.Helpers.{InputReader, Transformers}
77

8-
def run_1, do: input!() |> parse() |> rates() |> multiply_1_3()
9-
def run_2, do: input!() |> parse() |> count()
8+
def input, do: InputReader.read_from_file(2020, 10)
109

11-
def parse(input),
12-
do: [0 | Enum.map(String.split(input, "\n"), &String.to_integer/1)] |> Enum.sort(:desc)
10+
def run(input \\ input()) do
11+
input = Enum.sort([0 | Transformers.int_lines(input)], :desc)
12+
{multiply_1_3(rates(input)), count(input)}
13+
end
1314

1415
defp rates(data) do
1516
my_rate = Enum.max(data) + 3

lib/2020/day_12.ex

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ defmodule AdventOfCode.Y2020.Day12 do
33
--- Day 12: Rain Risk ---
44
Problem Link: https://adventofcode.com/2020/day/12
55
"""
6-
use AdventOfCode.Helpers.InputReader, year: 2020, day: 12
6+
alias AdventOfCode.Helpers.{InputReader, Transformers}
77

8-
def run_1, do: input!() |> parse() |> go() |> distance()
9-
def run_2, do: input!() |> parse() |> waypoints() |> distance()
8+
def input, do: InputReader.read_from_file(2020, 12)
109

11-
def parse(input), do: input |> String.split("\n") |> parse_actions()
10+
def run(input \\ input()) do
11+
input = input |> Transformers.lines() |> parse_actions()
12+
{distance(go(input)), distance(waypoints(input))}
13+
end
1214

1315
defp parse_actions([]), do: []
1416

lib/2020/day_13.ex

+9-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@ defmodule AdventOfCode.Y2020.Day13 do
33
--- Day 13: Shuttle Search ---
44
Problem Link: https://adventofcode.com/2020/day/13
55
"""
6-
use AdventOfCode.Helpers.InputReader, year: 2020, day: 13
7-
6+
alias AdventOfCode.Helpers.InputReader
87
alias ExAlgo.Number.ChineseRemainder
98

10-
def run_1, do: input!() |> parse_1() |> earliest_bus() |> elem(3)
11-
def run_2, do: input!() |> parse_2() |> ChineseRemainder.compute()
9+
def input, do: InputReader.read_from_file(2020, 13)
10+
11+
def run(input \\ input()) do
12+
{run_1(input), run_2(input)}
13+
end
14+
15+
def run_1(input), do: input |> parse_1() |> earliest_bus() |> elem(3)
16+
def run_2(input), do: input |> parse_2() |> ChineseRemainder.compute()
1217

1318
def parse_1(input) do
1419
[time, ids] = String.split(input, "\n", trim: true)

lib/2020/day_14.ex

+11-5
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,21 @@ defmodule AdventOfCode.Y2020.Day14 do
66
Hat-tip to Christian Blavier from ElixirForum.
77
https://github.com/cblavier/advent/tree/master/lib/2020/day14
88
"""
9-
use AdventOfCode.Helpers.InputReader, year: 2020, day: 14
9+
alias AdventOfCode.Helpers.{InputReader, Transformers}
1010

1111
import Bitwise
1212

13-
def run_1, do: input!() |> parse() |> run(&instruction_1/2) |> memory_sum()
14-
def run_2, do: input!() |> parse() |> run(&instruction_2/2) |> memory_sum()
15-
def parse(input), do: String.split(input, "\n")
13+
def input, do: InputReader.read_from_file(2020, 14)
1614

17-
def run(data, instruction), do: Enum.reduce(data, {%{}, nil}, instruction)
15+
def run(input \\ input()) do
16+
input = Transformers.lines(input)
17+
{run_1(input), run_2(input)}
18+
end
19+
20+
def run_1(input), do: input |> exec(&instruction_1/2) |> memory_sum()
21+
def run_2(input), do: input |> exec(&instruction_2/2) |> memory_sum()
22+
23+
defp exec(data, instruction), do: Enum.reduce(data, {%{}, nil}, instruction)
1824

1925
defp instruction_1("mask" <> _ = line, {memory, _mask}) do
2026
mask = line |> String.split(" = ") |> Enum.at(-1)

lib/2020/day_16.ex

+11-5
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ defmodule AdventOfCode.Y2020.Day16 do
33
--- Day 16: Ticket Translation ---
44
Problem Link: https://adventofcode.com/2020/day/16
55
"""
6-
use AdventOfCode.Helpers.InputReader, year: 2020, day: 16
6+
alias AdventOfCode.Helpers.InputReader
77

8-
def run_1 do
9-
{ranges, tickets} = process(input!())
8+
def input, do: InputReader.read_from_file(2020, 16)
9+
10+
def run(input \\ input()) do
11+
{run_1(input), run_2(input)}
12+
end
13+
14+
def run_1(input) do
15+
{ranges, tickets} = parse(input)
1016

1117
Enum.sum(
1218
Enum.filter(tickets, fn ticket ->
@@ -15,8 +21,8 @@ defmodule AdventOfCode.Y2020.Day16 do
1521
)
1622
end
1723

18-
def run_2, do: {:not_implemented, 2}
19-
def process(input), do: {get_ranges(input), nearby_tickets(input)}
24+
def run_2(_input), do: {:todo, 2}
25+
def parse(input), do: {get_ranges(input), nearby_tickets(input)}
2026

2127
def get_ranges(input) do
2228
Regex.scan(~r/\d+\-\d+/, input)

0 commit comments

Comments
 (0)