add 2021
This commit is contained in:
BIN
2021/02/first
Executable file
BIN
2021/02/first
Executable file
Binary file not shown.
53
2021/02/first.go
Normal file
53
2021/02/first.go
Normal 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
1000
2021/02/input
Normal file
File diff suppressed because it is too large
Load Diff
BIN
2021/02/second
Executable file
BIN
2021/02/second
Executable file
Binary file not shown.
43
2021/02/second.go
Normal file
43
2021/02/second.go
Normal 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
6
2021/02/testinput
Normal file
@ -0,0 +1,6 @@
|
||||
forward 5
|
||||
down 5
|
||||
forward 8
|
||||
up 3
|
||||
down 8
|
||||
forward 2
|
Reference in New Issue
Block a user