Archived
2nd: remove redundancy
This commit is contained in:
+7
-3
@@ -21,23 +21,27 @@ func solve(commands []string, alt bool) int {
|
|||||||
aim := 0
|
aim := 0
|
||||||
|
|
||||||
for _, v := range commands {
|
for _, v := range commands {
|
||||||
|
|
||||||
command := strings.Split(v, " ")
|
command := strings.Split(v, " ")
|
||||||
|
|
||||||
|
x, err := strconv.Atoi(command[1])
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
switch command[0] {
|
switch command[0] {
|
||||||
case "forward":
|
case "forward":
|
||||||
x, _ := strconv.Atoi(command[1])
|
|
||||||
horizontalPosition += x
|
horizontalPosition += x
|
||||||
if alt {
|
if alt {
|
||||||
depth += aim * x
|
depth += aim * x
|
||||||
}
|
}
|
||||||
case "up":
|
case "up":
|
||||||
x, _ := strconv.Atoi(command[1])
|
|
||||||
if !alt {
|
if !alt {
|
||||||
depth -= x
|
depth -= x
|
||||||
} else {
|
} else {
|
||||||
aim -= x
|
aim -= x
|
||||||
}
|
}
|
||||||
case "down":
|
case "down":
|
||||||
x, _ := strconv.Atoi(command[1])
|
|
||||||
if !alt {
|
if !alt {
|
||||||
depth += x
|
depth += x
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user