Day 1
This commit is contained in:
parent
439bcc64c6
commit
502d51d218
2 changed files with 34 additions and 0 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -1,3 +1,5 @@
|
||||||
|
input/
|
||||||
|
|
||||||
# ---> Lua
|
# ---> Lua
|
||||||
# Compiled Lua sources
|
# Compiled Lua sources
|
||||||
luac.out
|
luac.out
|
||||||
|
|
|
||||||
32
1.lua
Executable file
32
1.lua
Executable 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)
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue