Skip to content

Commit aa73378

Browse files
committed
redo day 1 (...then make it beautiful)
1 parent cc14675 commit aa73378

5 files changed

Lines changed: 57 additions & 73 deletions

File tree

elixir/2024/lib/advent_of_code/day_01.ex

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,17 @@ defmodule AdventOfCode.Day01 do
22
import AdventOfCode.Parsing
33

44
def part1(input) do
5-
[list1, list2] =
6-
input
7-
|> parse_columns_ints()
8-
|> Enum.map(fn list -> Enum.sort(list) end)
9-
10-
Enum.zip_with(list1, list2, fn first, second -> abs(first - second) end)
5+
input
6+
|> parse_columns_ints
7+
|> Enum.map(&Enum.sort/1)
8+
|> Enum.zip_with(fn [num1, num2] -> abs(num1 - num2) end)
119
|> Enum.sum()
1210
end
1311

1412
def part2(input) do
15-
[list1, list2] =
16-
input
17-
|> parse_columns_ints()
18-
19-
list2_freqs = Enum.frequencies(list2)
13+
[list1, list2] = parse_columns_ints(input)
14+
freqs = Enum.frequencies(list2)
2015

21-
Enum.reduce(list1, 0, fn num, acc ->
22-
acc + num * Map.get(list2_freqs, num, 0)
23-
end)
16+
Enum.reduce(list1, 0, fn val, acc -> val * Map.get(freqs, val, 0) + acc end)
2417
end
2518
end
Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
11
defmodule AdventOfCode.Parsing do
22
def parse_columns_ints(input) do
3+
input
4+
|> parse_rows_ints
5+
# transpose matrix
6+
|> Enum.zip_with(&Function.identity/1)
7+
end
8+
9+
def parse_rows_ints(input) do
310
input
411
|> String.trim()
512
|> String.split("\n")
6-
|> Enum.reduce(
7-
[],
8-
fn
9-
line, [] ->
10-
# first time through, set up acc
11-
String.split(line)
12-
|> Enum.map(fn val -> [String.to_integer(val)] end)
13-
14-
line, accs ->
15-
values = String.split(line)
16-
17-
Enum.zip_with(values, accs, fn val, acc ->
18-
[String.to_integer(val) | acc]
19-
end)
20-
end
21-
)
22-
|> Enum.map(fn list -> Enum.reverse(list) end)
13+
|> Enum.map(fn row -> row |> String.split() |> Enum.map(&String.to_integer/1) end)
2314
end
2415
end

elixir/2024/test/advent_of_code/parsing_test.exs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule AdventOfCode.Day01Test do
33

44
import AdventOfCode.Parsing
55

6-
test "two columns" do
6+
test "two columns of ints" do
77
input = """
88
1 2
99
3 4
@@ -15,7 +15,7 @@ defmodule AdventOfCode.Day01Test do
1515
assert expected == parse_columns_ints(input)
1616
end
1717

18-
test "three columns" do
18+
test "three columns of ints" do
1919
input = """
2020
1 2 6
2121
3 4 7
@@ -26,4 +26,26 @@ defmodule AdventOfCode.Day01Test do
2626

2727
assert expected == parse_columns_ints(input)
2828
end
29+
30+
test "rows of ints" do
31+
input = """
32+
7 6 4 2 1
33+
1 2 7 8 9
34+
9 7 6 2 1
35+
1 3 2 4 5
36+
8 6 4 4 1
37+
1 3 6 7 9
38+
"""
39+
40+
expected = [
41+
[7, 6, 4, 2, 1],
42+
[1, 2, 7, 8, 9],
43+
[9, 7, 6, 2, 1],
44+
[1, 3, 2, 4, 5],
45+
[8, 6, 4, 4, 1],
46+
[1, 3, 6, 7, 9]
47+
]
48+
49+
assert expected == parse_rows_ints(input)
50+
end
2951
end

python/solutions/2024/day_01/solution.py

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,25 @@
44

55
from ...base import StrSplitSolution, answer
66
from ...utils.parsing import parse_column_ints
7-
7+
from collections import Counter
88

99
class Solution(StrSplitSolution):
1010
_year = 2024
1111
_day = 1
1212

1313
@answer(2264607)
1414
def part_1(self) -> int:
15-
[list1, list2] = parse_column_ints(self.input)
16-
total: int = 0
17-
18-
list1.sort()
19-
list2.sort()
20-
21-
for (first, second) in zip(list1, list2):
22-
total = total + abs(first - second)
23-
15+
locations = map(sorted, parse_column_ints(self.input))
16+
total = 0
17+
for val1, val2 in zip(*locations):
18+
total += abs(val1 - val2)
2419
return total
2520

26-
# @answer(1234)
21+
@answer(19457120)
2722
def part_2(self) -> int:
2823
[list1, list2] = parse_column_ints(self.input)
29-
total: int = 0
30-
31-
list2_freqs = {}
32-
for num in list2:
33-
if num in list2_freqs:
34-
list2_freqs[num] += 1
35-
else:
36-
list2_freqs[num] = 1
37-
38-
for num in list1:
39-
total += num * list2_freqs.get(num, 0)
24+
freqs = Counter(list2)
25+
total = 0
26+
for val in list1:
27+
total += val * freqs.get(val, 0)
4028
return total
41-
42-
# @answer((1234, 4567))
43-
# def solve(self) -> tuple[int, int]:
44-
# pass

python/solutions/utils/parsing.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
# add whatever utilities you'll find useful across multiple solutions
2-
# import them in a solution using:
3-
# from ...utils.example import add
4-
5-
61
def parse_column_ints(input: list[str]):
7-
columns: list[int] = []
8-
9-
for line_num, line in enumerate(input):
10-
fields = line.split()
11-
for index, field in enumerate(fields):
12-
if line_num == 0:
13-
columns.append([int(field)])
2+
result = []
3+
for row_num, row in enumerate(input):
4+
values = map(int, row.split())
5+
for col_num, val in enumerate(values):
6+
if row_num == 0:
7+
result.append([val])
148
else:
15-
columns[index].append(int(field))
16-
return columns
9+
result[col_num].append(val)
10+
return result

0 commit comments

Comments
 (0)