Browse Source

Able to plot

Piotr Czajkowski 1 year ago
parent
commit
1b721fdefd
1 changed files with 43 additions and 1 deletions
  1. 43 1
      18/code.go

+ 43 - 1
18/code.go

@@ -37,6 +37,48 @@ func readInput(file *os.File) []Dig {
 	return plan
 }
 
+type Point struct {
+	y, x int
+}
+
+const (
+	Up    = "U"
+	Down  = "D"
+	Left  = "L"
+	Right = "R"
+)
+
+func (p *Point) getPoints(dig Dig) []Point {
+	var points []Point
+	for i := 0; i < dig.length; i++ {
+		switch dig.direction {
+		case Up:
+			p.y--
+		case Down:
+			p.y++
+		case Left:
+			p.x--
+		case Right:
+			p.x++
+		}
+
+		points = append(points, *p)
+	}
+
+	return points
+}
+
+func plot(plan []Dig) []Point {
+	var current Point
+	result := []Point{current}
+
+	for i := range plan {
+		result = append(result, current.getPoints(plan[i])...)
+	}
+
+	return result
+}
+
 func main() {
 	if len(os.Args) < 2 {
 		log.Fatal("You need to specify a file!")
@@ -50,5 +92,5 @@ func main() {
 	}
 
 	plan := readInput(file)
-	fmt.Println(plan)
+	fmt.Println(plot(plan))
 }