day1: std input name, refactor jl

main
Bryce Allen 3 years ago
parent 97ed63735a
commit eb392d45b2

@ -1,30 +1,48 @@
#!/usr/bin/env julia #!/usr/bin/env julia
infile = size(ARGS, 1) > 0 ? ARGS[1] : "day1input.txt" using Test
Ns = size(ARGS, 1) > 1 ? ARGS[2] : "1"
println("infile = ", infile)
N = parse(Int64, Ns) function elf_max_calories(infile)
max_calories = zeros(Int64, 3)
elf_calories = 0
max_calories = zeros(Int64, N) open(infile, "r") do io
elf_calories = 0 for line in eachline(io)
i = 1 if length(line) == 0
if elf_calories > max_calories[1]
io = open(infile, "r") max_calories[1] = elf_calories
for line in eachline(io) sort!(max_calories)
global i, max_calories, max_elf_idx, elf_calories end
if length(line) == 0 elf_calories = 0
continue
end
snack_calories = parse(Int64, line)
elf_calories += snack_calories
end
if elf_calories > max_calories[1] if elf_calories > max_calories[1]
max_calories[1] = elf_calories max_calories[1] = elf_calories
sort!(max_calories) sort!(max_calories)
end end
i += 1
elf_calories = 0
continue
end end
snack_calories = parse(Int64, line)
elf_calories += snack_calories 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 end
println(max_calories) main()
println(sum(max_calories))

@ -46,13 +46,13 @@ where P: AsRef<Path>, {
} }
fn main() { fn main() {
aoc_day1("day1input.txt", 1); aoc_day1("input.txt", 1);
} }
#[test] #[test]
fn day1input() { fn day1input() {
assert_eq!(aoc_day1("day1input.txt", 1), 69206); assert_eq!(aoc_day1("input.txt", 1), 69206);
assert_eq!(aoc_day1("day1input.txt", 3), 197400); assert_eq!(aoc_day1("input.txt", 3), 197400);
} }
#[test] #[test]

Loading…
Cancel
Save