summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexandre Jesus <adbjesus@gmail.com>2024-12-03 00:26:58 +0000
committerAlexandre Jesus <adbjesus@gmail.com>2024-12-03 00:41:29 +0000
commit6da10b268dd639594cb5a8cc6e9bbb86ce4beb5e (patch)
tree12242f658c49c0cbcf4f8bf987f0f8a52ee51f2f
downloadaoc2024-6da10b268dd639594cb5a8cc6e9bbb86ce4beb5e.tar.gz
aoc2024-6da10b268dd639594cb5a8cc6e9bbb86ce4beb5e.zip
Days 1 and 2
-rw-r--r--.gitignore3
-rw-r--r--flake.lock58
-rw-r--r--flake.nix22
-rw-r--r--src/day01.exs20
-rw-r--r--src/day02.exs36
5 files changed, 139 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..37c186c
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+.direnv/
+.envrc
+data/ \ No newline at end of file
diff --git a/flake.lock b/flake.lock
new file mode 100644
index 0000000..7e3bd39
--- /dev/null
+++ b/flake.lock
@@ -0,0 +1,58 @@
+{
+ "nodes": {
+ "flake-utils": {
+ "inputs": {
+ "systems": "systems"
+ },
+ "locked": {
+ "lastModified": 1731533236,
+ "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
+ "owner": "numtide",
+ "repo": "flake-utils",
+ "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
+ "type": "github"
+ },
+ "original": {
+ "owner": "numtide",
+ "repo": "flake-utils",
+ "type": "github"
+ }
+ },
+ "nixpkgs": {
+ "locked": {
+ "lastModified": 1730327045,
+ "narHash": "sha256-xKel5kd1AbExymxoIfQ7pgcX6hjw9jCgbiBjiUfSVJ8=",
+ "path": "/nix/store/ylrrz211xgjzkcpyiafpq9y2yws7fyah-source",
+ "rev": "080166c15633801df010977d9d7474b4a6c549d7",
+ "type": "path"
+ },
+ "original": {
+ "id": "nixpkgs",
+ "type": "indirect"
+ }
+ },
+ "root": {
+ "inputs": {
+ "flake-utils": "flake-utils",
+ "nixpkgs": "nixpkgs"
+ }
+ },
+ "systems": {
+ "locked": {
+ "lastModified": 1681028828,
+ "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
+ "owner": "nix-systems",
+ "repo": "default",
+ "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
+ "type": "github"
+ },
+ "original": {
+ "owner": "nix-systems",
+ "repo": "default",
+ "type": "github"
+ }
+ }
+ },
+ "root": "root",
+ "version": 7
+}
diff --git a/flake.nix b/flake.nix
new file mode 100644
index 0000000..3e5ccdd
--- /dev/null
+++ b/flake.nix
@@ -0,0 +1,22 @@
+{
+ description = "Development shell for aoc2024";
+
+ inputs = {
+ flake-utils = {
+ url = "github:numtide/flake-utils";
+ };
+ };
+
+ outputs = { self, nixpkgs, flake-utils }:
+ flake-utils.lib.eachDefaultSystem (system:
+ let
+ pkgs = import nixpkgs { inherit system; };
+ in {
+ devShells.default = pkgs.mkShell {
+ buildInputs = [
+ pkgs.elixir
+ pkgs.elixir-ls
+ ];
+ };
+ });
+}
diff --git a/src/day01.exs b/src/day01.exs
new file mode 100644
index 0000000..b9b346d
--- /dev/null
+++ b/src/day01.exs
@@ -0,0 +1,20 @@
+[l1, l2] = IO.read(:stdio, :eof)
+|> String.split([" ", "\n"], trim: true)
+|> Enum.map(&String.to_integer/1)
+|> Enum.with_index()
+|> Enum.split_with(fn {_v, i} -> rem(i, 2) == 0 end)
+|> Tuple.to_list()
+|> Enum.map(fn l -> Enum.map(l, fn {v, _i} -> v end) end)
+|> Enum.map(&Enum.sort/1)
+
+ans1 = Enum.zip(l1, l2)
+|> Enum.map(fn {v1, v2} -> abs(v1 - v2) end)
+|> Enum.sum()
+
+IO.puts(ans1)
+
+c2 = Enum.frequencies(l2)
+
+ans2 = Enum.map(l1, &(&1 * Map.get(c2, &1, 0))) |> Enum.sum()
+
+IO.puts(ans2)
diff --git a/src/day02.exs b/src/day02.exs
new file mode 100644
index 0000000..ae7bfde
--- /dev/null
+++ b/src/day02.exs
@@ -0,0 +1,36 @@
+defmodule Day02 do
+ def parse_report(line) do
+ line
+ |> String.trim()
+ |> String.split(" ")
+ |> Enum.map(&String.to_integer/1)
+ end
+
+ def is_report_safe(report) do
+ diffs = report
+ |> Enum.chunk_every(2, 1, :discard)
+ |> Enum.map(fn [a, b] -> b - a end)
+
+ Enum.all?(diffs, fn x -> 1 <= x and x <= 3 end) or
+ Enum.all?(diffs, fn x -> -3 <= x and x <= -1 end)
+ end
+
+ def is_report_safe_with_dampener(report) do
+ is_report_safe(report) or
+ Enum.any?(0..Enum.count(report)-1,
+ fn i -> List.delete_at(report, i) |> is_report_safe() end)
+ end
+end
+
+reports = IO.stream(:stdio, :line)
+|> Enum.map(&Day02.parse_report/1)
+
+ans1 = reports
+|> Enum.count(&Day02.is_report_safe/1)
+
+IO.puts(ans1)
+
+ans2 = reports
+|> Enum.count(&Day02.is_report_safe_with_dampener/1)
+
+IO.puts(ans2)