You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
1.6 KiB
77 lines
1.6 KiB
#!/usr/bin/env julia
|
|
|
|
using DelimitedFiles
|
|
|
|
infile = size(ARGS, 1) > 0 ? ARGS[1] : "day6input.txt"
|
|
println("infile = ", infile)
|
|
|
|
fish = vec(readdlm(infile, ',', Int))
|
|
|
|
function simulate_lanternfish(fish, days)
|
|
fish = copy(fish)
|
|
for day in 1:days
|
|
newfish = 0
|
|
for i in 1:length(fish)
|
|
fish[i] -= 1
|
|
if fish[i] < 0
|
|
fish[i] = 6
|
|
newfish += 1
|
|
end
|
|
end
|
|
append!(fish, repeat([8], newfish))
|
|
end
|
|
return fish
|
|
end
|
|
|
|
fish18 = simulate_lanternfish(fish, 18)
|
|
fish80 = simulate_lanternfish(fish18, 80-18)
|
|
|
|
println("18 : ", length(fish18))
|
|
println("80 : ", length(fish80))
|
|
|
|
using OffsetArrays
|
|
|
|
function simulate2(fish, days)
|
|
fish_map = OffsetVector(zeros(Int, 9), 0:8)
|
|
for i in 1:length(fish)
|
|
fish_map[fish[i]] += 1
|
|
end
|
|
for day in 1:days
|
|
add_next = 0
|
|
for c in 8:-1:0
|
|
tmp = fish_map[c]
|
|
fish_map[c] = add_next
|
|
add_next = tmp
|
|
end
|
|
fish_map[8] = add_next
|
|
fish_map[6] += add_next
|
|
end
|
|
return sum(fish_map)
|
|
end
|
|
|
|
function simulate3(fish, days)
|
|
fish_map = zeros(Int, 9)
|
|
for i in 1:length(fish)
|
|
fish_map[fish[i]+1] += 1
|
|
end
|
|
for day in 1:days
|
|
fish_map = circshift(fish_map, -1)
|
|
fish_map[7] += fish_map[9]
|
|
end
|
|
return sum(fish_map)
|
|
end
|
|
|
|
|
|
println("18 : ", simulate2(fish, 18))
|
|
println("80 : ", simulate2(fish, 80))
|
|
println("256: ", simulate2(fish, 256))
|
|
|
|
println("18 : ", simulate3(fish, 18))
|
|
println("80 : ", simulate3(fish, 80))
|
|
println("256: ", simulate3(fish, 256))
|
|
|
|
using BenchmarkTools
|
|
|
|
@btime simulate2(fish, 256)
|
|
@btime simulate3(fish, 256)
|