Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Day 01

2020/day01.livemd

Day 01

Mix.install([:kino_aoc])

Section

{:ok, puzzle_input} =
  KinoAOC.download_puzzle("2020", "1", System.fetch_env!("LB_ADVENT_OF_CODE_SESSION"))
{:ok,
 "1706\n1466\n1427\n1744\n1684\n1386\n2001\n1750\n1753\n1770\n1559\n1616\n1408\n1860\n1940\n2002\n1862\n1918\n1456\n1209\n1840\n1462\n1783\n1644\n1901\n1791\n1506\n2005\n1338\n1383\n1420\n1631\n1784\n1897\n1771\n1588\n1955\n1937\n1392\n1396\n1803\n1429\n1407\n1698\n1562\n1913\n1678\n1198\n1398\n1703\n1831\n1489\n1782\n1864\n1708\n1397\n1915\n1953\n1395\n1610\n1549\n1564\n1973\n1931\n2009\n1980\n1800\n1443\n1993\n1900\n1964\n1581\n1904\n1665\n1567\n1057\n1805\n1402\n1878\n1729\n1825\n1682\n1719\n1469\n1004\n1591\n1594\n811\n1523\n1424\n1756\n373\n1442\n1718\n1411\n1892\n1820\n1977\n1871\n1890\n1653\n1372\n1475\n1476\n1422\n2004\n1755\n1676\n639\n1425\n1853\n1712\n1525\n1514\n1455\n1658\n1963\n1579\n1861\n1458\n1474\n1613\n1681\n1586\n1441\n1499\n1865\n1735\n1989\n1952\n792\n1669\n1509\n1481\n1893\n1445\n1834\n1779\n1732\n1826\n1595\n1829\n449\n1920\n1707\n1780\n1935\n1867\n1769\n1107\n919\n1382\n1604\n1875\n1453\n1496\n1946\n1659\n1570\n1692\n1630\n1638\n1922\n1691\n1580\n1880\n1482\n1762\n1775\n1376\n1434\n1856\n1971\n1646\n1951\n1416\n1889\n1773\n1814\n1471\n1488\n1736\n1743\n1459\n1389\n1498\n1663\n1611\n1727\n1699\n1624\n1511\n1767\n1754\n1785\n1491\n1235\n1510\n1500\n1485"}
input =
  puzzle_input
  |> String.split("\n", trim: true)
  |> Enum.map(&String.to_integer/1)
  |> Enum.sort()
[373, 449, 639, 792, 811, 919, 1004, 1057, 1107, 1198, 1209, 1235, 1338, 1372, 1376, 1382, 1383,
 1386, 1389, 1392, 1395, 1396, 1397, 1398, 1402, 1407, 1408, 1411, 1416, 1420, 1422, 1424, 1425,
 1427, 1429, 1434, 1441, 1442, 1443, 1445, 1453, 1455, 1456, 1458, 1459, 1462, 1466, 1469, 1471,
 1474, ...]
defmodule Report do
  def find([], _, _), do: nil
  def find(_, _, sum) when sum < 0, do: nil
  def find([a | _], 1, a), do: [a]

  def find([a | rest], n, sum) do
    case find(rest, n - 1, sum - a) do
      nil -> find(rest, n, sum)
      nums when is_list(nums) -> [a | nums]
    end
  end
end
{:module, Report, <<70, 79, 82, 49, 0, 0, 8, ...>>, {:find, 3}}

Part 1

Report.find(input, 2, 2020) |> Enum.product()
980499

Part 2

Report.find(input, 3, 2020) |> Enum.product()
200637446