diff --git a/day_23/first.rb b/day_23/first.rb new file mode 100644 index 0000000..81884b1 --- /dev/null +++ b/day_23/first.rb @@ -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) diff --git a/day_23/second.jl b/day_23/second.jl new file mode 100644 index 0000000..a41af02 --- /dev/null +++ b/day_23/second.jl @@ -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]])