Skip to content

Commit e596364

Browse files
committed
feat: year 2024, day 03
Signed-off-by: Sgiath <[email protected]>
1 parent 68d7ffd commit e596364

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

lib/2024/day03.ex

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
defmodule AdventOfCode.Year2024.Day03 do
2+
@moduledoc ~S"""
3+
https://adventofcode.com/2024/day/3
4+
"""
5+
use AdventOfCode, year: 2024, day: 03
6+
7+
# =============================================================================================
8+
# Input
9+
# =============================================================================================
10+
11+
@impl AdventOfCode
12+
def test_input do
13+
"""
14+
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
15+
"""
16+
end
17+
18+
def parse(input) do
19+
~r/(mul\(\d+\,\d+\))|(do\(\))|(don't\(\))/
20+
|> Regex.scan(input)
21+
|> Enum.map(fn [match | _rest] ->
22+
String.split(match, ["(", ")", ","], trim: true)
23+
end)
24+
end
25+
26+
# =============================================================================================
27+
# Part 1
28+
# =============================================================================================
29+
30+
@impl AdventOfCode
31+
def part1(input) do
32+
input
33+
|> parse()
34+
|> Enum.map(fn
35+
["mul", a, b] -> String.to_integer(a) * String.to_integer(b)
36+
_otherwise -> 0
37+
end)
38+
|> Enum.sum()
39+
end
40+
41+
# =============================================================================================
42+
# Part 2
43+
# =============================================================================================
44+
45+
@impl AdventOfCode
46+
def part2(input) do
47+
input
48+
|> parse()
49+
|> run()
50+
end
51+
52+
def run(mem, result \\ 0, enabled \\ true)
53+
54+
# do instruction enables flag
55+
def run([["do"] | mem], result, _enabled), do: run(mem, result, true)
56+
57+
# don't instruction disables flag
58+
def run([["don't"] | mem], result, _enabled), do: run(mem, result, false)
59+
60+
# multiplication happens with enabled flag
61+
def run([["mul", a, b] | mem], result, true) do
62+
run(mem, result + String.to_integer(a) * String.to_integer(b), true)
63+
end
64+
65+
# ignore multiplication with disabled flag
66+
def run([_disabled | mem], result, false), do: run(mem, result, false)
67+
68+
# end of the program
69+
def run([], result, _enabled), do: result
70+
end

livebook/2024/day03.livemd

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!-- vim: syntax=markdown -->
2+
3+
# Year 2024, Day 03
4+
5+
<https://adventofcode.com/2024/day/3>
6+
7+
```elixir
8+
alias AdventOfCode.Year2024.Day03
9+
```
10+
11+
## Input
12+
13+
```elixir
14+
input = Day03.test_input()
15+
```
16+
17+
## Part 1
18+
19+
<!-- livebook:{"reevaluate_automatically":true} -->
20+
21+
```elixir
22+
Day03.part1(input)
23+
```
24+
25+
## Part 2
26+
27+
<!-- livebook:{"reevaluate_automatically":true} -->
28+
29+
```elixir
30+
Day03.part2(input)
31+
```

0 commit comments

Comments
 (0)