added 12023 files so far

This commit is contained in:
sejo 2023-12-06 21:12:03 +01:00
parent 5a24fcb705
commit d346e3341b
16 changed files with 1515 additions and 0 deletions

68
12023/01/01.lua Normal file
View File

@ -0,0 +1,68 @@
io.input("input")
sum1 = 0
sum2 = 0
digits = {
one=1,
two=2,
three=3,
four=4,
five=5,
six=6,
seven=7,
eight=8,
nine=9
}
for line in io.lines() do
-- print(line)
di1 = line:match("^%a*(%d)")
df1 = line:match("(%d)%a*$")
num1 = tonumber(di1)*10 + tonumber(df1)
sum1 = sum1 + num1
found = {}
for d,v in pairs(digits) do
init = 1
repeat
o,f = line:find(d, init)
if o then
table.insert(found, {s=d,["o"]=o,["f"]=f})
init = f+1
end
until not o
end
table.sort(found, function(a,b) return a.o < b.o end)
--[[
for i, d in ipairs(found) do
print(d.s, d.o, d.f)
end
--]]
firstindexnum = line:find("%d")
firstindextxt = #line
lastindextxt = 1
if #found>0 then
firstindextxt = found[1].o
lastindextxt = found[#found].f
end
lastindexnum = line:find("%d[%a]*$",lastindextxt)
-- print(firstindex, lastindex, firstindexnum, lastindexnum)
if not firstindexnum or firstindextxt < firstindexnum then
di2 = digits[ found[1].s ]
else
di2 = tonumber(line:sub(firstindexnum,firstindexnum))
end
if not lastindexnum then
df2 = digits[found[#found].s]
else
df2 = tonumber(line:sub(lastindexnum,lastindexnum))
end
-- print(di2, df2)
num2 = di2*10 + df2
sum2 = sum2 + num2
end
print("part 1", sum1)
print("part 2", sum2)

1000
12023/01/input2 Normal file

File diff suppressed because it is too large Load Diff

4
12023/01/test Normal file
View File

@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet

7
12023/01/test2 Normal file
View File

@ -0,0 +1,7 @@
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen

32
12023/02/02.lua Normal file
View File

@ -0,0 +1,32 @@
io.input("input")
sum1 = 0
sum2 = 0
maxs = { r=12, g=13, b=14 }
for line in io.lines() do
print(line)
id = line:match("Game (%d+):")
print(id)
is_valid = true
mins = {r=0, g=0, b=0}
for reveal in line:gmatch("[%d%a, ]+") do
r = tonumber(reveal:match("(%d+) red"))
g = tonumber(reveal:match("(%d+) green"))
b = tonumber(reveal:match("(%d+) blue"))
print(reveal,r,g,b)
if (r and r>maxs.r) or (g and g>maxs.g) or (b and b>maxs.b) then
is_valid = false
end
if r and r>mins.r then mins.r = r end
if g and g>mins.g then mins.g = g end
if b and b>mins.b then mins.b = b end
end
if is_valid then
print("valid", id)
sum1 = sum1 + id
end
sum2 = sum2 + mins.r*mins.g*mins.b
print(mins.r, mins.g, mins.b)
end
print("part 1", sum1)
print("part 2", sum2)

5
12023/02/test Normal file
View File

@ -0,0 +1,5 @@
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green

104
12023/03/03.lua Normal file
View File

@ -0,0 +1,104 @@
io.input("input")
symbols = "[%*%$%+%#%/%&%=%@%-%%]"
ast = "%*"
function coord(r,c) return r..","..c end
function add_pot_gear(coor, num)
if not pot_gears[coor] then
pot_gears[coor] = {}
end
table.insert(pot_gears[coor],num)
end
lines = {}
for line in io.lines() do
table.insert(lines, line)
end
pot_gears = {}
sum1 = 0
sum2 = 0
for i,line in ipairs(lines) do
-- print(i, line)
init = 1
repeat -- find all numbers in each line
o,f = line:find("%d+", init)
is_part = false
if o then
num = tonumber(line:sub(o,f))
-- right
if f<#line then
right = line:sub(f+1,f+1)
is_part = is_part or right:find(symbols)
if right:find(ast) then
coor = coord(i, f+1)
add_pot_gear(coor,num)
end
end
-- left
if o>1 then
left = line:sub(o-1,o-1)
is_part = is_part or left:find(symbols)
if left:find(ast) then
coor = coord(i, o-1)
add_pot_gear(coor,num)
end
end
-- top
if i>1 then
prev_line = lines[i-1]
colo = (o>1) and (o-1) or 1
colf = (f<#line) and (f+1) or f
top = prev_line:sub(colo,colf)
is_part = is_part or top:find(symbols)
ainit = 1
repeat
ao, af = top:find(ast, ainit)
if ao then
coor = coord(i-1, colo-1+ao)
add_pot_gear(coor,num)
ainit = af+1
end
until not ao
end
-- bottom
if i<#lines then
next_line = lines[i+1]
colo = (o>1) and (o-1) or 1
colf = (f<#line) and (f+1) or f
bottom = next_line:sub(colo,colf)
is_part = is_part or bottom:find(symbols)
ainit = 1
repeat
ao, af = bottom:find(ast, ainit)
if ao then
coor = coord(i+1,colo-1+ao)
add_pot_gear(coor,num)
ainit = af+1
end
until not ao
end
if is_part then sum1 = sum1 + num end
-- print(num,o,f,is_part)
init = f+1
end
until not o
end
print("part 1", sum1)
for coor,nums in pairs(pot_gears) do
if #nums==2 then
ratio = nums[1] * nums[2]
sum2 = sum2 + ratio
end
end
print("part 2", sum2)

10
12023/03/test Normal file
View File

@ -0,0 +1,10 @@
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..

10
12023/03/test2 Normal file
View File

@ -0,0 +1,10 @@
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..

42
12023/04/04.lua Normal file
View File

@ -0,0 +1,42 @@
io.input("input")
sum1 = 0
cards = {}
nlines = 198
for i=1,nlines do
cards[i] = 1
end
i = 1
for line in io.lines() do
pwinning, phave = line:match(": ([%d ]+) | ([%d ]+)$")
winning = {}
for win in pwinning:gmatch("%d+") do
winning[win] = true
end
points = 0
count = 0
for have in phave:gmatch("%d+") do
if winning[have] then
points = points==0 and 1 or (points*2)
count = count + 1
end
end
sum1 = sum1 + points
--print(count)
for n = (i+1),(i+count) do
cards[n] = cards[n] + cards[i]
end
i = i + 1
end
print("part 1", sum1)
sum2 = 0
for i,v in ipairs(cards) do
--print(i,v)
sum2 = sum2 + v
end
print("part 2:", sum2)

6
12023/04/test Normal file
View File

@ -0,0 +1,6 @@
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11

62
12023/05/05.lua Normal file
View File

@ -0,0 +1,62 @@
io.input("input")
seeds = {}
maps = {}
mapi = 0
for line in io.lines() do
if mapi == 0 then
s = {}
for num in line:gmatch("%d+") do
table.insert(s,tonumber(num))
end
table.insert(seeds, s)
end
if line:match("^$") then
if mapi>0 then
print("result")
for i, seed in ipairs(seeds[mapi]) do
if not seeds[mapi+1][i] then
seeds[mapi+1][i] = seed
end
print(i, seed, "->", seeds[mapi+1][i])
end
end
mapi = mapi + 1
table.insert(maps, {})
table.insert(seeds,{})
print(mapi)
goto continue
end
if mapi==0 or line:match("^[%a%- ]+:$") then
goto continue
end
dst, src, len = line:match("(%d+) (%d+) (%d+)")
dst = tonumber(dst)
src = tonumber(src)
len = tonumber(len)
print(dst, src, len)
for i, seed in ipairs(seeds[mapi]) do
if seed >= src and seed<src+len then -- transform
diff = seed - src
seeds[mapi+1][i] = dst + diff
print(seed,"->", dst+diff)
end
end
::continue::
end
print("final")
for i,s in ipairs(seeds[mapi]) do
if i==1 then min = s
else if s<min then min=s end
end
print(i,s)
end
print("part 1", min)

76
12023/05/05_part2.lua Normal file
View File

@ -0,0 +1,76 @@
--incomplete so far
io.input("input")
seeds = {}
nseeds = {}
mapi = 0
for line in io.lines() do
if mapi == 0 then
--[[
for num in line:gmatch("%d+") do
table.insert(seeds,tonumber(num))
end
--]]
--[
for start,range in line:gmatch("(%d+) (%d+)") do
start = tonumber(start)
range = tonumber(range)
for i=start,start+range-1 do
table.insert(seeds,i)
end
end
--]]
end
if line:match("^$") then
if mapi>0 then
-- print("result")
for i, seed in ipairs(seeds) do
if not nseeds[i] then
nseeds[i] = seed
end
-- print(i, seed, "->", nseeds[i])
end
for i,seed in ipairs(nseeds) do
seeds[i] = seed
end
nseeds = {}
end
mapi = mapi + 1
print(mapi)
goto continue
end
if mapi==0 or line:match("^[%a%- ]+:$") then
goto continue
end
dst, src, len = line:match("(%d+) (%d+) (%d+)")
dst = tonumber(dst)
src = tonumber(src)
len = tonumber(len)
-- print(dst, src, len)
for i, seed in ipairs(seeds) do
if seed >= src and seed<src+len then -- transform
diff = seed - src
nseeds[i] = dst + diff
-- print(seed,"->", dst+diff)
end
end
::continue::
end
print("final")
for i,s in ipairs(seeds) do
if i==1 then min = s
else if s<min then min=s end
end
print(i,s)
end
print("part 1", min)

34
12023/05/test Normal file
View File

@ -0,0 +1,34 @@
seeds: 79 14 55 13
seed-to-soil map:
50 98 2
52 50 48
soil-to-fertilizer map:
0 15 37
37 52 2
39 0 15
fertilizer-to-water map:
49 53 8
0 11 42
42 0 7
57 7 4
water-to-light map:
88 18 7
18 25 70
light-to-temperature map:
45 77 23
81 45 19
68 64 13
temperature-to-humidity map:
0 69 1
1 0 69
humidity-to-location map:
60 56 37
56 93 4

53
12023/06/06.lua Normal file
View File

@ -0,0 +1,53 @@
io.input("input")
time_line = io.read()
distance_line = io.read()
times = {}
distances = {}
time2 = ""
dist2 = ""
for t in time_line:gmatch("%d+") do
table.insert(times, tonumber(t))
time2 = time2 .. t
end
for d in distance_line:gmatch("%d+") do
table.insert(distances, tonumber(d))
dist2 = dist2 .. d
end
time2 = tonumber(time2)
dist2 = tonumber(dist2)
function dist(tt, tp) return tp*(tt-tp) end
function count(d, t)
-- v = Tp
-- d = Tp*Tr = Tp*(t-Tp) = -Tp²+t*Tp
-- -Tp² + t*Tp - d = 0
t1 = math.ceil((-t+math.sqrt(t*t-4*d))/(-2))
t2 = math.floor((-t-math.sqrt(t*t-4*d))/(-2))
c = t2-t1+1
-- we only care when the distance is greater
if dist(t,t1)==d then c = c - 2 end --t2 will be an integer too
return c
end
mul1 = 1
for i, t in ipairs(times) do
d = distances[i]
--[[
count = 0
for tp = 0,t do
if dist(t,tp)>d then count = count + 1 end
end
--]]
c = count(d,t)
mul1 = mul1*c
print(t,d,c)
end
print("part 1", mul1)
print("part 2", count(dist2,time2))

2
12023/06/test Normal file
View File

@ -0,0 +1,2 @@
Time: 7 15 30
Distance: 9 40 200