solve 2024 day 1
This commit is contained in:
parent
fbf7999209
commit
4d1b02e916
15 changed files with 117 additions and 137 deletions
5
2024/.formatter.exs
Normal file
5
2024/.formatter.exs
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
# Used by "mix format"
|
||||||
|
[
|
||||||
|
plugins: [Styler],
|
||||||
|
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
|
||||||
|
]
|
23
2024/.github/workflows/test.yml
vendored
23
2024/.github/workflows/test.yml
vendored
|
@ -1,23 +0,0 @@
|
||||||
name: test
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
- main
|
|
||||||
pull_request:
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
- uses: erlef/setup-beam@v1
|
|
||||||
with:
|
|
||||||
otp-version: "27.1.2"
|
|
||||||
gleam-version: "1.6.1"
|
|
||||||
rebar3-version: "3"
|
|
||||||
# elixir-version: "1.15.4"
|
|
||||||
- run: gleam deps download
|
|
||||||
- run: gleam test
|
|
||||||
- run: gleam format --check src test
|
|
32
2024/.gitignore
vendored
32
2024/.gitignore
vendored
|
@ -1,4 +1,30 @@
|
||||||
*.beam
|
# The directory Mix will write compiled artifacts to.
|
||||||
*.ez
|
/_build/
|
||||||
/build
|
|
||||||
|
# If you run "mix test --cover", coverage assets end up here.
|
||||||
|
/cover/
|
||||||
|
|
||||||
|
# The directory Mix downloads your dependencies sources to.
|
||||||
|
/deps/
|
||||||
|
|
||||||
|
# Where third-party dependencies like ExDoc output generated docs.
|
||||||
|
/doc/
|
||||||
|
|
||||||
|
# Ignore .fetch files in case you like to edit your project deps locally.
|
||||||
|
/.fetch
|
||||||
|
|
||||||
|
# If the VM crashes, it generates a dump, let's ignore it too.
|
||||||
erl_crash.dump
|
erl_crash.dump
|
||||||
|
|
||||||
|
# Also ignore archive artifacts (built via "mix archive.build").
|
||||||
|
*.ez
|
||||||
|
|
||||||
|
# Ignore package tarball (built via "mix hex.build").
|
||||||
|
aoc2024-*.tar
|
||||||
|
|
||||||
|
# Temporary files, for example, from tests.
|
||||||
|
/tmp/
|
||||||
|
|
||||||
|
.envrc
|
||||||
|
|
||||||
|
/input/
|
||||||
|
|
1
2024/.iex.exs
Normal file
1
2024/.iex.exs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
import AOC.IEx
|
|
@ -1,3 +0,0 @@
|
||||||
[tools]
|
|
||||||
gleam = "1.6.1"
|
|
||||||
erlang = "27.1.2"
|
|
|
@ -2,8 +2,9 @@
|
||||||
|
|
||||||
| S | M | T | W | T | F | S |
|
| S | M | T | W | T | F | S |
|
||||||
| :-: | :-: | :-: | :-: | :-: | :-: | :-: |
|
| :-: | :-: | :-: | :-: | :-: | :-: | :-: |
|
||||||
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|
| [1] | 2 | 3 | 4 | 5 | 6 | 7 |
|
||||||
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
|
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
|
||||||
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
|
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
|
||||||
| 22 | 23 | 24 | 25 | | | |
|
| 22 | 23 | 24 | 25 | | | |
|
||||||
|
|
||||||
|
[1]: ./lib/2024/1.ex
|
||||||
|
|
8
2024/config/config.exs
Normal file
8
2024/config/config.exs
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
import Config
|
||||||
|
|
||||||
|
config :advent_of_code_utils,
|
||||||
|
session: System.fetch_env!("AOC_SESSION"),
|
||||||
|
auto_compile?: true,
|
||||||
|
time_calls?: true
|
||||||
|
|
||||||
|
config :iex, inspect: [charlists: :as_lists]
|
|
@ -1,22 +0,0 @@
|
||||||
name = "aoc"
|
|
||||||
version = "1.0.0"
|
|
||||||
|
|
||||||
# Fill out these fields if you intend to generate HTML documentation or publish
|
|
||||||
# your project to the Hex package manager.
|
|
||||||
#
|
|
||||||
# description = ""
|
|
||||||
# licences = ["Apache-2.0"]
|
|
||||||
# repository = { type = "github", user = "", repo = "" }
|
|
||||||
# links = [{ title = "Website", href = "" }]
|
|
||||||
#
|
|
||||||
# For a full reference of all the available options, you can have a look at
|
|
||||||
# https://gleam.run/writing-gleam/gleam-toml/.
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
|
|
||||||
glint = ">= 1.1.0 and < 2.0.0"
|
|
||||||
argv = ">= 1.0.2 and < 2.0.0"
|
|
||||||
birl = ">= 1.7.1 and < 2.0.0"
|
|
||||||
|
|
||||||
[dev-dependencies]
|
|
||||||
gleeunit = ">= 1.0.0 and < 2.0.0"
|
|
34
2024/lib/2024/1.ex
Normal file
34
2024/lib/2024/1.ex
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
import AOC
|
||||||
|
|
||||||
|
aoc 2024, 1 do
|
||||||
|
def p1(input) do
|
||||||
|
{left, right} = read_input(input)
|
||||||
|
|
||||||
|
left
|
||||||
|
|> Enum.sort()
|
||||||
|
|> Enum.zip_with(Enum.sort(right), &abs(&1 - &2))
|
||||||
|
|> Enum.sum()
|
||||||
|
end
|
||||||
|
|
||||||
|
def p2(input) do
|
||||||
|
{left, right} = read_input(input)
|
||||||
|
|
||||||
|
right_list_freqs = Enum.frequencies(right)
|
||||||
|
|
||||||
|
left
|
||||||
|
|> Enum.map(&(&1 * Map.get(right_list_freqs, &1, 0)))
|
||||||
|
|> Enum.sum()
|
||||||
|
end
|
||||||
|
|
||||||
|
defp read_input(input) do
|
||||||
|
input
|
||||||
|
|> String.split("\n")
|
||||||
|
|> Enum.map(fn line ->
|
||||||
|
line
|
||||||
|
|> String.split()
|
||||||
|
|> Enum.map(&String.to_integer/1)
|
||||||
|
|> List.to_tuple()
|
||||||
|
end)
|
||||||
|
|> Enum.unzip()
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,22 +0,0 @@
|
||||||
# This file was generated by Gleam
|
|
||||||
# You typically do not need to edit this file
|
|
||||||
|
|
||||||
packages = [
|
|
||||||
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" },
|
|
||||||
{ name = "birl", version = "1.7.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "5C66647D62BCB11FE327E7A6024907C4A17954EF22865FE0940B54A852446D01" },
|
|
||||||
{ name = "gleam_community_ansi", version = "1.4.1", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "4CD513FC62523053E62ED7BAC2F36136EC17D6A8942728250A9A00A15E340E4B" },
|
|
||||||
{ name = "gleam_community_colour", version = "1.4.1", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "386CB9B01B33371538672EEA8A6375A0A0ADEF41F17C86DDCB81C92AD00DA610" },
|
|
||||||
{ name = "gleam_json", version = "2.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "0A57FB5666E695FD2BEE74C0428A98B0FC11A395D2C7B4CDF5E22C5DD32C74C6" },
|
|
||||||
{ name = "gleam_stdlib", version = "0.43.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "69EF22E78FDCA9097CBE7DF91C05B2A8B5436826D9F66680D879182C0860A747" },
|
|
||||||
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
|
|
||||||
{ name = "glint", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_community_colour", "gleam_stdlib", "snag"], otp_app = "glint", source = "hex", outer_checksum = "A3F1B7C665FD216BE6A886D56537F0E095FB07DF62146074109270B798F8CEC4" },
|
|
||||||
{ name = "ranger", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "ranger", source = "hex", outer_checksum = "1566C272B1D141B3BBA38B25CB761EF56E312E79EC0E2DFD4D3C19FB0CC1F98C" },
|
|
||||||
{ name = "snag", version = "0.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "snag", source = "hex", outer_checksum = "54D32E16E33655346AA3E66CBA7E191DE0A8793D2C05284E3EFB90AD2CE92BCC" },
|
|
||||||
]
|
|
||||||
|
|
||||||
[requirements]
|
|
||||||
argv = { version = ">= 1.0.2 and < 2.0.0" }
|
|
||||||
birl = { version = ">= 1.7.1 and < 2.0.0" }
|
|
||||||
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
|
|
||||||
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
|
|
||||||
glint = { version = ">= 1.1.0 and < 2.0.0" }
|
|
28
2024/mix.exs
Normal file
28
2024/mix.exs
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
defmodule Aoc2024.MixProject do
|
||||||
|
use Mix.Project
|
||||||
|
|
||||||
|
def project do
|
||||||
|
[
|
||||||
|
app: :aoc2024,
|
||||||
|
version: "0.1.0",
|
||||||
|
elixir: "~> 1.17",
|
||||||
|
start_permanent: Mix.env() == :prod,
|
||||||
|
deps: deps()
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
# Run "mix help compile.app" to learn about applications.
|
||||||
|
def application do
|
||||||
|
[
|
||||||
|
extra_applications: [:logger]
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
# Run "mix help deps" to learn about dependencies.
|
||||||
|
defp deps do
|
||||||
|
[
|
||||||
|
{:advent_of_code_utils, "~> 4.0"},
|
||||||
|
{:styler, "~> 1.2", only: [:dev, :test], runtime: false}
|
||||||
|
]
|
||||||
|
end
|
||||||
|
end
|
6
2024/mix.lock
Normal file
6
2024/mix.lock
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
%{
|
||||||
|
"advent_of_code_utils": {:hex, :advent_of_code_utils, "4.0.1", "591073a49600cbceae6d92e0fb2d2c56b59f6d383851e2a7c00c3bf0e4f33f4f", [:mix], [{:floki, "~> 0.34", [hex: :floki, repo: "hexpm", optional: false]}, {:tz, "~> 0.26", [hex: :tz, repo: "hexpm", optional: false]}], "hexpm", "684d016883d7b5443d9d22abc34013fb6f1d5d9ff114859a40890545a570eec8"},
|
||||||
|
"floki": {:hex, :floki, "0.36.3", "1102f93b16a55bc5383b85ae3ec470f82dee056eaeff9195e8afdf0ef2a43c30", [:mix], [], "hexpm", "fe0158bff509e407735f6d40b3ee0d7deb47f3f3ee7c6c182ad28599f9f6b27a"},
|
||||||
|
"styler": {:hex, :styler, "1.2.1", "28f9e3d4b065c22575c56b8ae03d05188add1b21bec5ae664fc1551e2dfcc41b", [:mix], [], "hexpm", "71dc33980e530d21ca54db9c2075e646faa6e7b744a9d4a3dfb0ff01f56595f0"},
|
||||||
|
"tz": {:hex, :tz, "0.28.1", "717f5ffddfd1e475e2a233e221dc0b4b76c35c4b3650b060c8e3ba29dd6632e9", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:mint, "~> 1.6", [hex: :mint, repo: "hexpm", optional: true]}], "hexpm", "bfdca1aa1902643c6c43b77c1fb0cb3d744fd2f09a8a98405468afdee0848c8a"},
|
||||||
|
}
|
|
@ -1,47 +0,0 @@
|
||||||
import argv
|
|
||||||
import birl
|
|
||||||
import gleam/int
|
|
||||||
import gleam/io
|
|
||||||
import gleam/list
|
|
||||||
import gleam/string
|
|
||||||
import glint
|
|
||||||
|
|
||||||
fn day_flag() -> glint.Flag(Int) {
|
|
||||||
let flag =
|
|
||||||
glint.int_flag("day")
|
|
||||||
|> glint.flag_help("The day to run")
|
|
||||||
|
|
||||||
case birl.now() |> birl.get_day() {
|
|
||||||
birl.Day(year: _, month: 12, date: date) ->
|
|
||||||
flag
|
|
||||||
|> glint.flag_default(date)
|
|
||||||
_ -> flag
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parts_flag() -> glint.Flag(List(Int)) {
|
|
||||||
glint.ints_flag("part")
|
|
||||||
|> glint.flag_default([1, 2])
|
|
||||||
|> glint.flag_help("The part(s) to run")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn run() -> glint.Command(Nil) {
|
|
||||||
use day <- glint.flag(day_flag())
|
|
||||||
use parts <- glint.flag(parts_flag())
|
|
||||||
use _, _, flags <- glint.command()
|
|
||||||
let assert Ok(day) = day(flags)
|
|
||||||
let assert Ok(parts) = parts(flags)
|
|
||||||
io.println(int.to_string(day))
|
|
||||||
parts
|
|
||||||
|> list.map(int.to_string)
|
|
||||||
|> string.join(", ")
|
|
||||||
|> io.println()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn main() {
|
|
||||||
glint.new()
|
|
||||||
|> glint.with_name("run")
|
|
||||||
|> glint.pretty_help(glint.default_pretty_help())
|
|
||||||
|> glint.add(at: [], do: run())
|
|
||||||
|> glint.run(argv.load().arguments)
|
|
||||||
}
|
|
|
@ -1,12 +0,0 @@
|
||||||
import gleeunit
|
|
||||||
import gleeunit/should
|
|
||||||
|
|
||||||
pub fn main() {
|
|
||||||
gleeunit.main()
|
|
||||||
}
|
|
||||||
|
|
||||||
// gleeunit test functions end in `_test`
|
|
||||||
pub fn hello_world_test() {
|
|
||||||
1
|
|
||||||
|> should.equal(1)
|
|
||||||
}
|
|
|
@ -15,7 +15,7 @@
|
||||||
| [2021] | **43/50** 🌟 | Elixir |
|
| [2021] | **43/50** 🌟 | Elixir |
|
||||||
| [2022] | **14/50** 🌟 | Elixir, Haskell |
|
| [2022] | **14/50** 🌟 | Elixir, Haskell |
|
||||||
| [2023] | **19/50** 🌟 | Elixir, Haskell |
|
| [2023] | **19/50** 🌟 | Elixir, Haskell |
|
||||||
| [2024] | | |
|
| [2024] | **2/50** 🌟 | Elixir |
|
||||||
|
|
||||||
[2015]: ./2015
|
[2015]: ./2015
|
||||||
[2017]: ./2017
|
[2017]: ./2017
|
||||||
|
|
Loading…
Reference in a new issue