1
0
Fork 0

2020 day 2

This commit is contained in:
Lucidiot 2020-12-07 00:30:09 +01:00
parent 8584d36762
commit cbdecb235e
Signed by: lucidiot
GPG Key ID: 3358C1CA6906FB8D
2 changed files with 36 additions and 1 deletions

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())

View File

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