did day 1 in lua for fun
This commit is contained in:
parent
f7f8f632bd
commit
8b27923932
1998
2024/day1/input.text
1998
2024/day1/input.text
File diff suppressed because it is too large
Load Diff
57
2024/day1/main.lua
Normal file
57
2024/day1/main.lua
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
-- Parse Input
|
||||||
|
|
||||||
|
local file = io.open("input.text", "r")
|
||||||
|
|
||||||
|
io.input(file)
|
||||||
|
|
||||||
|
local left = {}
|
||||||
|
local right = {}
|
||||||
|
|
||||||
|
local i = 1
|
||||||
|
while true do
|
||||||
|
local line = io.read()
|
||||||
|
|
||||||
|
if line == nil then break end
|
||||||
|
|
||||||
|
local l = true
|
||||||
|
for str in string.gmatch(line, "%S+") do
|
||||||
|
if l then
|
||||||
|
left[i] = tonumber(str)
|
||||||
|
l = not l
|
||||||
|
else
|
||||||
|
right[i] = tonumber(str)
|
||||||
|
i = i + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Part 1
|
||||||
|
|
||||||
|
table.sort(left)
|
||||||
|
table.sort(right)
|
||||||
|
|
||||||
|
local total = 0
|
||||||
|
for i, n in ipairs(left) do
|
||||||
|
total = total + math.abs(n - right[i])
|
||||||
|
end
|
||||||
|
|
||||||
|
print("Part 1: "..total)
|
||||||
|
|
||||||
|
-- Part 2
|
||||||
|
function count(a, m)
|
||||||
|
local c = 0
|
||||||
|
for _, n in ipairs(a) do
|
||||||
|
if n == m then
|
||||||
|
c = c + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return c
|
||||||
|
end
|
||||||
|
|
||||||
|
total = 0
|
||||||
|
for i, n in ipairs(left) do
|
||||||
|
total = total + (n * count(right, n))
|
||||||
|
end
|
||||||
|
|
||||||
|
print("Part 2: "..total)
|
||||||
@ -1,32 +1,32 @@
|
|||||||
with open('input.text', 'r') as file:
|
with open('input.text', 'r') as file:
|
||||||
data: list[str] = file.readlines()
|
data: list[str] = file.readlines()
|
||||||
|
|
||||||
# Parse Input
|
# Parse Input
|
||||||
|
|
||||||
left = []
|
left = []
|
||||||
right = []
|
right = []
|
||||||
|
|
||||||
for line in data:
|
for line in data:
|
||||||
nums = line.split()
|
nums = line.split()
|
||||||
|
|
||||||
left.append(int(nums[0]))
|
left.append(int(nums[0]))
|
||||||
right.append(int(nums[1]))
|
right.append(int(nums[1]))
|
||||||
|
|
||||||
# Part 1
|
# Part 1
|
||||||
|
|
||||||
left.sort()
|
left.sort()
|
||||||
right.sort()
|
right.sort()
|
||||||
|
|
||||||
total = 0
|
total = 0
|
||||||
for i, n in enumerate(left):
|
for i, n in enumerate(left):
|
||||||
total += abs(n - right[i])
|
total += abs(n - right[i])
|
||||||
|
|
||||||
print(f'Part 1: {total}')
|
print(f'Part 1: {total}')
|
||||||
|
|
||||||
# Part 2
|
# Part 2
|
||||||
|
|
||||||
total = 0
|
total = 0
|
||||||
for i, n in enumerate(left):
|
for i, n in enumerate(left):
|
||||||
total += n * right.count(n)
|
total += n * right.count(n)
|
||||||
|
|
||||||
print(f'Part 2: {total}')
|
print(f'Part 2: {total}')
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
3 4
|
3 4
|
||||||
4 3
|
4 3
|
||||||
2 5
|
2 5
|
||||||
1 3
|
1 3
|
||||||
3 9
|
3 9
|
||||||
3 3
|
3 3
|
||||||
Loading…
x
Reference in New Issue
Block a user