parent
97ed63735a
commit
eb392d45b2
@ -1,30 +1,48 @@
|
||||
#!/usr/bin/env julia
|
||||
|
||||
infile = size(ARGS, 1) > 0 ? ARGS[1] : "day1input.txt"
|
||||
Ns = size(ARGS, 1) > 1 ? ARGS[2] : "1"
|
||||
println("infile = ", infile)
|
||||
|
||||
N = parse(Int64, Ns)
|
||||
using Test
|
||||
|
||||
max_calories = zeros(Int64, N)
|
||||
function elf_max_calories(infile)
|
||||
max_calories = zeros(Int64, 3)
|
||||
elf_calories = 0
|
||||
i = 1
|
||||
|
||||
io = open(infile, "r")
|
||||
open(infile, "r") do io
|
||||
for line in eachline(io)
|
||||
global i, max_calories, max_elf_idx, elf_calories
|
||||
if length(line) == 0
|
||||
if elf_calories > max_calories[1]
|
||||
max_calories[1] = elf_calories
|
||||
sort!(max_calories)
|
||||
end
|
||||
i += 1
|
||||
elf_calories = 0
|
||||
continue
|
||||
end
|
||||
snack_calories = parse(Int64, line)
|
||||
elf_calories += snack_calories
|
||||
end
|
||||
if elf_calories > max_calories[1]
|
||||
max_calories[1] = elf_calories
|
||||
sort!(max_calories)
|
||||
end
|
||||
end
|
||||
|
||||
return (max_calories[end], sum(max_calories))
|
||||
end
|
||||
|
||||
function test()
|
||||
@testset "elf snacks" verbose=true begin
|
||||
@test elf_max_calories("example.txt") == (24000, 45000)
|
||||
@test elf_max_calories("input.txt") == (69206, 197400)
|
||||
end
|
||||
end
|
||||
|
||||
function main()
|
||||
if size(ARGS, 1) == 0
|
||||
test()
|
||||
else
|
||||
infile = ARGS[1]
|
||||
println("infile = ", infile)
|
||||
println("snack calories: ", elf_max_calories(infile))
|
||||
end
|
||||
end
|
||||
|
||||
println(max_calories)
|
||||
println(sum(max_calories))
|
||||
main()
|
||||
|
||||
Loading…
Reference in new issue