Added the first days

This commit is contained in:
Julien Dessaux 2021-12-07 14:26:36 +01:00
parent 7ebc2cd003
commit 6ed18289c8
26 changed files with 6168 additions and 0 deletions

44
2021/02/second.go Normal file
View file

@ -0,0 +1,44 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
func main() {
f, err := os.Open("input")
if err != nil {
log.Fatalf("%+v", err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanLines)
aim := 0
pos := 0
depth := 0
for scanner.Scan() {
elts := strings.Split(scanner.Text(), " ")
i, err := strconv.Atoi(elts[1])
if err != nil {
log.Fatalf("%+v", err)
}
switch elts[0] {
case "forward":
pos += i
depth += i * aim
case "down":
aim += i
case "up":
aim -= i
default:
panic(elts[0])
}
}
fmt.Println(pos * depth)
}