Last active 1733202650

2024-Day1-Solution.lua Raw
1local list1, list2 = {}, {}
2
3for line in io.lines("input.txt") do
4 list1[#list1 + 1] = line:match("^(%d+)%s")
5 list2[#list2 + 1] = line:match("%s(%d+)$")
6end
7
8table.sort(list1)
9table.sort(list2)
10
11local counts = {}
12for q = 1, #list1 do
13 if counts[list1[q]] == nil then counts[list1[q]] = 0 end
14 if counts[list2[q]] == nil then counts[list2[q]] = 0 end
15 counts[list2[q]] = counts[list2[q]] + 1
16end
17
18local distance, similarity = 0, 0
19for q = 1, #list1 do
20 distance = distance + math.abs(list1[q] - list2[q])
21 similarity = similarity + (list1[q] * counts[list1[q]])
22end
23
24print(distance)
25print(similarity)
26