Created
December 5, 2021 06:37
-
-
Save Nv7-GitHub/cb8837811d44bf67ae4016b94817e89a to your computer and use it in GitHub Desktop.
Advent of Code 2021 Day 2
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
package main | |
import ( | |
_ "embed" | |
"fmt" | |
"strconv" | |
"strings" | |
) | |
//go:embed input.txt | |
var input string | |
func main() { | |
lines := strings.Split(input, "\n") | |
horiz := 0 | |
vert := 0 | |
for _, line := range lines { | |
vals := strings.Split(line, " ") | |
num, err := strconv.Atoi(vals[1]) | |
if err != nil { | |
panic(err) | |
} | |
switch vals[0] { | |
case "forward": | |
horiz += num | |
case "down": | |
vert += num | |
case "up": | |
vert -= num | |
} | |
} | |
fmt.Println(horiz * vert) | |
} |
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
package main | |
import ( | |
_ "embed" | |
"fmt" | |
"strconv" | |
"strings" | |
) | |
//go:embed input.txt | |
var input string | |
func main() { | |
lines := strings.Split(input, "\n") | |
aim := 0 | |
horiz := 0 | |
vert := 0 | |
for _, line := range lines { | |
vals := strings.Split(line, " ") | |
num, err := strconv.Atoi(vals[1]) | |
if err != nil { | |
panic(err) | |
} | |
switch vals[0] { | |
case "forward": | |
horiz += num | |
vert += num * aim | |
case "down": | |
aim += num | |
case "up": | |
aim -= num | |
} | |
} | |
fmt.Println(horiz * vert) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment