Solution for Day 23
This commit is contained in:
parent
7fa8c31b11
commit
82ffe815a3
44
day_23/first.rb
Normal file
44
day_23/first.rb
Normal file
@ -0,0 +1,44 @@
|
||||
def game(cups, moves)
|
||||
idx = 0
|
||||
|
||||
moves.times do |i|
|
||||
cycle = 0
|
||||
pickup = begin
|
||||
p = cups.slice!(idx + 1, 3)
|
||||
cycle = 3 - p.size
|
||||
p += cups.slice!(0, 3 - p.size) if p.size < 3
|
||||
p
|
||||
end
|
||||
|
||||
prev_cup = cups[idx - cycle]
|
||||
curr_cup = if idx < cups.size
|
||||
cups[idx]
|
||||
else
|
||||
cups[idx - cycle]
|
||||
end
|
||||
|
||||
dest_idx = begin
|
||||
tmp = 1
|
||||
loop do
|
||||
if pickup.include?(curr_cup - tmp)
|
||||
tmp += 1
|
||||
elsif cups.min > (curr_cup - tmp)
|
||||
break cups.index(cups.max)
|
||||
else
|
||||
break cups.index(curr_cup - tmp)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
cups.insert(dest_idx + 1, *pickup)
|
||||
cups.push(cups.shift) while cups[idx] != prev_cup
|
||||
|
||||
idx = (idx + 1) % cups.size
|
||||
end
|
||||
cups.push(cups.shift) until cups.first == 1
|
||||
cups.drop(1).join
|
||||
end
|
||||
|
||||
input = [5, 6, 2, 8, 9, 3, 1, 4, 7]
|
||||
|
||||
puts game(input, 100)
|
41
day_23/second.jl
Normal file
41
day_23/second.jl
Normal file
@ -0,0 +1,41 @@
|
||||
function game(input)
|
||||
map = Array{Int}(undef, 1000000)
|
||||
|
||||
for i = 1:1000000
|
||||
if i < length(input)
|
||||
map[input[i]] = input[i + 1]
|
||||
elseif i == length(input)
|
||||
map[input[end]] = maximum(input) + 1
|
||||
else
|
||||
map[i] = i + 1
|
||||
end
|
||||
end
|
||||
|
||||
map[1000000] = input[1]
|
||||
|
||||
curr = input[1]
|
||||
|
||||
for i = 1:10000000
|
||||
p1 = map[curr]
|
||||
p2 = map[p1]
|
||||
p3 = map[p2]
|
||||
tmp = 0
|
||||
|
||||
map[curr] = map[p3]
|
||||
|
||||
tmp = curr == 1 ? 1000000 : curr - 1
|
||||
|
||||
while tmp in [p1,p2,p3]
|
||||
tmp = tmp == 1 ? 1000000 : tmp - 1
|
||||
end
|
||||
|
||||
map[p3] = map[tmp]
|
||||
map[tmp] = p1
|
||||
curr = map[curr]
|
||||
end
|
||||
map
|
||||
end
|
||||
|
||||
input = [5, 6, 2, 8, 9, 3, 1, 4, 7]
|
||||
out = game(input)
|
||||
println(out[1] * out[out[1]])
|
Loading…
x
Reference in New Issue
Block a user