This commit is contained in:
2023-11-30 22:36:04 +01:00
parent 3fa5a03fb2
commit b8bacd133b
75 changed files with 8610 additions and 0 deletions

BIN
2021/02/first Executable file

Binary file not shown.

53
2021/02/first.go Normal file
View File

@ -0,0 +1,53 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
"strconv"
)
var vertical int = 0
var horizontal int = 0
func checkErr(e error){
if e != nil{
log.Fatal(e)
panic(e)
}
}
func main(){
f, err:=os.Open("input")
checkErr(err)
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan(){
evalCommand(scanner.Text())
}
fmt.Println(horizontal*vertical)
}
func evalCommand(c string){
s := strings.Split(c," ")
switch s[0]{
case "forward":
s,err := strconv.Atoi(s[1])
checkErr(err)
horizontal = horizontal+s
case "backward":
s,err := strconv.Atoi(s[1])
checkErr(err)
horizontal = horizontal-s
case "up":
s,err := strconv.Atoi(s[1])
checkErr(err)
vertical=vertical-s
case "down":
s,err := strconv.Atoi(s[1])
checkErr(err)
vertical=vertical+s
}
}

1000
2021/02/input Normal file

File diff suppressed because it is too large Load Diff

BIN
2021/02/second Executable file

Binary file not shown.

43
2021/02/second.go Normal file
View File

@ -0,0 +1,43 @@
package main
import (
"bufio"
"fmt"
"os"
"strings"
"strconv"
)
var vertical int = 0
var horizontal int = 0
var aim int = 0
func checkErr(e error){
if e != nil{
panic(e)
}
}
func main(){
f,e:=os.Open("input")
checkErr(e)
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan(){
evalCommand(scanner.Text())
}
defer fmt.Println(horizontal*vertical)
}
func evalCommand(c string){
s := strings.Split(c," ")
value,e:= strconv.Atoi(s[1])
checkErr(e)
switch s[0]{
case "forward":
horizontal += value
vertical += aim* value
case "up":
aim -= value
case "down":
aim += value
}
}

6
2021/02/testinput Normal file
View File

@ -0,0 +1,6 @@
forward 5
down 5
forward 8
up 3
down 8
forward 2