This commit is contained in:
nazrin 2024-12-01 13:46:29 +00:00
parent 439bcc64c6
commit 502d51d218
2 changed files with 34 additions and 0 deletions

2
.gitignore vendored
View file

@ -1,3 +1,5 @@
input/
# ---> Lua
# Compiled Lua sources
luac.out

32
1.lua Executable file
View file

@ -0,0 +1,32 @@
#!/bin/env lua
local left, right = {}, {}
local rightCount = {}
for line in assert(io.open("input/1")):lines() do
local l, r = line:match("(%d+)%s+(%d+)")
l, r = assert(tonumber(l)), assert(tonumber(r))
table.insert(left, l)
table.insert(right, r)
rightCount[r] = (rightCount[r] or 0) + 1
end
table.sort(left)
table.sort(right)
local sumDiff = 0
for i=1,#left do
local diff = math.abs(left[i] - right[i])
sumDiff = sumDiff + diff
end
print("Part one", sumDiff)
local simScore = 0
for _,l in ipairs(left) do
simScore = simScore + l * (rightCount[l] or 0)
end
print("Part two", simScore)