-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06-wait-for-it.jl
More file actions
51 lines (37 loc) · 1.1 KB
/
Copy path06-wait-for-it.jl
File metadata and controls
51 lines (37 loc) · 1.1 KB
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
# input_filename = "example.txt"
input_filename = "input.txt"
function nb_of_int(x, y)
return ceil(Int, y-1) - floor(Int, x)
end
function compute_score(course_time, record)
delta = course_time^2 - 4 * record
x1 = (-course_time + sqrt(delta)) / (-2)
x2 = (-course_time - sqrt(delta)) / (-2)
return nb_of_int(x1, x2)
end
function part_I(lines)
times = parse.(Int, split(lines[1], r"\s+")[begin+1:end])
dist = parse.(Int, split(lines[2], r"\s+")[begin+1:end])
print(times, "\n")
print(dist, "\n")
score = 1
for i in eachindex(times)
local_score = compute_score(times[i], dist[i])
score *= local_score
end
return score
end
function part_II(lines)
time = parse(Int, replace(split(lines[1], ":")[end], " " => ""))
dist = parse(Int, replace(split(lines[2], ":")[end], " " => ""))
print(time, "\n")
print(dist, "\n")
return compute_score(time, dist)
end
f = open(input_filename, "r")
lines = readlines(f)
close(f)
score_I = part_I(lines)
print("Part I: ", score_I, "\n\n")
score_II = part_II(lines)
print("Part II: ", score_II, "\n")