aboutsummaryrefslogtreecommitdiff
path: root/01.lua
blob: fefffa4a160dc0c13ed98dcb355084bd7bbd51de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
function lines ()
    local x = io.read("*n")
    local y = io.read("*n")
    return x, y
end

local input = {{}, {}}

for x, y in lines do
    table.insert(input[1], x)
    table.insert(input[2], y)
end

function input:part1()
    table.sort(self[1])
    table.sort(self[2])

    local sum = 0
    for i = 1,#self[1] do
        sum = sum + math.abs(self[1][i] - self[2][i])
    end

    print(sum)
end

function input:part2()
    local dict = {}
    for _, val in ipairs(self[2]) do
        dict[val] = (dict[val] or 0) + 1
    end

    local sum = 0
    for _, val in ipairs(self[1]) do
        if dict[val] then sum = sum + val * dict[val] end
    end
    print(sum)
end

return input