-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add puzzle status error Also, added basic test for the Puzzle object while getting unmarshall Signed-off-by: Rodolfo Sanchez <[email protected]> * format geometry pkg --------- Signed-off-by: Rodolfo Sanchez <[email protected]>
- Loading branch information
Showing
10 changed files
with
189 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,44 @@ | ||
package math | ||
|
||
import ( | ||
"math" | ||
"strconv" | ||
"math" | ||
"strconv" | ||
) | ||
|
||
type Point struct { | ||
X, Y float64 | ||
X, Y float64 | ||
} | ||
|
||
type Segment struct { | ||
A, B Point | ||
A, B Point | ||
} | ||
|
||
func CalDistancePoints(pos1 Point, pos2 Point) string { | ||
partX := math.Abs(float64(pos1.X) - float64(pos2.X)) | ||
partY := math.Abs(float64(pos1.Y) - float64(pos2.Y)) | ||
return strconv.Itoa(int(partX + partY)) | ||
partX := math.Abs(float64(pos1.X) - float64(pos2.X)) | ||
partY := math.Abs(float64(pos1.Y) - float64(pos2.Y)) | ||
return strconv.Itoa(int(partX + partY)) | ||
} | ||
|
||
|
||
func CalIntersectionPoint(a, b, c, d Point) (bool, Point) { | ||
cross := (c.Y-d.Y)*(b.X-a.X) - (c.X-d.X)*(b.Y-a.Y) | ||
if cross == 0 { | ||
return false, Point{} // Parallel lines, no intersection | ||
} | ||
|
||
t1 := float64((c.Y-d.Y)*(c.X-a.X) + (d.X-c.X)*(c.Y-a.Y)) / float64(cross) | ||
t2 := float64((a.Y-b.Y)*(c.X-a.X) + (b.X-a.X)*(c.Y-a.Y)) / float64(cross) | ||
|
||
p1 := Point{b.X - a.X, b.Y - a.Y} | ||
// Check if the intersection point is within the line segments | ||
if t1 >= 0 && t1 <= 1 && t2 >= 0 && t2 <= 1 { | ||
intersectionX := a.X + t1*p1.X | ||
intersectionY := a.Y + t1*p1.Y | ||
return true, Point{intersectionX, intersectionY} | ||
} | ||
|
||
return false, Point{} // The intersection point is outside the line segments | ||
cross := (c.Y-d.Y)*(b.X-a.X) - (c.X-d.X)*(b.Y-a.Y) | ||
if cross == 0 { | ||
return false, Point{} // Parallel lines, no intersection | ||
} | ||
|
||
t1 := float64((c.Y-d.Y)*(c.X-a.X)+(d.X-c.X)*(c.Y-a.Y)) / float64(cross) | ||
t2 := float64((a.Y-b.Y)*(c.X-a.X)+(b.X-a.X)*(c.Y-a.Y)) / float64(cross) | ||
|
||
p1 := Point{b.X - a.X, b.Y - a.Y} | ||
// Check if the intersection point is within the line segments | ||
if t1 >= 0 && t1 <= 1 && t2 >= 0 && t2 <= 1 { | ||
intersectionX := a.X + t1*p1.X | ||
intersectionY := a.Y + t1*p1.Y | ||
return true, Point{intersectionX, intersectionY} | ||
} | ||
|
||
return false, Point{} // The intersection point is outside the line segments | ||
} | ||
|
||
func IsValidTriangle(a, b, c int) bool { | ||
return a+b > c && a+c > b && b+c > a | ||
return a+b > c && a+c > b && b+c > a | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,40 @@ | ||
package math | ||
|
||
import ( | ||
"testing" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCalDistancePoints(t *testing.T) { | ||
assert.Equal(t, "5", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: 5, Y: 0})) | ||
assert.Equal(t, "5", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: 0, Y: 5})) | ||
assert.Equal(t, "5", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: -5, Y: 0})) | ||
assert.Equal(t, "5", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: 0, Y: -5})) | ||
assert.Equal(t, "10", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: 5, Y: 5})) | ||
assert.Equal(t, "10", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: -5, Y: 5})) | ||
assert.Equal(t, "10", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: 5, Y: -5})) | ||
assert.Equal(t, "10", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: -5, Y: -5})) | ||
assert.Equal(t, "5", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: 5, Y: 0})) | ||
assert.Equal(t, "5", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: 0, Y: 5})) | ||
assert.Equal(t, "5", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: -5, Y: 0})) | ||
assert.Equal(t, "5", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: 0, Y: -5})) | ||
assert.Equal(t, "10", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: 5, Y: 5})) | ||
assert.Equal(t, "10", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: -5, Y: 5})) | ||
assert.Equal(t, "10", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: 5, Y: -5})) | ||
assert.Equal(t, "10", CalDistancePoints(Point{X: 0, Y: 0}, Point{X: -5, Y: -5})) | ||
} | ||
|
||
func TestCalIntersectionPoint(t *testing.T) { | ||
intersect, value := CalIntersectionPoint(Point{X: 0, Y: 0}, Point{X: 8, Y: 0}, Point{X: 4, Y: -4}, Point{X: 4, Y: 4}) | ||
assert.Equal(t, true, intersect) | ||
assert.Equal(t, Point{4,0}, value) | ||
|
||
intersect, value = CalIntersectionPoint(Point{X: 4, Y: -4}, Point{X: 4, Y: 4}, Point{X: 0, Y: 0}, Point{X: 8, Y: 0}) | ||
assert.Equal(t, true, intersect) | ||
assert.Equal(t, Point{4,0}, value) | ||
intersect, value = CalIntersectionPoint(Point{X: 0, Y: -8}, Point{X: 0, Y: -1}, Point{X: 0, Y: 0}, Point{X: 8, Y: 0}) | ||
assert.Equal(t, false, intersect) | ||
intersect, value := CalIntersectionPoint(Point{X: 0, Y: 0}, Point{X: 8, Y: 0}, Point{X: 4, Y: -4}, Point{X: 4, Y: 4}) | ||
assert.Equal(t, true, intersect) | ||
assert.Equal(t, Point{4, 0}, value) | ||
|
||
intersect, value = CalIntersectionPoint(Point{X: 4, Y: -4}, Point{X: 4, Y: 4}, Point{X: 0, Y: 0}, Point{X: 8, Y: 0}) | ||
assert.Equal(t, true, intersect) | ||
assert.Equal(t, Point{4, 0}, value) | ||
|
||
intersect, value = CalIntersectionPoint(Point{X: 0, Y: -8}, Point{X: 0, Y: -1}, Point{X: 0, Y: 0}, Point{X: 8, Y: 0}) | ||
assert.Equal(t, false, intersect) | ||
} | ||
|
||
func TestTriangleMath(t *testing.T) { | ||
|
||
assert.Equal(t, true, IsValidTriangle(1, 1, 1)) | ||
assert.Equal(t, false, IsValidTriangle(5, 10, 25)) | ||
assert.Equal(t, true, IsValidTriangle(10, 1, 10)) | ||
assert.Equal(t, true, IsValidTriangle(25, 1, 25)) | ||
assert.Equal(t, true, IsValidTriangle(1, 1, 1)) | ||
assert.Equal(t, false, IsValidTriangle(5, 10, 25)) | ||
assert.Equal(t, true, IsValidTriangle(10, 1, 10)) | ||
assert.Equal(t, true, IsValidTriangle(25, 1, 25)) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package puzzle | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
const ( | ||
Unsolved PuzzleStatus = "UNSOLVED" | ||
Solved PuzzleStatus = "SOLVED" | ||
Unreachable PuzzleStatus = "UNREACHABLE" | ||
) | ||
|
||
var ( | ||
ErrInvalidStatus = errors.New("invalid status") | ||
) | ||
|
||
type InvalidPuzzle struct { | ||
appError error | ||
statusErr error | ||
} | ||
|
||
// to implement the error interface we need to implement the Error() method | ||
func (e *InvalidPuzzle) Error() string { | ||
return errors.Join(e.statusErr, e.appError).Error() | ||
} | ||
|
||
func NewError(appError error, statusErr error) *InvalidPuzzle { | ||
return &InvalidPuzzle{ | ||
appError: appError, | ||
statusErr: statusErr, | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package puzzle | ||
|
||
import ( | ||
"testing" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSolve(t *testing.T) { | ||
ROOT_DIR := os.Getenv("PWD") | ||
basicYaml := filepath.Join(ROOT_DIR, "test_data/basic.yaml") | ||
subject, err := NewPuzzleFromCache(basicYaml, []string{}) | ||
|
||
// Basic Yaml has no errors | ||
assert.Nil(t, err) | ||
|
||
// Metada is set correctly | ||
assert.Equal(t, subject.Metadata.Year, "year_basic") | ||
assert.Equal(t, subject.Metadata.Day, "day_basic") | ||
assert.Equal(t, subject.Metadata.Title, "title_basic") | ||
|
||
// Puzzles check | ||
assert.Equal(t, len(subject.Puzzles), 2) | ||
assert.Equal(t, subject.Puzzles[0].Description, "description_1_basic") | ||
assert.Equal(t, subject.Puzzles[0].Answer, "answer_1_basic") | ||
assert.Equal(t, subject.Puzzles[0].Status, Solved) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
metadata: | ||
day: day_basic | ||
year: year_basic | ||
title: title_basic | ||
puzzles: | ||
- description: description_1_basic | ||
answer: answer_1_basic | ||
status: solved | ||
- description: description_2_basic | ||
answer: answer_2_basic | ||
status: unsolved |