diff options
| author | Juan Marín Noguera <juan@mnpi.eu> | 2024-12-01 14:29:18 +0100 |
|---|---|---|
| committer | Juan Marín Noguera <juan@mnpi.eu> | 2024-12-01 14:29:18 +0100 |
| commit | 47eb33e373f0b6fe9891b786e8ba3951fc41476a (patch) | |
| tree | 5364e8342d176c0d33a3f0e4e7c66189cb5ff3a5 /01.lua | |
| parent | f4327e7fb94a6a7621941c3d447f7270d59db357 (diff) | |
Day 01
This also includes a template to call the relevant functions from each
day more conveniently.
Diffstat (limited to '01.lua')
| -rw-r--r-- | 01.lua | 39 |
1 files changed, 39 insertions, 0 deletions
@@ -0,0 +1,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 |
