1
0
Fork 0

Compare commits

...

6 Commits

Author SHA1 Message Date
Lucidiot 47b3d9fc19
2020 day 6 2020-12-07 02:22:01 +01:00
Lucidiot 47a5c60f13
2020 day 5 2020-12-07 02:10:24 +01:00
Lucidiot 2a3fe2951b
2020 day 4 2020-12-07 01:39:49 +01:00
Lucidiot 2543ec7db3
2020 day 3 2020-12-07 01:39:26 +01:00
Lucidiot cbdecb235e
2020 day 2 2020-12-07 00:30:09 +01:00
Lucidiot 8584d36762
2020 day 1 2020-12-06 23:50:56 +01:00
7 changed files with 234 additions and 7 deletions

32
2020/1/day1.lua Executable file
View File

@ -0,0 +1,32 @@
#!/usr/bin/env lua
-- I'm feeling lazy.
local numbers = {}
for line in io.lines() do
table.insert(numbers, tonumber(line))
end
local function part1()
for _, x in ipairs(numbers) do
for _, y in ipairs(numbers) do
if x + y == 2020 then
return x * y
end
end
end
end
local function part2()
for _, x in ipairs(numbers) do
for _, y in ipairs(numbers) do
for _, z in ipairs(numbers) do
if x + y + z == 2020 then
return x * y * z
end
end
end
end
end
print(part1())
print(part2())

35
2020/2/day2.lua Executable file
View File

@ -0,0 +1,35 @@
#!/usr/bin/env lua
local data = {}
for line in io.lines() do
local password_start, n1, n2, char = select(2, line:find('^(%d+)-(%d+) (%l): '))
table.insert(data, {tonumber(n1), tonumber(n2), char, line:sub(password_start + 1)})
end
local function part1()
local count = 0
for _, line in ipairs(data) do
local min, max, char, password = table.unpack(line)
local char_count = select(2, password:gsub(char, ''))
if min <= char_count and char_count <= max then
count = count + 1
end
end
return count
end
local function part2()
local count = 0
for _, line in ipairs(data) do
local pos1, pos2, char, password = table.unpack(line)
local has1, has2 = password:sub(pos1, pos1) == char, password:sub(pos2, pos2) == char
if (has1 or has2) and not (has1 and has2) then
count = count + 1
end
end
return count
end
print(part1())
print(part2())

24
2020/3/day3.lua Normal file
View File

@ -0,0 +1,24 @@
local lines = {}
for line in io.lines() do
table.insert(lines, line)
end
local function count_trees(right, down)
right = right or 1
down = down or 1
local count, iteration = 0, 1
for index = 1,#lines,down do
local pos = ((iteration - 1) * right) % lines[index]:len() + 1
if lines[index]:sub(pos, pos) == "#" then
count = count + 1
end
iteration = iteration + 1
end
return count
end
local part1 = count_trees(3)
local part2 = count_trees() * part1 * count_trees(5) * count_trees(7) * count_trees(1, 2)
print(part1)
print(part2)

79
2020/4/day4.lua Normal file
View File

@ -0,0 +1,79 @@
#!/usr/bin/env lua
local passports, current = {}, {}
for line in io.lines() do
if line == "" then
table.insert(passports, current)
current = {}
end
for name, value in line:gmatch("(%a+):(%g+)") do
current[name] = value
end
end
table.insert(passports, current)
local function validate_byr(val)
val = tonumber(val)
return val and val >= 1920 and val <= 2002
end
local function validate_iyr(val)
val = tonumber(val)
return val and val >= 2010 and val <= 2020
end
local function validate_eyr(val)
val = tonumber(val)
return val and val >= 2020 and val <= 2030
end
local function validate_hgt(val)
local suffix = val:sub(-2)
val = tonumber(val:sub(1, -3))
if not val then return false end
if suffix == "cm" then
return val >= 150 and val <= 193
elseif suffix == "in" then
return val >= 59 and val <= 76
else return false end
end
local function validate_hcl(val)
return val and val:match("#%x%x%x%x%x")
end
local function validate_ecl(val)
return ({amb=true, blu=true, brn=true, gry=true, grn=true, hzl=true, oth=true})[val]
end
local function validate_pid(val)
return val and val:match("^%d%d%d%d%d%d%d%d%d$")
end
local required_fields = {
byr=validate_byr,
iyr=validate_iyr,
eyr=validate_eyr,
hgt=validate_hgt,
hcl=validate_hcl,
ecl=validate_ecl,
pid=validate_pid,
}
local function validate(run_checks)
local count = 0
for _, passport in ipairs(passports) do
local ok = 1
for field, validator in pairs(required_fields) do
if passport[field] == nil or (run_checks and not validator(passport[field])) then
ok = 0
break
end
end
count = count + ok
end
return count
end
print(validate(false))
print(validate(true))

28
2020/5/day5.lua Normal file
View File

@ -0,0 +1,28 @@
#!/usr/bin/env lua
local seat_ids = {}
local function parse(line)
local word = tonumber(line:gsub("[FBLR]", {F="0", B="1", L="0", R="1"}), 2)
local row, column = word >> 3, word & 7
return row * 8 + column
end
for line in io.lines() do
table.insert(seat_ids, parse(line))
end
local lowest_id, highest_id = math.min(table.unpack(seat_ids)), math.max(table.unpack(seat_ids))
print(highest_id)
-- Build an equivalent of a set for faster access
local seat_ids_set = {}
for _, id in ipairs(seat_ids) do
seat_ids_set[id] = true
end
for id = lowest_id, highest_id do
if not seat_ids_set[id] and seat_ids_set[id - 1] and seat_ids_set[id + 1] then
print(id)
return
end
end

29
2020/6/day6.lua Normal file
View File

@ -0,0 +1,29 @@
#!/usr/bin/env lua
local groups, current = {}, {count=0}
for line in io.lines() do
if line == "" then
table.insert(groups, current)
current = {count=0}
else
for char in line:gmatch(".") do
current[char] = (current[char] or 0) + 1
end
current.count = current.count + 1
end
end
table.insert(groups, current)
local any_count, all_count = 0, 0
for _, group in ipairs(groups) do
for name, question_count in pairs(group) do
if name ~= "count" then
any_count = any_count + 1
if question_count == group.count then
all_count = all_count + 1
end
end
end
end
print(any_count)
print(all_count)

View File

@ -5,13 +5,13 @@ My solutions to the Advent of Code puzzles.
## Progress
```
15 16 17 18 19
1 ██ ██ ██ ██
2 ██ ██ ██ ██
3 ██ ██ ██ ██
4 ██ ██ ██
5 ██ ██ ██
6 ██ ██
15 16 17 18 19 20
1 ██ ██ ██ ██ ██
2 ██ ██ ██ ██ ██
3 ██ ██ ██ ██ ██
4 ██ ██ ██ ██
5 ██ ██ ██ ██
6 ██ ██ ██
7 ██ ██
8 ██ ██
9 █ ██