parent
							
								
									2b722205d2
								
							
						
					
					
						commit
						764c2e4942
					
				@ -0,0 +1,77 @@
 | 
				
			||||
#!/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()
 | 
				
			||||
@ -0,0 +1,6 @@
 | 
				
			||||
2-4,6-8
 | 
				
			||||
2-3,4-5
 | 
				
			||||
5-7,7-9
 | 
				
			||||
2-8,3-7
 | 
				
			||||
6-6,4-6
 | 
				
			||||
2-6,4-8
 | 
				
			||||
											
												
													File diff suppressed because it is too large
													Load Diff
												
											
										
									
								
					Loading…
					
					
				
		Reference in new issue