Skip to content

Instantly share code, notes, and snippets.

@Nv7-GitHub
Created December 5, 2021 06:37
Show Gist options
  • Save Nv7-GitHub/cb8837811d44bf67ae4016b94817e89a to your computer and use it in GitHub Desktop.
Save Nv7-GitHub/cb8837811d44bf67ae4016b94817e89a to your computer and use it in GitHub Desktop.
Advent of Code 2021 Day 2
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)
}
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