1
0
Fork 0

Compare commits

...

6 Commits

Author SHA1 Message Date
Lucidiot 4e5c92b4f5
2021 day 2 2021-12-02 08:35:26 +01:00
Lucidiot 786ac542c2
2020 day 19 part 2 2021-12-01 23:17:39 +01:00
Lucidiot 217fe176c9
2020 day 19 part 1 2021-12-01 23:03:14 +01:00
Lucidiot 9aa9151b27
2020 day 18 2021-12-01 21:58:40 +01:00
Lucidiot 50164f79bb
2020 day 13 part 2 2021-12-01 21:01:15 +01:00
Lucidiot 178f389df9
2021 day 1 2021-12-01 19:34:16 +01:00
6 changed files with 193 additions and 6 deletions

View File

@ -27,3 +27,47 @@ repeat
bus_id = find_bus(timestamp)
until bus_id
print(bus_id * (timestamp - start_timestamp))
-- Chinese remainder theorem. I will never ever understand congruences.
local n = 1
for _, v in ipairs(sorted_buses) do
n = n * v
end
local function inverse(antibus, bus)
for i = 1, bus - 1 do
if (antibus % bus * i) % bus == 1 then
return i
end
end
return 1
end
timestamp = 0
for i, bus in ipairs(buses) do
if bus > 0 then
local antibus = n / bus
timestamp = timestamp + (bus - i + 1) * antibus * inverse(antibus, bus)
end
end
timestamp = timestamp % n
--[[
There is a bug in the above code and I can't fix it but it seems that
this gets me really close to the real answer, so I'll just go back to the
bruteforce way from here.
]]
local function check_part2(timestamp)
for i, bus in ipairs(buses) do
if bus > 0 and (timestamp - 1 + i) % bus ~= 0 then
return
end
end
return timestamp
end
while not check_part2(timestamp) do
timestamp = timestamp + 1
end
print(string.format("%d", timestamp))

49
2020/18/day18.lua Normal file
View File

@ -0,0 +1,49 @@
#!/usr/bin/env lua
-- Parse parentheses, evaluating sub-expressions using another function
local function eval(expr, eval_func)
-- Remove any extra parenthses around the entire expression
while expr:find("^%b()$") do
expr = expr:sub(2, #expr - 1)
end
-- Parse all of the outermost parentheses recursively
local matches = 0
repeat
expr, matches = expr:gsub("%b()", function (subexpr) return eval(subexpr, eval_func) end)
until matches < 1
return eval_func(expr)
end
local function part1_eval(expr)
local left, operator
for token in expr:gmatch "%S+" do
if left == nil then
left = tonumber(token)
elseif operator == nil then
operator = token
elseif operator == "+" then
left = left + tonumber(token)
operator = nil
elseif operator == "*" then
left = left * tonumber(token)
operator = nil
else
error("unexpected operator!")
end
end
return left
end
local function part2_eval(expr)
return eval(expr:gsub("(%d+%s+[%d%+%s]+%s+%d+)", "(%1)"), part1_eval)
end
local part1 = 0
local part2 = 0
for line in io.lines() do
part1 = part1 + eval(line, part1_eval)
part2 = part2 + eval(line, part2_eval)
end
print(part1)
print(part2)

39
2020/19/day19.pl Normal file
View File

@ -0,0 +1,39 @@
#!/usr/bin/env perl
use strict;
use warnings;
sub parse_rule {
my ($rule) = @_;
$rule =~ s/"//g;
$rule =~ s/(\d+)(?![\d:])/(?&rule$1)/g;
$rule =~ s/^(\d+):\s*(.*)$/(?<rule$1>$2)/;
return $rule;
}
my $rules = "";
while (<>) {
chomp;
if ($_ eq "") {
last;
}
$rules .= parse_rule($_) . "\n";
}
my $regexp = "(?(DEFINE)" . $rules . ")^(?&rule0)\$";
my $newrule8 = parse_rule('8: 42 | 42 8');
my $newrule11 = parse_rule('11: 42 31 | 42 11 31');
my $regexp2 = $regexp;
$regexp2 =~ s/\(\?<rule8>.*\)/$newrule8/;
$regexp2 =~ s/\(\?<rule11>.*\)/$newrule11/;
$regexp =~ s/\s*//g;
$regexp2 =~ s/\s*//g;
my $part1 = 0, my $part2 = 0;
while (<>) {
$part1 += ($_ =~ $regexp);
$part2 += ($_ =~ $regexp2);
}
print $part1, "\n", $part2, "\n";

33
2021/1/day1.lua Normal file
View File

@ -0,0 +1,33 @@
#!/usr/bin/env lua
local numbers = {}
for line in io.lines() do
table.insert(numbers, tonumber(line))
end
local function part1()
local count = 0
for i = 2, #numbers do
if numbers[i-1] < numbers[i] then
count = count + 1
end
end
return count
end
local function part2()
local count = 0
for i = 4, #numbers do
local previous_sum = numbers[i-3] + numbers[i-2] + numbers[i-1]
local current_sum = numbers[i-2] + numbers[i-1] + numbers[i]
if previous_sum < current_sum then
count = count + 1
end
end
return count
end
print(part1())
print(part2())

22
2021/2/day2.lua Normal file
View File

@ -0,0 +1,22 @@
#!/usr/bin/env lua
local position = 0
local depth = 0
local aim = 0
for line in io.lines() do
_, _, action, value = line:find('(%a+)%s+(%d+)')
value = tonumber(value)
if action == "forward" then
position = position + value
depth = depth + aim * value
elseif action == "up" then
aim = aim - value
elseif action == "down" then
aim = aim + value
else
error("unknown action")
end
end
print(position * aim)
print(position * depth)

View File

@ -10,9 +10,9 @@ is acceptable; anything goes as long as I solve it myself!
## Progress
```
15 16 17 18 19 20
1 ██ ██ ██ ██ ██
2 ██ ██ ██ ██ ██
15 16 17 18 19 20 21
1 ██ ██ ██ ██ ██ ██
2 ██ ██ ██ ██ ██ ██
3 ██ ██ ██ ██ ██
4 ██ ██ ██ ██
5 ██ ██ ██ ██
@ -23,13 +23,13 @@ is acceptable; anything goes as long as I solve it myself!
10 ██ ██ ██
11 ██ ██ ██
12 ██ ██ ██
13 ██ █
13 ██ █
14 ██ ██ ██
15 ██ ██
16 ██ ██ ██
17 ██ ██
18 ██
19
18 ██ ██
19 ██
20 ██
21
22 █