Skip to content

Instantly share code, notes, and snippets.

@TravisL12
Last active December 23, 2015 23:09
Show Gist options
  • Select an option

  • Save TravisL12/6707744 to your computer and use it in GitHub Desktop.

Select an option

Save TravisL12/6707744 to your computer and use it in GitHub Desktop.
3;3;1 2 3 4 5 6 7 8 9
5;5; 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
4; 5; a b c d e f g h i j k l m n o p q r s t
6; 4; a b c d e f g h i j k l m n o p q r s t u v w x
4; 5; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
6; 3; a b c d e f g h i j k l m n o p q r
7; 4; 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
12; 3; a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J
9; 4; a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J
4; 3; a b c d e f g h i j k l
4; 12; a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V
7; 6; 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
3; 9; a b c d e f g h i j k l m n o p q r s t u v w x y z A
6; 3; a b c d e f g h i j k l m n o p q r
3; 8; a b c d e f g h i j k l m n o p q r s t u v w x
3; 4; a b c d e f g h i j k l
3; 3; a b c d e f g h i
5; 3; a b c d e f g h i j k l m n o
11; 3; a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G
4; 10; a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N
13; 3; a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M
3; 8; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
def spiral_matrix(row, col, numbers)
return 0 if numbers.size > row * col
total_rows = row.times.map { numbers.slice!(0..col-1) }
matrix_print = []
0.upto(row-1) do |index|
0.upto(col-1) { |val| matrix_print << total_rows[index][val] unless matrix_print.include?(total_rows[index][val]) }
0.upto(row-1) { |val| matrix_print << total_rows[val][(col-1)-index] unless matrix_print.include?(total_rows[val][(col-1)-index]) }
(col-1).downto(0) { |val| matrix_print << total_rows[(row-1)-index][val] unless matrix_print.include?(total_rows[(row-1)-index][val]) }
(row-1).downto(0) { |val| matrix_print << total_rows[val][index] unless matrix_print.include?(total_rows[val][index]) }
end
matrix_print.each { |num| print "#{num} " }
puts
end
inputfile = File.new(ARGV[0]).readlines.map { |val| val.chomp.split(";") }
inputfile.each do |input|
next if input.empty?
row = input[0].to_i
columns = input[1].to_i
numbers = input[2].split
spiral_matrix(row, columns, numbers)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment