Skip to content

Commit ad5682a

Browse files
committed
Remove input macro from 2019 solutions
1 parent ad5a0ba commit ad5682a

16 files changed

+130
-116
lines changed

lib/2019/day_01.ex

+12-9
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,23 @@ defmodule AdventOfCode.Y2019.Day01 do
33
--- Day 1: The Tyranny of the Rocket Equation ---
44
Problem Link: https://adventofcode.com/2019/day/1
55
"""
6-
use AdventOfCode.Helpers.InputReader, year: 2019, day: 1
6+
alias AdventOfCode.Helpers.InputReader
77

8-
@spec run_1 :: non_neg_integer()
9-
def run_1 do
10-
input!()
11-
|> parse()
8+
def input, do: InputReader.read_from_file(2019, 1)
9+
10+
def run(input \\ input()) do
11+
parsed_data = parse(input)
12+
{run_1(parsed_data), run_2(parsed_data)}
13+
end
14+
15+
def run_1(input) do
16+
input
1217
|> Enum.map(&fuel_required/1)
1318
|> Enum.sum()
1419
end
1520

16-
@spec run_2 :: non_neg_integer()
17-
def run_2 do
18-
input!()
19-
|> parse()
21+
def run_2(input) do
22+
input
2023
|> Enum.map(fn mass ->
2124
required_fuel = fuel_required(mass)
2225
refuel(required_fuel, required_fuel)

lib/2019/day_02.ex

+9-10
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ defmodule AdventOfCode.Y2019.Day02 do
33
--- Day 2: 1202 Program Alarm ---
44
Problem Link: https://adventofcode.com/2019/day/2
55
"""
6-
use AdventOfCode.Helpers.InputReader, year: 2019, day: 2
7-
6+
alias AdventOfCode.Helpers.InputReader
87
alias AdventOfCode.Y2019.IntCode
98

9+
def input, do: InputReader.read_from_file(2019, 2)
10+
1011
@range 99..0
1112

12-
@spec run_1 :: integer()
13-
def run_1 do
13+
def run(input \\ input()), do: {run_1(input), run_2(input)}
14+
15+
def run_1(input) do
1416
{:ok, pid} =
15-
input!()
17+
input
1618
|> parse()
1719
|> fix1202()
1820
|> IntCode.start_link()
@@ -21,9 +23,8 @@ defmodule AdventOfCode.Y2019.Day02 do
2123
IntCode.get_output(pid)
2224
end
2325

24-
@spec run_2 :: integer | nil
25-
def run_2 do
26-
memory = input!() |> parse()
26+
def run_2(input) do
27+
memory = input |> parse()
2728

2829
pairs = for i <- @range, j <- @range, do: {i, j}
2930

@@ -53,6 +54,4 @@ defmodule AdventOfCode.Y2019.Day02 do
5354
|> List.replace_at(1, one)
5455
|> List.replace_at(2, two)
5556
end
56-
57-
def run, do: {run_1(), run_2()}
5857
end

lib/2019/day_03.ex

+12-7
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,25 @@ defmodule AdventOfCode.Y2019.Day03 do
33
--- Day 3: Crossed Wires ---
44
Problem Link: https://adventofcode.com/2019/day/3
55
"""
6-
use AdventOfCode.Helpers.InputReader, year: 2019, day: 3
6+
alias AdventOfCode.Helpers.InputReader
77

88
@origin {0, 0}
99

10-
def run_1 do
11-
input!()
12-
|> parse()
10+
def input, do: InputReader.read_from_file(2019, 3)
11+
12+
def run(input \\ input()) do
13+
input = parse(input)
14+
{run_1(input), run_2(input)}
15+
end
16+
17+
def run_1(input) do
18+
input
1319
|> Enum.map(&move(&1, @origin, []))
1420
|> nearest_intersection()
1521
end
1622

17-
def run_2 do
18-
input!()
19-
|> parse()
23+
def run_2(input) do
24+
input
2025
|> Enum.map(fn wire ->
2126
wire
2227
|> move(@origin, [])

lib/2019/day_04.ex

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ defmodule AdventOfCode.Y2019.Day04 do
33
--- Day 4: Secure Container ---
44
Problem Link: https://adventofcode.com/2019/day/4
55
"""
6-
@range 245_182..790_572
6+
def input, do: 245_182..790_572
77

8-
def run_1 do
9-
Enum.count(for n <- @range, n |> adjacent() and n |> increasing(), do: n)
8+
def run(input \\ input()), do: {run_1(input), run_2(input)}
9+
10+
def run_1(range) do
11+
Enum.count(for n <- range, n |> adjacent() and n |> increasing(), do: n)
1012
end
1113

12-
def run_2 do
13-
Enum.count(for n <- @range, n |> adjacent_2() and n |> increasing(), do: n)
14+
def run_2(range) do
15+
Enum.count(for n <- range, n |> adjacent_2() and n |> increasing(), do: n)
1416
end
1517

1618
def adjacent(number) do

lib/2019/day_05.ex

+13-7
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ defmodule AdventOfCode.Y2019.Day05 do
33
--- Day 5: Sunny with a Chance of Asteroids ---
44
Problem Link: https://adventofcode.com/2019/day/5
55
"""
6-
use AdventOfCode.Helpers.InputReader, year: 2019, day: 5
7-
6+
alias AdventOfCode.Helpers.InputReader
87
alias AdventOfCode.Y2019.IntCode
98

10-
def parse(data), do: data |> String.split(",") |> Enum.map(&String.to_integer/1)
9+
def input, do: InputReader.read_from_file(2019, 5)
10+
11+
def run(input \\ input()) do
12+
input = parse(input)
13+
{run_1(input), run_2(input)}
14+
end
1115

12-
def run_1 do
13-
{:ok, pid} = input!() |> parse() |> IntCode.start_link()
16+
def run_1(input) do
17+
{:ok, pid} = input |> IntCode.start_link()
1418
%{output: output} = IntCode.run(pid)
1519

1620
output
@@ -21,7 +25,9 @@ defmodule AdventOfCode.Y2019.Day05 do
2125
end
2226
end
2327

24-
def run_2 do
25-
{:not_implemented, 2}
28+
def run_2(_) do
29+
{:todo, 2}
2630
end
31+
32+
def parse(data), do: data |> String.split(",") |> Enum.map(&String.to_integer/1)
2733
end

lib/2019/day_06.ex

+12-7
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,23 @@ defmodule AdventOfCode.Y2019.Day06 do
33
--- Day 6: Universal Orbit Map ---
44
Problem Link: https://adventofcode.com/2019/day/6
55
"""
6-
use AdventOfCode.Helpers.InputReader, year: 2019, day: 6
6+
alias AdventOfCode.Helpers.InputReader
77

8-
def run_1 do
9-
input!()
10-
|> parse()
8+
def input, do: InputReader.read_from_file(2019, 6)
9+
10+
def run(input \\ input()) do
11+
input = parse(input)
12+
{run_1(input), run_2(input)}
13+
end
14+
15+
def run_1(input) do
16+
input
1117
|> to_graph(:digraph.new([:acyclic]))
1218
|> count_orbits()
1319
end
1420

15-
def run_2 do
16-
input!()
17-
|> parse()
21+
def run_2(input) do
22+
input
1823
|> to_graph(:digraph.new())
1924
|> count_orbital_transfers()
2025
end

lib/2019/day_08.ex

+20-14
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,45 @@ defmodule AdventOfCode.Y2019.Day08 do
33
--- Day 8: Space Image Format ---
44
Problem Link: https://adventofcode.com/2019/day/8
55
"""
6-
use AdventOfCode.Helpers.InputReader, year: 2019, day: 8
6+
alias AdventOfCode.Helpers.InputReader
77

88
@width 25
99
@height 6
1010
@printchar "▒"
1111

12-
def run_1 do
13-
input!()
14-
|> process()
12+
def input, do: InputReader.read_from_file(2019, 8)
13+
14+
def run(input \\ input()) do
15+
input = parse(input)
16+
17+
{run_1(input), run_2(input)}
18+
end
19+
20+
def run_1(input) do
21+
input
1522
|> Enum.map(fn layer -> Enum.flat_map(layer, & &1) end)
1623
|> Enum.min_by(&Enum.count(&1, fn x -> x == 0 end))
1724
|> then(fn x -> Enum.count(x, &(&1 == 2)) * Enum.count(x, &(&1 == 1)) end)
1825
end
1926

20-
def run_2 do
21-
input!()
22-
|> process()
27+
def run_2(input) do
28+
input
2329
|> Enum.map(fn layer -> Enum.flat_map(layer, & &1) end)
2430
|> Enum.reduce(&overlay(&2, &1))
2531
|> Enum.join()
2632
|> print_image()
2733
end
2834

29-
def process(data) do
35+
def parse(data) do
3036
data
31-
|> String.codepoints()
37+
|> String.graphemes()
3238
|> Enum.map(&String.to_integer/1)
3339
|> chunkify()
3440
end
3541

36-
defp overlay(l1, l2) do
37-
l1
38-
|> Enum.zip(l2)
42+
defp overlay(layer_1, layer_2) do
43+
layer_1
44+
|> Enum.zip(layer_2)
3945
|> Enum.map(fn
4046
{2, x} -> x
4147
{1, _} -> 1
@@ -49,9 +55,9 @@ defmodule AdventOfCode.Y2019.Day08 do
4955
|> String.replace("0", " ")
5056
|> String.codepoints()
5157
|> chunkify()
52-
|> hd()
58+
|> List.first()
5359
|> Enum.map_join("\n", &Enum.join/1)
54-
|> IO.puts()
60+
|> tap(&IO.puts/1)
5561
end
5662

5763
defp chunkify(data) do

lib/2019/day_10.ex

+14-8
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,27 @@ defmodule AdventOfCode.Y2019.Day10 do
33
--- Day 10: Monitoring Station ---
44
Problem Link: https://adventofcode.com/2019/day/10
55
"""
6-
use AdventOfCode.Helpers.InputReader, year: 2019, day: 10
6+
alias AdventOfCode.Helpers.InputReader
77

88
@asteroid "#"
99

10-
def run_1 do
11-
input!()
12-
|> parse()
10+
def input, do: InputReader.read_from_file(2019, 10)
11+
12+
def run(input \\ input()) do
13+
input = parse(input)
14+
15+
{run_1(input), run_2(input)}
16+
end
17+
18+
def run_1(input) do
19+
input
1320
|> visible_asteroid_count()
1421
|> Enum.max_by(fn {_, number} -> number end)
1522
|> elem(1)
1623
end
1724

18-
def run_2 do
19-
input!()
20-
|> parse()
25+
def run_2(input) do
26+
input
2127
|> the_200th_asteroid()
2228
|> result()
2329
end
@@ -28,7 +34,7 @@ defmodule AdventOfCode.Y2019.Day10 do
2834
|> Enum.with_index()
2935
|> Enum.flat_map(fn {line, row} ->
3036
line
31-
|> String.codepoints()
37+
|> String.graphemes()
3238
|> Enum.with_index()
3339
|> Enum.map(fn {maybe_asteroid, col} ->
3440
{{col, row}, maybe_asteroid}

test/2019/day_01_test.exs

+2-6
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ defmodule AdventOfCode.Y2019.Day01Test do
55

66
alias AdventOfCode.Y2019.Day01, as: Solution
77

8-
test "Year 2019, Day 1, Part 1" do
9-
assert Solution.run_1() == 3_421_505
10-
end
11-
12-
test "Year 2019, Day 1, Part 2" do
13-
assert Solution.run_2() == 5_129_386
8+
test "Year 2019, Day 1" do
9+
assert Solution.run() == {3_421_505, 5_129_386}
1410
end
1511
end

test/2019/day_02_test.exs

+2-6
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ defmodule AdventOfCode.Y2019.Day02Test do
55

66
alias AdventOfCode.Y2019.Day02, as: Solution
77

8-
test "Year 2019, Day 2, Part 1" do
9-
assert Solution.run_1() == 3_562_624
10-
end
11-
12-
test "Year 2019, Day 2, Part 2" do
13-
assert Solution.run_2() == 8298
8+
test "Year 2019, Day 2" do
9+
assert Solution.run() == {3_562_624, 8298}
1410
end
1511
end

test/2019/day_03_test.exs

+2-6
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ defmodule AdventOfCode.Y2019.Day03Test do
55

66
alias AdventOfCode.Y2019.Day03, as: Solution
77

8-
test "Year 2019, Day 3, Part 1" do
9-
assert Solution.run_1() == 1195
10-
end
11-
12-
test "Year 2019, Day 3, Part 2" do
13-
assert Solution.run_2() == 91_518
8+
test "Year 2019, Day 3" do
9+
assert Solution.run() == {1195, 91_518}
1410
end
1511
end

test/2019/day_04_test.exs

+2-6
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ defmodule AdventOfCode.Y2019.Day04Test do
55

66
alias AdventOfCode.Y2019.Day04, as: Solution
77

8-
test "Year 2019, Day 4, Part 1" do
9-
assert Solution.run_1() == 1099
10-
end
11-
12-
test "Year 2019, Day 4, Part 2" do
13-
assert Solution.run_2() == 710
8+
test "Year 2019, Day 4" do
9+
assert Solution.run() == {1099, 710}
1410
end
1511
end

test/2019/day_05_test.exs

+2-7
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@ defmodule AdventOfCode.Y2019.Day05Test do
55

66
alias AdventOfCode.Y2019.Day05, as: Solution
77

8-
test "Year 2019, Day 5, Part 1" do
9-
assert Solution.run_1() == 6_745_903
10-
end
11-
12-
@tag :skip
13-
test "Year 2019, Day 5, Part 2" do
14-
assert Solution.run_2() == :not_implemented
8+
test "Year 2019, Day 5" do
9+
assert Solution.run() == {6_745_903, {:todo, 2}}
1510
end
1611
end

test/2019/day_06_test.exs

+2-6
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ defmodule AdventOfCode.Y2019.Day06Test do
55

66
alias AdventOfCode.Y2019.Day06, as: Solution
77

8-
test "Year 2019, Day 6, Part 1" do
9-
assert Solution.run_1() == 147_807
10-
end
11-
12-
test "Year 2019, Day 6, Part 2" do
13-
assert Solution.run_2() == 229
8+
test "Year 2019, Day 6" do
9+
assert Solution.run() == {147_807, 229}
1410
end
1511
end

0 commit comments

Comments
 (0)