-
Notifications
You must be signed in to change notification settings - Fork 0
/
day23.jl
79 lines (71 loc) · 2.19 KB
/
day23.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
function iterate(n, pos, max_val)
if pos % 100 == 0
@show pos
end
wrapped_pos = mod(pos, length(n))
wrapped_pos = wrapped_pos == 0 ? length(n) : wrapped_pos
cur_cup = n[wrapped_pos]
inds = replace(mod.(pos + 1 : pos + 3, length(n)), 0=>length(n))
to_move = n[inds]
remaining = setdiff(n, to_move)
destination_cup, dest_idx = find_dest(remaining, cur_cup, max_val)
n2 = vcat(remaining[1:dest_idx], to_move, remaining[dest_idx+1:end])
# Carry out a circle shift to get cur_cup into pos
idx_diff = pos - findfirst(isequal(cur_cup), n2)
circshift(n2, idx_diff)
end
function find_dest(v, val, max_val)
len_v = length(v)
for dest_val in (mod(val-idx, max_val) for idx in 1:max_val)
if dest_val == 0
dest_val = max_val
end
idx = findfirst(isequal(dest_val), v)
if idx !== nothing
return v[idx], idx
end
end
error("did not find a proper destination with $v and $val")
end
function solve(numbers, n_moves)
n = copy(numbers)
max_val = maximum(n)
for pos in 1:n_moves
# @show n, pos
n = iterate(n, pos, max_val)
end
n
end
function part1(numbers, n_moves)
n = solve(numbers, n_moves)
idx_1 = findfirst(isequal(1), n)
len_n = length(n)
inds_after_1 = [mod(idx_1 + idx, len_n) for idx in 1:len_n-1]
replace!(inds_after_1, 0=>len_n)
join(n[inds_after_1])
end
function part2(numbers, n_moves)
# n = solve(numbers, n_moves)
# idx_1 = findfirst(isequal(1), n)
# len_n = length(n)
# inds_after_1 = [mod(idx_1 + idx, len_n) for idx in 1:2]
# replace!(inds_after_1, 0=>len_n)
# prod(inds_after_1)
end
function create_successor_vec(numbers)
vs = zeros(Int, length(numbers))
for (v, v_next) in pairwise(numbers)
vs[v] = v_next
end
vs[numbers[end]] = first(numbers)
vs
end
function pairwise(xs)
zip(xs, Iterators.drop(xs, 1))
end
# filename = "inputs/day23.txt"
filename = "example_day23.txt"
numbers = [parse(Int, x) for x in readline(filename)]
part1_solution = part1(numbers, 100)
part2_nums = vcat(numbers, (maximum(numbers)+1):1_000_000)
part2_solution = part2(part2_nums, 10_000_000)