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.
78 lines
1.5 KiB
78 lines
1.5 KiB
#!/usr/bin/env julia
|
|
|
|
using Test
|
|
|
|
function parse_line_ranges(line)
|
|
parts = split(line, ',')
|
|
ranges = Vector{UnitRange{Int64}}()
|
|
for p in parts
|
|
start, stop = split(p, '-', limit=2)
|
|
prange = UnitRange(parse(Int64, start), parse(Int64, stop))
|
|
push!(ranges, prange)
|
|
end
|
|
return ranges
|
|
end
|
|
|
|
function line_range_contained(line)
|
|
ranges = parse_line_ranges(line)
|
|
inter = intersect(ranges[1], ranges[2])
|
|
return (inter == ranges[1] || inter == ranges[2])
|
|
end
|
|
|
|
function line_range_overlap(line)
|
|
ranges = parse_line_ranges(line)
|
|
inter = intersect(ranges[1], ranges[2])
|
|
return length(inter) > 0
|
|
end
|
|
|
|
function count_range_contained(infile)
|
|
rsum = 0
|
|
|
|
open(infile, "r") do io
|
|
for line in eachline(io)
|
|
if line_range_contained(line)
|
|
rsum += 1
|
|
end
|
|
end
|
|
end
|
|
|
|
return rsum
|
|
end
|
|
|
|
function count_range_overlap(infile)
|
|
osum = 0
|
|
|
|
open(infile, "r") do io
|
|
for line in eachline(io)
|
|
if line_range_overlap(line)
|
|
osum += 1
|
|
end
|
|
end
|
|
end
|
|
|
|
return osum
|
|
end
|
|
|
|
function test()
|
|
@testset "elf cleaning contained" verbose=true begin
|
|
@test count_range_contained("example.txt") == 2
|
|
@test count_range_contained("input.txt") == 500
|
|
end
|
|
@testset "elf cleaning overlap" verbose=true begin
|
|
@test count_range_overlap("example.txt") == 4
|
|
@test count_range_overlap("input.txt") == 815
|
|
end
|
|
end
|
|
|
|
function main()
|
|
if size(ARGS, 1) == 0
|
|
test()
|
|
else
|
|
infile = ARGS[1]
|
|
println("infile = ", infile)
|
|
println("bad assignments: ", count_range_contained(infile))
|
|
end
|
|
end
|
|
|
|
main()
|