-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12_1.jl
59 lines (51 loc) · 1.53 KB
/
12_1.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
#!/usr/bin/env julia
"""
https://adventofcode.com/2019/day/1
"""
function calculate_fuel_required(mass)
return calculate_fuel_required(parse(Int, mass))
end
function calculate_fuel_required(mass::Int)
fuel = floor(mass / 3) - 2
if fuel < 0
fuel = 0
end
return fuel
end
function calculate_fuel_required2(mass)
return calculate_fuel_required2(parse(Int, mass))
end
function calculate_fuel_required2(mass)
total_module_fuel = Int[]
# Calculate original fuel requirements
push!(total_module_fuel, calculate_fuel_required(mass))
# Calculate iterative fuel requirements
while total_module_fuel[end] > 0
push!(total_module_fuel, calculate_fuel_required(total_module_fuel[end]))
end
return sum(total_module_fuel)
end
function main1()
# Getting curl 400 error so downloading file beforehand
#input_file = download("https://adventofcode.com/2019/day/1/input")
input_file = joinpath(pwd(), "files", "12_1_input.txt")
total_fuel = zero(Int)
open(input_file, "r") do ifh
lines = [strip(line) for line in eachline(ifh)]
total_fuel = sum(calculate_fuel_required, lines)
end
println("Part 1 Answer:")
@show Int(total_fuel)
end
function main2()
input_file = joinpath(pwd(), "files", "12_1_input.txt")
total_fuel = zero(Int)
open(input_file, "r") do ifh
lines = [strip(line) for line in eachline(ifh)]
total_fuel = sum(calculate_fuel_required2, lines)
end
println("Part 2 Answer:")
@show Int(total_fuel)
end
main1()
main2()