— Day 1: Historian Hysteria —
Mix.install([{:kino_aoc, "~> 0.1"}])
Part 1
{:ok, puzzle_input} =
KinoAOC.download_puzzle("2024", "1", System.fetch_env!("LB_AOC_SESSION_COOKIE"))
{:ok,
"85215 94333\n24582 34558\n98037 94333\n75786 66247\n45656 85863\n70998 87003\n30367 62007\n81780 23161\n90260 65786\n24710 86514\n14018 34310\n43565 47888\n59781 79173\n47761 71538\n85892 22181\n25701 61839\n18264 33438\n33747 43258\n39697 94333\n61838 37358\n70437 22496\n23562 26799\n11216 34419\n63191 11393\n88615 31544\n93481 62720\n29534 40919\n29935 18758\n95190 87857\n51306 33515\n30938 29652\n77253 30646\n66807 67041\n75203 67041\n15696 61800\n73541 29496\n52063 51002\n55826 40919\n79183 35633\n77348 44025\n65423 33750\n65816 10624\n52110 89611\n18201 22051\n27748 66807\n92259 42784\n14988 33500\n58623 64359\n88260 90432\n62079 77685\n45698 40919\n91705 64359\n53661 73356\n93541 28465\n76689 96938\n60498 93056\n25784 65786\n32811 24732\n62264 72520\n41995 43258\n38545 84426\n19555 72520\n73703 78074\n86068 72520\n25850 87009\n49433 10684\n74291 61839\n40711 10516\n37728 82526\n80842 40919\n24677 23062\n29575 52605\n20692 77673\n82910 68845\n33830 62419\n68434 73604\n62401 84426\n82646 70854\n38398 56339\n79408 51171\n63734 27770\n13556 24577\n75123 37015\n61363 69734\n95609 86251\n88545 27770\n50893 74026\n14882 87009\n91379 33438\n60224 52605\n72679 47888\n75505 59781\n10186 62720\n88379 64359\n90496 67041\n73586 93056\n95670 63191\n87141 59781\n13919 74934\n98555 74846\n87897 32687\n21880 62687\n29658 50213\n96248 83521\n15492 86906\n29496 53112\n25334 27594\n20643 86251\n56611 39477\n29416 24577\n74832 63164\n43193 43258\n81875 75725\n61800 61927\n24065 94333\n83853 31467\n17180 10130\n43720 37354\n65647 45425\n51476 10461\n57574 93056\n31072 83739\n75113 19490\n79797 26799\n72214 11294\n13116 48228\n93787 33438\n46134 19254\n40985 28931\n25988 11393\n23062 59151\n79383 11393\n75339 89198\n79708 29496\n13058 61839\n16278 45698\n80414 14142\n16351 40919\n54813 43258\n81250 87434\n50921 61839\n53645 43258\n70432 22523\n81070 32306\n38985 31359\n19798 99501\n92509 43258\n25063 66000\n44665 33438\n64806 93056\n74946 26300\n48029 30646\n77560 83292\n82468 95064\n29223 71914\n87584 63191\n93056 26799\n55648 29949\n14352 48844\n49873 34111\n56227 45698\n67703 63191\n68135 30646\n44722 90812\n65476 78955\n82704 47888\n50424 23079\n51796 70111\n85079 22013\n61910 66098\n84132 89544\n47927 62720\n61766 78856\n38484 42757\n57255 93056\n48855 78836\n73127 78836\n21236 61839\n15138 94333\n26829 65786\n48823 93140\n28401 63191\n75082 42757\n41215 26799\n67176 19254\n13673 78836\n53842 26386\n41964 59781\n46737 21097\n52719 29652\n20212 76559\n89201 82291\n98568 63191\n55425 93322\n69878 63283\n36501 27429\n16724 47888\n61592 81085\n41114 93056\n44198 47888\n47061 86251\n45289 56992\n47597 15576\n18834 66807\n31018 40919\n40775 78266\n25185 76021\n48462 64359\n26799 32811\n37199 19555\n94411 77560\n45846 48696\n16862 77508\n77988 94333\n89121 66807\n41240 94333\n88088 59151\n52125 29652\n12932 13054\n61211 28578\n26024 87009\n26312 55718\n14585 45698\n65939 91730\n68138 59781\n70317 78836\n83217 78836\n22036 26799\n63215 66807\n54110 21264\n12981 33069\n61857 29652\n54637 94333\n15241 93056\n19897 33438\n77229 86251\n55685 37316\n30161 66807\n61780 14101\n19289 17661\n48121 51324\n99355 72435\n62767 32811\n98670 19490\n85332 31012\n92588 18461\n67574 94379\n90643 66607\n95487 66807\n33530 46429\n87009 23062\n61978 27770\n53891 19254\n64426 58297\n70651 61800\n40653 40474\n45888 33438\n55241 44527\n11126 42398\n92609 93056\n21760 22129\n72079 51542\n41177 26196\n67610 68036\n62720 45698\n38295 64359\n69482 93056\n24219 66807\n69511 45698\n30242 24577\n46626 18573\n36316 80909\n40214 72520\n16785 10921\n27770 11393\n70571 43258\n50406 30875\n76411 98707\n75186 77560\n80536 11587\n74209 78836\n20188 72520\n30587 57435\n44135 58144\n29251 46185\n73362 45698\n52443 53860\n61094 46357\n12167 47231\n51846 62720\n75168 23761\n97509 65786\n29546 " <> ...}
defmodule HistorianHysteria do
# 1 | 2
def lists(input) do
input
|> String.split("\n")
|> Enum.reduce([[], []], fn ids, [list_l, list_r] ->
[l, r] = String.split(ids)
[[String.to_integer(l) | list_l], [String.to_integer(r) | list_r]]
end)
end
# 1
def zip_sort([l, r]), do: [Enum.sort(l), Enum.sort(r)]
# 1
def sum_distances([l, r]) do
Enum.zip_reduce(l, r, 0, fn idl, idr, acc ->
abs(idl - idr) + acc
end)
end
# 2
def similarity_score([l, r]) do
freq = Enum.frequencies(r)
Enum.reduce(l, 0, fn id, acc ->
acc + Map.get(freq, id, 0) * id
end)
end
end
{:module, HistorianHysteria, <<70, 79, 82, 49, 0, 0, 13, ...>>, {:similarity_score, 1}}
import HistorianHysteria
puzzle_input
|> lists()
|> zip_sort()
|> sum_distances()
1319616
import HistorianHysteria
puzzle_input
|> lists()
|> similarity_score()
27267728