-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday6a.rb
98 lines (91 loc) · 2.06 KB
/
day6a.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
98
inputstring=File.read( ARGV[0] )
zeilen=inputstring.chomp.split("\n").length
spalten=inputstring.split("\n")[0].length
puts "Spalten=#{spalten}, Zeilen=#{zeilen}"
class Map
def initialize(ymax,xmax)
@ymax=ymax
@xmax=xmax
@map = Array.new( ymax ) { Array.new( xmax, "o") }
@pos=Array.new
@direction=[-1,0] #up
@left_coordinate_system=false
end
def prettyprint
for y in 0..(@map.length-1)
for x in 0..(@map[y].length-1)
print @map[y][x]
end
print "\n"
end
puts "Pos=#{@pos} Dir=#{@direction}"
end
def parseInputString( str)
str.split("\n").each_with_index do |line,y|
line=line.chomp
line.split('').each_with_index do | char, x |
# puts "y=#{y}, x=#{x}, char=#{char}"
@map[y][x]=char
if char == '^'
@pos=[y,x]
end
end
end
end
def directionChooser( current) # => next
return [0,1] if current==[-1,0]
return [1,0] if current==[0,1]
return [0,-1] if current==[1,0]
return [-1,0] if current==[0,-1]
end
def canWalk?
newy=@pos[0]+@direction[0]
newx=@pos[1]+@direction[1]
return true if newy < 0
return true if newy >= @ymax
return true if newx < 0
return true if newx >= @xmax
return true if @map[newy][newx] != '#'
return false
end
def walk
if ! self.canWalk?
@direction=directionChooser( @direction)
return
end
if @pos[0] >= 0 &&
@pos[0] < @ymax &&
@pos[1] >= 0 &&
@pos[1] < @xmax
@map[@pos[0]][@pos[1]]="X"
@pos[0] += @direction[0]
@pos[1] += @direction[1]
else
@left_coordinate_system = true
end
end
def done?
return @left_coordinate_system
end
def cnt
c=Hash.new(0)
for y in 0..(@map.length-1)
for x in 0..(@map[y].length-1)
c[@map[y][x]]+=1
end
end
c.keys.sort.each do |k|
puts "Lösung #{k}=#{c[k]}"
end
end
end
m=Map.new(zeilen,spalten)
m.prettyprint
m.parseInputString( inputstring)
m.prettyprint
puts m.canWalk?
while ! m.done?
m.walk
# m.prettyprint
end
m.cnt