Skip to content

Commit 0c8db20

Browse files
committed
Initial commit
0 parents  commit 0c8db20

File tree

11 files changed

+314
-0
lines changed

11 files changed

+314
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.input.txt

2023/.formatter.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Used by "mix format"
2+
[
3+
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
4+
]

2023/.gitignore

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# The directory Mix will write compiled artifacts to.
2+
/_build/
3+
4+
# If you run "mix test --cover", coverage assets end up here.
5+
/cover/
6+
7+
# The directory Mix downloads your dependencies sources to.
8+
/deps/
9+
10+
# Where third-party dependencies like ExDoc output generated docs.
11+
/doc/
12+
13+
# Ignore .fetch files in case you like to edit your project deps locally.
14+
/.fetch
15+
16+
# If the VM crashes, it generates a dump, let's ignore it too.
17+
erl_crash.dump
18+
19+
# Also ignore archive artifacts (built via "mix archive.build").
20+
*.ez
21+
22+
# Ignore package tarball (built via "mix hex.build").
23+
aoc2023-*.tar
24+
25+
# Temporary files, for example, from tests.
26+
/tmp/
27+
28+
.input.txt
29+
30+
.elixir_ls

2023/lib/day1.ex

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
defmodule Day1a do
2+
def first([c | _]) when ?0 <= c and c <= ?9 do
3+
to_string([c])
4+
end
5+
6+
def first([c | tail]) when ?0 > c or c > ?9 do
7+
first(tail)
8+
end
9+
10+
def run do
11+
File.read!(".input.txt")
12+
|> String.split("\n", trim: true)
13+
|> Enum.map(fn line ->
14+
String.to_integer(first(to_charlist(line)) <> first(Enum.reverse(to_charlist(line))))
15+
end)
16+
|> Enum.sum()
17+
end
18+
end
19+
20+
defmodule Day1b do
21+
def map_match(res) do
22+
case res do
23+
{a, _} -> a
24+
:nomatch -> nil
25+
end
26+
end
27+
28+
def first(str, [{x, num} | tail], min, val) do
29+
currIdx = str |> :binary.match(num) |> map_match()
30+
31+
{newPrec, newVal} =
32+
if currIdx && currIdx <= min do
33+
{currIdx, x}
34+
else
35+
{min, val}
36+
end
37+
38+
first(str, tail, newPrec, newVal)
39+
end
40+
41+
def first(_, [], _, val) do
42+
to_string(val)
43+
end
44+
45+
def run do
46+
numbers = [
47+
{0, "0"},
48+
{1, "1"},
49+
{2, "2"},
50+
{3, "3"},
51+
{4, "4"},
52+
{5, "5"},
53+
{6, "6"},
54+
{7, "7"},
55+
{8, "8"},
56+
{9, "9"},
57+
{0, "zero"},
58+
{1, "one"},
59+
{2, "two"},
60+
{3, "three"},
61+
{4, "four"},
62+
{5, "five"},
63+
{6, "six"},
64+
{7, "seven"},
65+
{8, "eight"},
66+
{9, "nine"}
67+
]
68+
69+
numbers_rev = numbers |> Enum.map(fn {x, num} -> {x, String.reverse(num)} end)
70+
71+
File.read!(".input.txt")
72+
|> String.split("\n", trim: true)
73+
|> Enum.map(fn line ->
74+
String.to_integer(
75+
first(line, numbers, 999_999_999_999, -1) <>
76+
first(String.reverse(line), numbers_rev, 999_999_999_999, -1)
77+
)
78+
end)
79+
|> Enum.sum()
80+
end
81+
end

2023/lib/day2.ex

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
defmodule Day2a do
2+
def parse_line(line) do
3+
[game_dirty, values_dirty] = String.split(line, ": ")
4+
5+
[_, game_id_str] = String.split(game_dirty)
6+
game_id = String.to_integer(game_id_str)
7+
8+
values =
9+
String.split(values_dirty, "; ")
10+
|> Enum.map(fn subset_dirty ->
11+
String.split(subset_dirty, ", ")
12+
|> Enum.map(fn value_dirty ->
13+
[count, color] = String.split(value_dirty, " ")
14+
{color, String.to_integer(count)}
15+
end)
16+
end)
17+
18+
{game_id, values}
19+
end
20+
21+
def is_color_possible?({color, count}) do
22+
case color do
23+
"red" -> count <= 12
24+
"green" -> count <= 13
25+
"blue" -> count <= 14
26+
_ -> false
27+
end
28+
end
29+
30+
def is_possible?(game) do
31+
game
32+
|> Enum.all?(fn subset ->
33+
subset
34+
|> Enum.all?(&is_color_possible?/1)
35+
end)
36+
end
37+
38+
def run do
39+
File.read!(".input.txt")
40+
|> String.split("\n", trim: true)
41+
|> Enum.map(&parse_line/1)
42+
|> Enum.filter(fn {_, values} -> is_possible?(values) end)
43+
|> Enum.map(&elem(&1, 0))
44+
|> Enum.sum()
45+
end
46+
end
47+
48+
defmodule Day2b do
49+
def parse_line(line) do
50+
[game_dirty, values_dirty] = String.split(line, ": ")
51+
52+
[_, game_id_str] = String.split(game_dirty)
53+
game_id = String.to_integer(game_id_str)
54+
55+
values =
56+
String.split(values_dirty, "; ")
57+
|> Enum.map(fn subset_dirty ->
58+
String.split(subset_dirty, ", ")
59+
|> Enum.map(fn value_dirty ->
60+
[count, color] = String.split(value_dirty, " ")
61+
{String.to_atom(color), String.to_integer(count)}
62+
end)
63+
|> Enum.into(%{})
64+
end)
65+
66+
{game_id, values}
67+
end
68+
69+
def subset_max([values | tail], max) do
70+
maxp =
71+
max
72+
|> Enum.map(fn {color, count} ->
73+
{color,
74+
case Map.get(values, color) do
75+
nil -> count
76+
value -> max(count, value)
77+
end}
78+
end)
79+
|> Enum.into(%{})
80+
81+
subset_max(tail, maxp)
82+
end
83+
84+
def subset_max([], max) do
85+
max
86+
end
87+
88+
def power(set) do
89+
set
90+
|> Enum.map(&elem(&1, 1))
91+
|> Enum.product()
92+
end
93+
94+
def run do
95+
File.read!(".input.txt")
96+
|> String.split("\n", trim: true)
97+
|> Enum.map(&parse_line/1)
98+
|> Enum.map(fn {_, values} -> subset_max(values, %{blue: 0, green: 0, red: 0}) end)
99+
|> Enum.map(&power/1)
100+
|> Enum.sum()
101+
end
102+
end

2023/mix.exs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
defmodule Aoc2023.MixProject do
2+
use Mix.Project
3+
4+
def project do
5+
[
6+
app: :aoc2023,
7+
version: "0.1.0",
8+
elixir: "~> 1.17",
9+
start_permanent: Mix.env() == :prod,
10+
deps: deps()
11+
]
12+
end
13+
14+
# Run "mix help compile.app" to learn about applications.
15+
def application do
16+
[
17+
extra_applications: [:logger]
18+
]
19+
end
20+
21+
# Run "mix help deps" to learn about dependencies.
22+
defp deps do
23+
[
24+
# {:dep_from_hexpm, "~> 0.3.0"},
25+
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
26+
]
27+
end
28+
end

2024/.formatter.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Used by "mix format"
2+
[
3+
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
4+
]

2024/.gitignore

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# The directory Mix will write compiled artifacts to.
2+
/_build/
3+
4+
# If you run "mix test --cover", coverage assets end up here.
5+
/cover/
6+
7+
# The directory Mix downloads your dependencies sources to.
8+
/deps/
9+
10+
# Where third-party dependencies like ExDoc output generated docs.
11+
/doc/
12+
13+
# Ignore .fetch files in case you like to edit your project deps locally.
14+
/.fetch
15+
16+
# If the VM crashes, it generates a dump, let's ignore it too.
17+
erl_crash.dump
18+
19+
# Also ignore archive artifacts (built via "mix archive.build").
20+
*.ez
21+
22+
# Ignore package tarball (built via "mix hex.build").
23+
aoc2024-*.tar
24+
25+
# Temporary files, for example, from tests.
26+
/tmp/
27+
28+
.elixir_ls

2024/lib/day1.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
defmodule Day1a do
2+
def run do
3+
File.read!(".input.txt")
4+
5+
raise "Not implemented"
6+
end
7+
end

2024/mix.exs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
defmodule Aoc2024.MixProject do
2+
use Mix.Project
3+
4+
def project do
5+
[
6+
app: :aoc2024,
7+
version: "0.1.0",
8+
elixir: "~> 1.17",
9+
start_permanent: Mix.env() == :prod,
10+
deps: deps()
11+
]
12+
end
13+
14+
# Run "mix help compile.app" to learn about applications.
15+
def application do
16+
[
17+
extra_applications: [:logger]
18+
]
19+
end
20+
21+
# Run "mix help deps" to learn about dependencies.
22+
defp deps do
23+
[
24+
# {:dep_from_hexpm, "~> 0.3.0"},
25+
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
26+
]
27+
end
28+
end

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Advent of Code Solutions

0 commit comments

Comments
 (0)