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.
		
		
		
		
		
			
		
			
				
					
					
						
							84 lines
						
					
					
						
							1.8 KiB
						
					
					
				
			
		
		
	
	
							84 lines
						
					
					
						
							1.8 KiB
						
					
					
				#!/usr/bin/env julia
 | 
						|
 | 
						|
using DelimitedFiles
 | 
						|
 | 
						|
infile = size(ARGS, 1) > 0 ? ARGS[1] : "day5input.txt"
 | 
						|
println("infile = ", infile)
 | 
						|
 | 
						|
# 0,9 -> 5,9
 | 
						|
line_re = r"(\d+),(\d+) -> (\d+),(\d+)"
 | 
						|
 | 
						|
open(infile, "r") do io
 | 
						|
    horiz = Vector[]
 | 
						|
    vert  = Vector[]
 | 
						|
    diag  = Vector[]
 | 
						|
    max_x = 0
 | 
						|
    max_y = 0
 | 
						|
 | 
						|
    for line in eachline(io)
 | 
						|
        m = match(line_re, line)
 | 
						|
        nums = [parse(Int, m[i])+1 for i in 1:4]
 | 
						|
        max_x = max(max_x, nums[1], nums[3])
 | 
						|
        max_y = max(max_y, nums[2], nums[4])
 | 
						|
        if nums[1] == nums[3]
 | 
						|
            if (nums[2] > nums[4])
 | 
						|
                nums[2], nums[4] = nums[4], nums[2]
 | 
						|
            end
 | 
						|
            push!(vert, nums)
 | 
						|
        elseif nums[2] == nums[4]
 | 
						|
            if (nums[1] > nums[3])
 | 
						|
                nums[1], nums[3] = nums[3], nums[1]
 | 
						|
            end
 | 
						|
            push!(horiz, nums)
 | 
						|
        else
 | 
						|
            push!(diag, nums)
 | 
						|
        end
 | 
						|
    end
 | 
						|
 | 
						|
    println("max ", max_x, ", ", max_y)
 | 
						|
    println("horiz ", length(horiz))
 | 
						|
    println("vert  ", length(vert))
 | 
						|
 | 
						|
    M = zeros(Int64, max_y, max_x)
 | 
						|
 | 
						|
    for hl in horiz
 | 
						|
        M[hl[2], hl[1]:hl[3]] .+= 1
 | 
						|
    end
 | 
						|
    for vl in vert
 | 
						|
        M[vl[2]:vl[4], vl[1]] .+= 1
 | 
						|
    end
 | 
						|
 | 
						|
    if (length(M) < 1000)
 | 
						|
        println("M")
 | 
						|
        display(M)
 | 
						|
        println()
 | 
						|
    end
 | 
						|
 | 
						|
    ndanger = length(findall(M .> 1))
 | 
						|
    println("dangerous coords ", ndanger)
 | 
						|
 | 
						|
    for d in diag
 | 
						|
        if (d[1] > d[3])
 | 
						|
            xrange = d[1]:-1:d[3]
 | 
						|
        else
 | 
						|
            xrange = d[1]:d[3]
 | 
						|
        end
 | 
						|
        if (d[2] > d[4])
 | 
						|
            yrange = d[2]:-1:d[4]
 | 
						|
        else
 | 
						|
            yrange = d[2]:d[4]
 | 
						|
        end
 | 
						|
        idx = [CartesianIndex(c) for c in zip(yrange, xrange)]
 | 
						|
        M[idx] .+= 1
 | 
						|
    end
 | 
						|
 | 
						|
    if (length(M) < 1000)
 | 
						|
        println("M")
 | 
						|
        display(M)
 | 
						|
        println()
 | 
						|
    end
 | 
						|
 | 
						|
    ndanger2 = length(findall(M .> 1))
 | 
						|
    println("dangerous coords (part 2) ", ndanger2)
 | 
						|
end
 |