-
Notifications
You must be signed in to change notification settings - Fork 1
/
day-12-part1.rb
97 lines (72 loc) · 1.54 KB
/
day-12-part1.rb
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
require 'benchmark'
path = File.join(__dir__, 'input.txt')
lines = *File.readlines(path).map {_1.chomp!}
input = lines.map{ |line| line.split("-") }
class Cave
attr_accessor :name, :connections
def initialize(name)
@visit_counter = 0
@name = name
@connections = []
end
def small?
@name == @name.downcase
end
def start?
@name == "start"
end
def explorable?
return false if start?
return @visit_counter < 1 if small?
true
end
def visit!
@visit_counter += 1
end
def add_connection(other_cave)
@connections << other_cave
end
def to_s
"'#{@name}'"
end
end
class Caves
def initialize(caves, map)
@caves = caves
@map = map
end
def step
end
def start
@caves.find
end
end
caves = input.flatten.uniq.map{ Cave.new(_1) }.inject({}) {|memo, cave| memo[cave.name] = cave; memo}
input.map{ |a,b| [caves[a], caves[b]] }.each do | a,b |
a.add_connection(b)
b.add_connection(a)
end
def visit(cave, path = [])
cave.visit!
path += [cave]
puts "walking: #{path.join(', ')}"
if cave.name == "end"
puts "end reached: #{path.join(',')}"
return path
else
# divert into multiple direction and collect the paths
cave.connections.map do |other_cave|
if other_cave.explorable?
if other_cave.name == 'b'
puts 'o'
end
puts "visit: #{cave} -> #{other_cave}"
visit(other_cave, path)
else
path
end
end
end
end
list = visit(caves['start'])
puts "ok"