75 lines
1.3 KiB
Go
75 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
)
|
|
|
|
func isLetter(c byte) bool {
|
|
return c >= 'A' && c <= 'Z'
|
|
}
|
|
|
|
func isEmpty(s string, i int) bool {
|
|
if i+3 > len(s) || i+2 > len(s) || i+1 > len(s) {
|
|
return false
|
|
}
|
|
return s[i:i+3] == " "
|
|
}
|
|
|
|
func prepend(s []string, e []string) []string {
|
|
s = append(s, e...)
|
|
copy(s[len(e):], s)
|
|
copy(s, e)
|
|
return s
|
|
}
|
|
|
|
func move(columns [][]string, line string) [][]string {
|
|
var quantity int
|
|
var from int
|
|
var to int
|
|
fmt.Sscanf(line, "move %d from %d to %d", &quantity, &from, &to)
|
|
from--
|
|
to--
|
|
columns[to] = prepend(columns[to], columns[from][0:quantity])
|
|
columns[from] = columns[from][quantity:]
|
|
return columns
|
|
}
|
|
|
|
func main() {
|
|
file, err := os.Open("./input.txt")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer file.Close()
|
|
scanner := bufio.NewScanner(file)
|
|
var columns [][]string
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
var c int = 0
|
|
if columns == nil {
|
|
columns = make([][]string, (len(line) + 1) / 4)
|
|
}
|
|
for i := 0; i < len(line); i++ {
|
|
if line[i] == '[' {
|
|
i++
|
|
if isLetter(line[i]) {
|
|
columns[c] = append(columns[c], string(line[i]))
|
|
|
|
}
|
|
c++
|
|
} else if isEmpty(line, i) {
|
|
i += 3
|
|
c++
|
|
}
|
|
}
|
|
if len(line) > 4 && line[0:4] == "move" {
|
|
move(columns, line)
|
|
}
|
|
}
|
|
for i := 0; i < len(columns); i++ {
|
|
fmt.Print(columns[i][0])
|
|
}
|
|
fmt.Println()
|
|
} |