2021 - Day 2

This commit is contained in:
Dreaded_X 2021-12-02 21:59:10 +01:00
parent 4410cef1c4
commit 18d3b51e20
6 changed files with 2116 additions and 0 deletions

3
2021/2/1/go.mod Normal file
View File

@ -0,0 +1,3 @@
module AoC/2021/2/1
go 1.17

1000
2021/2/1/input Normal file

File diff suppressed because it is too large Load Diff

55
2021/2/1/main.go Normal file
View File

@ -0,0 +1,55 @@
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"strconv"
"strings"
)
func main() {
horizontal := 0
depth := 0
input, err := os.Open("input")
if err != nil {
panic(err)
}
defer input.Close()
scanner := bufio.NewScanner(input)
for scanner.Scan() {
line := scanner.Text()
re := regexp.MustCompile("[0-9]+")
if !re.MatchString(line) {
panic("Instruction does not contain number")
}
number, err := strconv.Atoi(re.FindString(line))
if err != nil {
panic(err)
}
if strings.HasPrefix(line, "forward") {
horizontal += number
} else if strings.HasPrefix(line, "up") {
depth -= number
} else if strings.HasPrefix(line, "down") {
depth += number
} else {
panic("Unknown instruction")
}
}
fmt.Println(horizontal, depth, horizontal*depth)
if err := scanner.Err(); err != nil {
panic(err)
}
}

3
2021/2/2/go.mod Normal file
View File

@ -0,0 +1,3 @@
module AoC/2021/2/1
go 1.17

1000
2021/2/2/input Normal file

File diff suppressed because it is too large Load Diff

55
2021/2/2/main.go Normal file
View File

@ -0,0 +1,55 @@
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"strconv"
"strings"
)
func main() {
horizontal := 0
depth := 0
aim := 0
input, err := os.Open("input")
if err != nil {
panic(err)
}
defer input.Close()
scanner := bufio.NewScanner(input)
for scanner.Scan() {
line := scanner.Text()
re := regexp.MustCompile("[0-9]+")
if !re.MatchString(line) {
panic("Instruction does not contain number")
}
number, err := strconv.Atoi(re.FindString(line))
if err != nil {
panic(err)
}
if strings.HasPrefix(line, "forward") {
horizontal += number
depth += aim * number
} else if strings.HasPrefix(line, "up") {
aim -= number
} else if strings.HasPrefix(line, "down") {
aim += number
} else {
panic("Unknown instruction")
}
}
fmt.Println(horizontal, depth, horizontal*depth)
if err := scanner.Err(); err != nil {
panic(err)
}
}