| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- package main
- import "container/list"
- import "strings"
- import "strconv"
- import "bufio"
- import "math"
- import "fmt"
- import "os"
- type Vect struct {
- posX int
- posY int
- head int
- }
- func (this *Vect) addStepCount() {
- switch this.head {
- case 0:
- this.posY++
- break
- case 1:
- this.posX++
- break
- case 2:
- this.posY--
- break;
- case 3:
- this.posX--
- break
- }
- }
- func (this *Vect) addStep(dir string) bool {
- if dir == "R" {
- this.head++;
- for this.head >= 4 { this.head -= 4 }
- } else if dir == "L" {
- this.head--;
- for this.head < 0 { this.head += 4 }
- } else {
- return false
- }
- return true
- }
- func (this Vect) toTaxiCab() int {
- return int(math.Abs(float64(this.posX)) +
- math.Abs(float64(this.posY)))
- }
- func (this Vect) inList(list list.List) bool {
- for e := list.Front(); e != nil; e = e.Next() {
- if e.Value.(Vect).posX == this.posX &&
- e.Value.(Vect).posY == this.posY {
- return true
- }
- }
- return false
- }
- func (this *Vect) relative(other Vect) {
- this.posX -= other.posX
- this.posY -= other.posY
- }
- func dumpList(l list.List) {
- for e := l.Front(); e != nil; e = e.Next() {
- fmt.Println(e.Value.(Vect))
- }
- }
- func main() {
- coords := Vect{}
- reader := bufio.NewReader(os.Stdin)
- var visited list.List
- var visitedTwice Vect
- var hasVisitedTwice bool = false
- for {
- read, err := reader.ReadString(',')
- read = strings.Trim(read, ", \n")
- stepCount, _ := strconv.Atoi(read[1:])
- coords.addStep(read[:1])
- for i := 0; i < stepCount; i++ {
- if !hasVisitedTwice {
- if coords.inList(visited) {
- visitedTwice = coords
- hasVisitedTwice = true
- } else {
- visited.PushBack(Vect(coords))
- }
- }
- coords.addStepCount()
- }
- if err != nil {
- break
- }
- }
- fmt.Println(coords.toTaxiCab())
- if (hasVisitedTwice) {
- fmt.Println(visitedTwice.toTaxiCab())
- }
- }
|