new(go): Solve day 2 second puzzle.
authorsgf <sgf.dma@gmail.com>
Sat, 11 Dec 2021 20:41:38 +0000 (23:41 +0300)
committersgf <sgf.dma@gmail.com>
Sat, 11 Dec 2021 20:41:38 +0000 (23:41 +0300)
day2/main.go
main.go

index eb0ddc3..6e8580e 100644 (file)
@@ -11,65 +11,76 @@ import (
 )
 
 type Position struct {
-    horiz, depth int
+    horiz, depth, aim int
 }
 
 type Move struct {
-    horiz, depth int
+    horiz, depth, aim int
 }
 
-func f1(moves []Move) Position {
-    p := Position{}
-    for _, d := range moves {
-        p.horiz += d.horiz
-        p.depth += d.depth
-    }
-
-    return p
-}
-
-func parseMoves(r io.Reader) (moves []Move, err error) {
+func parseMoves(r io.Reader) ([]Move, error) {
     s := bufio.NewScanner(r)
 
+    moves := make([]Move, 0)
     for s.Scan() {
         m := Move{}
         ws := strings.Split(s.Text(), " ")
         if len(ws) != 2 {
-            err = fmt.Errorf("Wrong move %v\n", ws)
-            return
+            err := fmt.Errorf("Wrong move %v\n", ws)
+            return nil, err
         }
         switch ws[0] {
             case "forward":
-                m.horiz, err = strconv.Atoi(ws[1])
-                if err != nil {
-                    err = fmt.Errorf("Wrong move number %v\n", ws)
-                    return
-                }
+                x, err := strconv.Atoi(ws[1])
+                if err != nil { return nil, err }
+                m.horiz = x
+
             case "up":
-                m.depth, err = strconv.Atoi(ws[1])
-                if err != nil {
-                    err = fmt.Errorf("Wrong move number %v\n", ws)
-                    return
-                }
-                m.depth = 0 - m.depth
+                x, err := strconv.Atoi(ws[1])
+                if err != nil { return nil, err }
+                m.depth = 0 - x
+                m.aim = 0 - x
+
             case "down":
-                m.depth, err = strconv.Atoi(ws[1])
-                if err != nil {
-                    err = fmt.Errorf("Wrong move number %v\n", ws)
-                    return
-                }
+                x, err := strconv.Atoi(ws[1])
+                if err != nil { return nil, err }
+                m.depth = x
+                m.aim = x
+
             default:
-                moves = nil
-                err = fmt.Errorf("Wrong move %v\n", ws)
-                return
+                err := fmt.Errorf("Wrong move %v\n", ws)
+                return nil, err
         }
 
-        fmt.Printf("Move: %#v\n", m)
+        //fmt.Printf("Move: %#v\n", m)
         moves = append(moves, m)
     }
 
-    return
+    return moves, nil
 }
+
+func f1(moves []Move) Position {
+    p := Position{}
+    for _, m := range moves {
+        p.horiz += m.horiz
+        p.depth += m.depth
+    }
+    return p
+}
+
+func f2(moves []Move) Position {
+    p := Position{}
+    for _, m := range moves {
+        p.horiz += m.horiz
+        p.aim += m.aim
+        if m.horiz != 0 {
+            p.depth += p.aim * m.horiz
+        }
+        //fmt.Printf("After %#v result is %#v\n", m, p)
+    }
+    return p
+}
+
 func RunF1(input string) {
     h0, err := os.Open(input)
     if err != nil { return }
@@ -84,3 +95,18 @@ func RunF1(input string) {
 
     fmt.Printf("Answer1: %d (%d, %d)\n", p.horiz * p.depth, p.horiz, p.depth)
 }
+
+func RunF2(input string) {
+    h0, err := os.Open(input)
+    if err != nil { return }
+    defer h0.Close()
+
+    moves, err := parseMoves(h0)
+    if err != nil {
+        fmt.Printf("Error: %v\n", err)
+        return
+    }
+    p := f2(moves)
+
+    fmt.Printf("Answer2: %d (%d, %d)\n", p.horiz * p.depth, p.horiz, p.depth)
+}
diff --git a/main.go b/main.go
index e5a1189..d3e1114 100644 (file)
--- a/main.go
+++ b/main.go
@@ -11,4 +11,5 @@ func main() {
     //day1.RunF2("day1/input.txt")
 
     day2.RunF1("day2/input.txt")
+    day2.RunF2("day2/input.txt")
 }