J

#### James K.

The explanation of what I'm trying to do are noted in the comments...
I'm freakin' lost... the really big issue is determining how to test
mines that have been blown up against remaining
mines... you'll find that comment smack dab in the middle...

today is the first day I've ever used Ruby, I appreciate the help:

## Load data from file into Python
mines_data = file.open('example_mines.txt', 'r')

## function to determine if a mine blows up another mine
((x1 - x2) ** 2 + (y1 - y2) ** 2) <= (blast_radius ** 2)
end

## define variables to keep track of outputs
qty_mines_blown = 0
max_mines_blown = 0

## define a multidimensional array with x number of rows and 3 columns
master_array = array.new(r) {Array.new(3)}
cnt = 0
starting_mine = master_array [cnt]
# For Each Mine do this:
master_array.each = {|r|
if bomb x1, y1 blows up bomb x2, y2
master_array.each{|r|
master_array[cnt], master_array[r], master_array[r]) = True
qty_mines_blown += 1
## code to test mines that have been blown up against remaining
mines
endif
}
if qty_mines_blown > max_mines_blown
max_mines_blown = qty_mines_blown
elseif qty_mines_blown = max_mines_blown
endif
cnt += 1
}
print "The maximum number of mines that can be blown up from triggering
just one mine is: "
print max_mines_blown
print "The location and blast radius of mine(s) that can trigger the
blast is :"

R

#### Robert Klemme

The explanation of what I'm trying to do are noted in the comments...
I'm freakin' lost... the really big issue is determining how to test
mines that have been blown up against remaining
mines... you'll find that comment smack dab in the middle...

today is the first day I've ever used Ruby, I appreciate the help:

## Load data from file into Python
mines_data =3D file.open('example_mines.txt', 'r')

You are not loading data here, you are merely opening a file for
reading - and then do nothing with it. Did you mean any of these?

## function to determine if a mine blows up another mine
=A0((x1 - x2) ** 2 + (y1 - y2) ** 2) <=3D (blast_radius ** 2)
end

## define variables to keep track of outputs
qty_mines_blown =3D 0
max_mines_blown =3D 0

## define a multidimensional =A0array with x number of rows and 3 columns
master_array =3D array.new(r) {Array.new(3)}

Spelling error. Also, in the comment you write "x" but in the code it's "r=
".
cnt =3D 0
starting_mine =3D master_array [cnt]
# For Each Mine do this:
master_array.each =3D {|r|
=A0 =A0# within_blast_radius(x1, y1, bomb_radius, x2, y2) =A0## Tests to = see
if bomb x1, y1 blows up bomb x2, y2
=A0 =A0master_array.each{|r|

The "r" used here shadows the "r" used in the outer #each block. This
is likely an issue.
master_array[cnt], master_array[r], master_array[r]) =3D True
=A0 =A0 =A0 =A0qty_mines_blown +=3D 1
=A0 =A0 =A0 =A0## code to test mines that have been blown up against rema= ining
mines
=A0 =A0endif
=A0 =A0}
=A0 =A0if qty_mines_blown > max_mines_blown
=A0 =A0 =A0 =A0max_mines_blown =3D qty_mines_blown
=A0 =A0 =A0 =A0answers_array =3D starting_mine
=A0 =A0elseif qty_mines_blown =3D max_mines_blown
=A0 =A0 =A0 =A0answers_array.next_line =3D starting_mine
=A0 =A0endif
=A0 =A0cnt +=3D 1
=A0 =A0}
print "The maximum number of mines that can be blown up from triggering
just one mine is: "
print max_mines_blown
print "The location and blast radius of mine(s) that can trigger the
blast is :"

If I understand your problem properly basically this is what you want
to do: for all possible locations of a bomb in the grid check how many
mines can be blown up from here including mines which are blown by
other mines. Determine the maximum number of mines blown up.

Going at this top down (i.e. from the most abstract to the more
detailed) a simple implementation would look like this

grid =3D Array.new(rows) { Array.new(columns) }
# fill grid

max_blown =3D 0

grid.each_with_index do |row, row_idx|
row.size.times do |col_idx|
blows =3D calc_blows(row_idx, col_idx)
max_blown =3D blows if blows > max_blown
end
end

printf "Max number of blows in grid: %d\n", max_blown

Now we need to determine the number of blows. We start from the
center (i.e. a possible bomb position) and increase the radius until
we find no more explosions:

def calc_blows(x, y)
blown =3D [[x,y]] # init with the bomb

# increase the radius until we do not find
# anything which gets blown
begin
current =3D find_blown(x, y, radius, blown)
sum.concat current
end until current.empty?

blown.size
end

Now you only need to implement find_blown() - and loading of the grid
from the file.

It seems this straightforward approach does several calculations
multiple times. I guess if we look long enough on the problem we will
find a solution which is more efficient. This sounds like a candidate
for dynamic programming but I don't have the time right now.

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

J

#### James K.

Thanks for your advice! I solved the issue and pretty much gutted the
logic.

this thread can be closed if someone can do so