-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_1.exs
75 lines (60 loc) · 2.8 KB
/
day_1.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
defmodule Day1 do
def find_pair(list) do
Enum.reduce list, [], fn x, acc ->
case inner_pair(x, list) do
y_list when y_list != [] -> [x, hd(y_list)]
_ -> acc
end
end
end
def find_three_numbers(list) do
Enum.reduce list, [], fn x, acc ->
Enum.reduce list, acc, fn y, acc ->
case inner_pair(x, y, list) do
z_list when z_list != [] -> [x, y, hd(z_list)]
_ -> acc
end
end
end
end
def multiply_pair(list) do
list |> find_pair |> Enum.reduce(1, fn x, acc -> x * acc end)
end
def multiply_three_numbers(list) do
list |> find_three_numbers |> Enum.reduce(1, fn x, acc -> x * acc end)
end
defp inner_pair(x, list) do
Enum.filter list, fn y ->
x + y == 2020
end
end
defp inner_pair(x, y, list), do: inner_pair(x + y, list)
end
case System.argv() do
["--test"] ->
ExUnit.start()
defmodule Day1Test do
use ExUnit.Case
import Day1
test "Find a pair which sum of them will be 2020" do
numbers = [1721, 979, 366, 299, 675, 1456]
assert Enum.sum(find_pair(numbers)) == 2020
end
test "Find three number which sum of them will be 2020" do
numbers = [1721, 979, 366, 299, 675, 1456]
assert Enum.sum(find_three_numbers(numbers)) == 2020
end
test "Multiply a pair which sum of them will be 2020" do
numbers = [1721, 979, 366, 299, 675, 1456]
assert multiply_pair(numbers) == 514579
end
test "Multiply three numbers which sum of them will be 2020" do
numbers = [1721, 979, 366, 299, 675, 1456]
assert multiply_three_numbers(numbers) == 241861950
end
end
_ ->
numbers = [1597,1857,1703,1956,1809,1683,1629,230,1699,1875,1564,1700,1911,1776,1955,1585,1858,1725,1813,1568,1962,1535,487,1621,1620,1573,1918,1794,2003,1957,1840,1936,285,1630,1753,1649,1951,1968,1916,1694,1593,1980,1806,1779,1637,1674,1842,1659,1553,1846,1677,1944,1811,1645,1784,1791,1988,1864,1596,1773,1132,1715,1938,1901,1617,1892,1708,1788,1765,1684,1595,1971,1798,1543,507,1772,1757,1950,1844,1898,1671,1602,1599,1524,2005,1855,1624,1884,1990,1604,1873,1736,1747,1900,1534,1713,1690,1525,1838,587,74,1979,1635,1896,1580,1727,1994,1940,1819,1758,1852,1639,1754,1559,1919,1879,1874,1921,1575,1693,1710,1949,1719,1933,1673,1909,1989,1897,909,1889,1641,1658,1530,1541,1952,1627,1839,1803,833,1527,1756,2009,1605,1548,1660,1966,1770,1552,1939,1726,382,1665,1960,1733,1983,1544,1974,1985,1625,609,1931,1749,1975,1562,1563,1922,2008,2010,1704,1545,1636,1762,1841,1717,622,2007,1670,1771,1910,1978,1615,1805,1999,1643,1748,1531,1539,1787,1722,1111,1774,1540,1607,1902,1991,1763,1691,1812,1783,1579]
IO.inspect(Day1.multiply_pair(numbers), label: "part 1")
IO.inspect(Day1.multiply_three_numbers(numbers), label: "part 2")
end