-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10-pipe-maze.jl
More file actions
162 lines (140 loc) Β· 4.1 KB
/
Copy path10-pipe-maze.jl
File metadata and controls
162 lines (140 loc) Β· 4.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# input_filename = "ex1.txt"
# input_filename = "ex2.txt"
# input_filename = "ex3.txt"
input_filename = "input.txt"
up = (-1, 0, ['7', 'F', '|', 'S'], 'd')
down = ( 1, 0, ['L', 'J', '|', 'S'], 'u')
left = ( 0,-1, ['L', 'F', '-', 'S'], 'r')
right= ( 0, 1, ['7', 'J', '-', 'S'], 'l')
maps = Dict(
'|' => [up, down],
'-' => [left, right],
'L' => [up, right],
'J' => [left, up],
'7' => [left, down],
'F' => [down, right],
)
from = Dict(
'u' => 'd',
'd' => 'u',
'l' => 'r',
'r' => 'l',
)
char_convert = Dict(
'|' => 'β',
'-' => 'β',
'L' => 'β',
'J' => 'β',
'7' => 'β',
'F' => 'β',
'S' => 'S',
)
function look_for_S(lines)
for (i, line) in enumerate(lines)
for (j, c) in enumerate(line)
if c == 'S'
return (i, j)
end
end
end
end
function size_loop_aux(lines, i, j, source, elts, D=0)
# println("**** Call with ", i, ", ", j, " ( ", source, " ), D = ", D)
if lines[i][j] == 'S'
# println("---------------> Found S")
return D+1, elts
else
for (di, dj, compat, dir) in get(maps, lines[i][j], [])
i_ = i + di
j_ = j + dj
# println(" Look to ", i_, ", ", j_, " ( ", compat, " ) di=", di, " dj=", dj, " dir=", dir, " source=", source, "")
if i_ >= 1 && i_ <= length(lines) && j_ >= 1 && j_ <= length(lines[begin])
# println(" -> ", lines[i_][j_], " ( ", compat, " )" )
if lines[i_][j_] in compat && from[dir] != source
# println(" Going to ", i_, ", ", j_, " ( ", lines[i_][j_], " )")
elts = vcat(elts, [(i, j, source)])
return size_loop_aux(lines, i_, j_, dir, elts, D+1)
end
end
end
end
end
function size_loop(lines, i0, j0)
for (di, dj, compat, source) in [up, down, left, right]
i_ = i0 + di
j_ = j0 + dj
# println("Look to ", i_, ", ", j_, " ( ", compat, " )")
if i_ > 0 && i_ <= length(lines) && j_ > 0 && j_ <= length(lines[begin])
if lines[i_][j_] in compat
# println(" Going to ", i_, ", ", j_, " ( ", lines[i_][j_], " )")
D, elts = size_loop_aux(lines, i_, j_, source, [(i0, j0, "X")], 0)
return div(D, 2), elts
end
end
end
end
function print_path(lines, elts)
new_lines = []
for line in lines
push!(new_lines, collect(line))
end
for (i, j, dir) in elts
new_lines[i][j] = char_convert[lines[i][j]]
end
return new_lines
end
function get_dir(i, j, elts)
for dir in ['u', 'd', 'l', 'r']
if (i, j, dir) in elts
return dir
end
end
return '.'
end
function count_inside(lines, elts)
count = 0
for (i, line) in enumerate(lines)
print('\n')
# in_path = ((i, 0) in elts) && (line[begin] in ['L', 'J', '7', 'F', '|', 'S'])
in_path = false
look_for = 'u'
for (j, c) in enumerate(line)
dir = get_dir(i, j, elts)
if dir != '.'
if c == 'J' && dir == 'l'
dir = 'd'
end
if dir in ['u', 'd'] && dir == look_for
in_path = !in_path
if in_path
println(i, ", ", j, ": Entering")
else
println(i, ", ", j, ": Exiting")
end
look_for = (look_for == 'u') ? 'd' : 'u'
end
else
println(i, ", ", j, " : ", c, " (", in_path, ")" )
if in_path
count += 1
end
end
end
end
return count
end
f = open(input_filename, "r")
lines = readlines(f)
close(f)
S = look_for_S(lines)
D, elts = size_loop(lines, S...)
# println(elts)
println("I: ", D)
# II = count_inside(lines, elts)
# println("II: ", II)
new_lines = print_path(lines, elts)
f = open("output.txt", "w")
for line in new_lines
println(f, join(line, ""))
end
close(f)