Skip to content

Commit 3ca4bd2

Browse files
committed
day 7 part 2
1 parent 827b46a commit 3ca4bd2

3 files changed

Lines changed: 56 additions & 21 deletions

File tree

elixir/2024/lib/advent_of_code/day_07.ex

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,31 @@ defmodule AdventOfCode.Day07 do
99
|> Enum.reduce(0, fn [test, _], acc -> acc + test end)
1010
end
1111

12-
def part2(_args) do
12+
def part2(input) do
13+
data = parse(input)
14+
15+
Enum.filter(data, fn [test, nums] ->
16+
possible = possible_results(Enum.reverse(nums), [&Kernel.+/2, &Kernel.*/2, &concatenate/2])
17+
test in possible
18+
end)
19+
|> Enum.reduce(0, fn [test, _], acc -> acc + test end)
20+
end
21+
22+
defp concatenate(x, y) do
23+
Enum.join(Integer.digits(y) ++ Integer.digits(x)) |> String.to_integer()
1324
end
1425

15-
def possible_results([x], _), do: [x]
26+
defp possible_results([x], _), do: [x]
1627

17-
def possible_results([head | tail], operators) do
28+
defp possible_results([head | tail], operators) do
1829
subresults = possible_results(tail, operators)
1930

2031
Enum.flat_map(subresults, fn sub ->
21-
Enum.map(operators, fn op ->
22-
op.(head, sub)
23-
end)
32+
Enum.map(operators, fn op -> op.(head, sub) end)
2433
end)
2534
end
2635

27-
def parse(input) do
36+
defp parse(input) do
2837
input
2938
|> String.split("\n", trim: true)
3039
|> Enum.map(fn line ->

elixir/2024/test/advent_of_code/day_07_test.exs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,25 @@ defmodule AdventOfCode.Day07Test do
1515
21037: 9 7 18 13
1616
292: 11 6 16 20
1717
"""
18+
1819
3749 = part1(input)
1920
end
2021

21-
@tag :skip
2222
test "part2" do
23-
input = nil
24-
result = part2(input)
25-
26-
assert result
23+
input = """
24+
190: 10 19
25+
3267: 81 40 27
26+
83: 17 5
27+
156: 15 6
28+
7290: 6 8 6 15
29+
161011: 16 10 13
30+
192: 17 8 14
31+
21037: 9 7 18 13
32+
292: 11 6 16 20
33+
"""
34+
# input = "156: 15 6"
35+
# result = part2(input)
36+
# assert result == 156
37+
11387 = part2(input)
2738
end
2839
end

python/solutions/2024/day_07/solution.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,36 @@ class Solution(StrSplitSolution):
1212
_day = 7
1313

1414
# @answer((1234, 4567))
15-
def solve(self) -> tuple[int, int]:
15+
def part_1(self) -> int:
16+
lines = self._parse()
17+
18+
return sum(
19+
line[0]
20+
for line in lines
21+
if line[0] in self._possible_results(line[1], [operator.add, operator.mul])
22+
)
23+
24+
def part_2(self) -> int:
25+
lines = self._parse()
26+
27+
return sum(
28+
line[0]
29+
for line in lines
30+
if line[0] in self._possible_results(line[1], [operator.add, operator.mul, self._concatenate])
31+
)
32+
33+
def _parse(self):
1634
lines = []
1735
for line in self.input:
1836
[test, num_str] = line.split(":")
1937
nums = []
20-
for index, num in enumerate(num_str.split()):
38+
for num in num_str.split():
2139
nums.append(int(num))
2240
lines.append((int(test), nums))
41+
return lines
2342

24-
total = sum(
25-
line[0]
26-
for line in lines
27-
if line[0] in self._possible_results(line[1], [operator.add, operator.mul])
28-
)
29-
return (total, 0)
43+
def _concatenate(self, x: int, y: int) -> int:
44+
return int(str(x) + str(y))
3045

3146
def _possible_results(self, nums: list[int], ops: list[Callable]) -> list[int]:
3247
current = nums.pop()
@@ -35,7 +50,7 @@ def _possible_results(self, nums: list[int], ops: list[Callable]) -> list[int]:
3550
new_results = []
3651
for sub in subresults:
3752
for op in ops:
38-
new_results.append(op(current, sub))
53+
new_results.append(op(sub, current))
3954
return new_results
4055
else:
4156
return [current]

0 commit comments

Comments
 (0)