Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "strconv"
- "strings"
- )
- type Stack []string
- // used to create Stacks
- func (st *Stack) Push(str string) {
- if str == " " {
- return
- }
- *st = append(*st, str)
- }
- // used in actual moving of the data between stacks
- func (st *Stack) MovePush(str string) {
- s := []string{str}
- *st = append(s, *st...)
- }
- func (st *Stack) Pop() string {
- length := len(*st)
- if length == 0 {
- return ""
- }
- str := (*st)[0]
- *st = (*st)[1:]
- return str
- }
- func parseInput(d []byte) []string {
- strs := strings.Split(string(d), "\n")
- strs = strs[:len(strs)-1]
- return strs
- }
- // helper function to neatly seprate each coloum into
- // individual strings
- func createSubArrarys(data []string) (newData [][]string) {
- newData = make([][]string, len(data))
- for i := 0; i < len(data); i++ {
- for j := 0; j < len(data[i]); j += 4 {
- newData[i] = append(newData[i], data[i][j:j+3])
- }
- }
- return
- }
- // create arry of integers from instrcution sentences
- // since all sentences have the same form
- // it is safe to parse line by line
- func creatInstArray(d []byte) [][]int {
- strs := strings.Split(string(d), "\n")
- ints := make([][]int, len(strs))
- for idx, line := range strs {
- substrs := strings.Split(line, " ")
- for _, word := range substrs {
- converted, err := strconv.Atoi(word)
- if err == nil {
- ints[idx] = append(ints[idx], converted)
- }
- }
- }
- return ints
- }
- func createStacks(data [][]string) []Stack {
- stacks := make([]Stack, 9)
- for _, line := range data {
- for i := range line {
- stacks[i].Push(line[i])
- }
- }
- return stacks
- }
- func move(stacks []*Stack, insts []int) {
- for i := 1; i <= insts[0]; i++ {
- temp := stacks[insts[1]-1].Pop()
- stacks[insts[2]-1].MovePush(temp)
- }
- }
- func getTops(stacks []Stack, instructions [][]int) string {
- ptrStacks := []*Stack{}
- for i := 0; i < len(stacks); i++ {
- ptrStacks = append(ptrStacks, &stacks[i])
- }
- builder := strings.Builder{}
- replacer := strings.NewReplacer("[", "", "]", "")
- for _, i := range instructions {
- move(ptrStacks, i)
- }
- for _, i := range ptrStacks {
- builder.WriteString(replacer.Replace(i.Pop()))
- }
- return builder.String()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement