subparry_labyrinth_solver 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9b17cd6a8c8688ea730f7e22376498be890519374be6864366b9c84bd1b4b2a0
4
+ data.tar.gz: cc2d7cfe598d99a276b448babf7b1688b3618dca31567d62780ecb3907e5f756
5
+ SHA512:
6
+ metadata.gz: aed1745011a6b6308acee72a4b2713bd9cb940422266fe4b2986688fecf5bb46b0734472d1477131f553cdfed5e723c63b9bd673ae87781ce90fae54306f2670
7
+ data.tar.gz: 72affed219ac055ff39e4215e2586151c90dff1c19ff1bffdd56026ea866464d86f6f1d0d2bd8ddf3b03092027795743898df955987453acf52642740265bfd6
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## 1.0.0 2021-01-21
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c), Adrian Parry
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,8 @@
1
+ # subparry_labyrinth_solver
2
+
3
+ [![codecov](https://codecov.io/gh/subparry/subparry-labyrinth-solver/branch/master/graph/badge.svg?token=AHG1N8FZA0)](https://codecov.io/gh/subparry/subparry-labyrinth-solver)
4
+ [![Build Status](https://travis-ci.com/subparry/subparry-labyrinth-solver.svg?branch=master)](https://travis-ci.com/subparry/subparry-labyrinth-solver)
5
+
6
+ ## Description
7
+
8
+ Fun little project about labyrinth solving.
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rake'
4
+ require 'rubocop/rake_task'
5
+ RuboCop::RakeTask.new do |task|
6
+ task.requires << 'rubocop-performance'
7
+ task.requires << 'rubocop-rspec'
8
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require File.expand_path('lib/subparry_labyrinth_solver/version', __dir__)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'subparry_labyrinth_solver'
7
+ spec.version = LabyrinthSolver::VERSION
8
+ spec.authors = ['Adrian Parry']
9
+ spec.email = ['subparry@gmail.com']
10
+ spec.summary = 'An exercise for gem creation based on solving a labyrinth'
11
+ spec.description = 'This gem allows to find path for exiting a labyrinth'
12
+ spec.homepage = 'https://github.com/subparry/subparry-labyrinth-solver'
13
+ spec.license = 'MIT'
14
+ spec.platform = Gem::Platform::RUBY
15
+ spec.required_ruby_version = '>= 2.7.1'
16
+
17
+ spec.files = Dir[
18
+ 'README.md',
19
+ 'LICENSE',
20
+ 'CHANGELOG.md',
21
+ 'Gemfile',
22
+ 'Rakefile',
23
+ 'lib/**/*.rb',
24
+ 'labyrinth.gemspec'
25
+ ]
26
+ spec.extra_rdoc_files = ['README.md']
27
+
28
+ spec.add_development_dependency 'codecov', '~> 0.1'
29
+ spec.add_development_dependency 'rspec', '~> 3.0'
30
+ spec.add_development_dependency 'rubocop', '~> 1.8'
31
+ spec.add_development_dependency 'rubocop-performance', '~> 1.5'
32
+ spec.add_development_dependency 'rubocop-rspec', '~> 2.1'
33
+ spec.add_development_dependency 'simplecov', '~> 0.16'
34
+ end
File without changes
@@ -0,0 +1,3 @@
1
+ class MissingPathsError < ArgumentError; end
2
+ class InvalidMoveError < RuntimeError; end
3
+ class MissingCheeseError < ArgumentError; end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ Point = Struct.new(:x, :y)
6
+ # Labyrinth class in charge of keeping track of all nodes and performing movements
7
+ class Labyrinth
8
+ attr_reader :height, :width, :position
9
+
10
+ extend Forwardable
11
+
12
+ def_delegators :current_node, :open?, :cheese?, :close
13
+
14
+ def self.validate_data data
15
+ raise ArgumentError unless data.respond_to?(:each) && data.first.respond_to?(:each)
16
+ raise MissingCheeseError unless data.any? { |rows| rows.any? {|paths| paths[:cheese]} }
17
+ end
18
+
19
+ def initialize(data)
20
+ Labyrinth.validate_data data
21
+
22
+ @height = data.size
23
+ @width = data.first.size
24
+ @position = Point.new(0, 0)
25
+ @nodes = data.collect { |row| row.collect { |cell| Node.new(cell) } }
26
+ end
27
+
28
+ def go direction
29
+ raise InvalidMoveError, "Attempted to move #{direction}" unless open? direction
30
+
31
+ case direction
32
+ when :up
33
+ @position.y -= 1
34
+ when :down
35
+ @position.y += 1
36
+ when :left
37
+ @position.x -= 1
38
+ when :right
39
+ @position.x += 1
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def current_node
46
+ @nodes[@position.y][@position.x]
47
+ end
48
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Class to represent a single labyrinth square or node
4
+ class Node
5
+ def initialize(paths)
6
+ raise MissingPathsError if paths[:up].nil? || paths[:down].nil? || paths[:left].nil? || paths[:right].nil?
7
+
8
+ @paths = paths
9
+ @cheese = paths[:cheese] || false
10
+ end
11
+
12
+ def open? direction
13
+ @paths[direction]
14
+ end
15
+
16
+ def cheese?
17
+ @cheese
18
+ end
19
+
20
+ def close direction
21
+ @paths[direction] = false
22
+ end
23
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ # Takes a labyrinth and attempts to solve it saving the path taken
6
+ class Solver
7
+ attr_reader :path
8
+
9
+ extend Forwardable
10
+
11
+ OPPOSITE_DIRECTIONS = { up: :down, right: :left, down: :up, left: :right }.freeze
12
+
13
+ def_delegators :@labyrinth, :position, :cheese?, :go, :close
14
+
15
+ def initialize labyrinth
16
+ raise ArgumentError unless labyrinth.instance_of? Labyrinth
17
+
18
+ @labyrinth = labyrinth
19
+ @path = []
20
+ end
21
+
22
+ def go_next
23
+ next_dir = OPPOSITE_DIRECTIONS.find { |dir, opp| @labyrinth.open?(dir) && opp != @path.last }
24
+ return dead_end unless next_dir
25
+
26
+ go(next_dir.first)
27
+ @path.push(next_dir.first)
28
+ end
29
+
30
+ def solve
31
+ go_next until cheese?
32
+ end
33
+
34
+ private
35
+
36
+ def dead_end
37
+ to_close = path.pop
38
+ go(OPPOSITE_DIRECTIONS[to_close])
39
+ close(to_close)
40
+ end
41
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LabyrinthSolver
4
+ VERSION = '1.0.0'
5
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: subparry_labyrinth_solver
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Adrian Parry
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-01-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: codecov
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop-performance
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.5'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.1'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.16'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.16'
97
+ description: This gem allows to find path for exiting a labyrinth
98
+ email:
99
+ - subparry@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files:
103
+ - README.md
104
+ files:
105
+ - CHANGELOG.md
106
+ - Gemfile
107
+ - LICENSE
108
+ - README.md
109
+ - Rakefile
110
+ - labyrinth.gemspec
111
+ - lib/subparry_labyrinth_solver.rb
112
+ - lib/subparry_labyrinth_solver/exceptions.rb
113
+ - lib/subparry_labyrinth_solver/labyrinth.rb
114
+ - lib/subparry_labyrinth_solver/node.rb
115
+ - lib/subparry_labyrinth_solver/solver.rb
116
+ - lib/subparry_labyrinth_solver/version.rb
117
+ homepage: https://github.com/subparry/subparry-labyrinth-solver
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: 2.7.1
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubygems_version: 3.2.6
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: An exercise for gem creation based on solving a labyrinth
140
+ test_files: []