-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.jl
117 lines (104 loc) · 2.62 KB
/
10.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
function get_locations(data)
rows = split(data,"\n")
output = Set{(Tuple{Int,Int})}()
for (y,r) in enumerate(rows)
for (x,e) in enumerate(split(r,""))
e == "#" && push!(output,(x-1,y-1))
end
end
return output
end
function hits(p1,p2,p)
if p1[1] == p2[1] == p[1]
x = (p[2] - p1[2])/(p2[2]-p1[2])
elseif p1[2] == p2[2] == p[2]
x = (p[1] - p1[1])/(p2[1]-p1[1])
elseif p1[1] == p2[1] || p1[2] == p2[2]
return false
else
x1 = (p .- p1)./(p2.-p1)
x1[1] ≈ x1[2] || return false
x = x1[1]
end
return 0.0 <= x <= 1.0
end
function check_intersections(coords)
coord_count = Dict{Tuple{Int,Int},Int}()
for testp in coords
coord_count[testp] = check_intersections(coords,testp)
end
return coord_count
end
function check_intersections(coords,testp)
c = 0
for reach in coords
reach == testp && continue
any(hits(testp,reach,p) for p in coords if p != testp && p != reach) && continue
c += 1
end
return c
end
data = """.#..#
.....
#####
....#
...##"""
coords = get_locations(data)
check_intersections(coords)
coords = get_locations(read("10.input",String))
inter = check_intersections(coords)
function in_sight(coords,testp)
in_sight = Set{Tuple{Int,Int}}()
for reach in coords
reach == testp && continue
any(hits(testp,reach,p) for p in coords if p != testp && p != reach) && continue
push!(in_sight,reach)
end
return in_sight
end
function angle(p1,p2)
v1 = p2.-p1
a = pi/2-atan(-v1[2],v1[1])
a < 0 && return (2π+a)%(2π)
return a%(2π)
end
function vaporize_circle(coords,testp)
sight = collect(in_sight(coords,testp))
sort!(sight,by=x->angle(testp,x))
end
function vaporize(coords,testp)
_coords = copy(coords)
vaporized = Tuple{Int,Int}[]
while length(_coords) > 1
vap = vaporize_circle(_coords,testp)
append!(vaporized,vap)
setdiff!(_coords,Set(vap))
end
return vaporized
end
data = """.#..##.###...#######
##.############..##.
.#.######.########.#
.###.#######.####.#.
#####.##.#.##.###.##
..#####..#.#########
####################
#.####....###.#.#.##
##.#################
#####.##.###..####..
..######..##.#######
####.##.####...##..#
.#####..#.######.###
##...#.##########...
#.##########.#######
.####.#.###.###.#.##
....##.##.###..#####
.#.#.###########.###
#.#.#.#####.####.###
###.##.####.##.#..##"""
coords = get_locations(data)
res = vaporize(coords,(11,13))
res[200]
coords = get_locations(read("10.input",String))
res = vaporize(coords,(19,11))
res[200][1]*100+res[200][2]