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()) } }