Advertisement
SetKaung

AdventOfCodeDay5Part1

Sep 26th, 2023 (edited)
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.18 KB | Source Code | 0 0
  1. package main
  2.  
  3. import (
  4.     "strconv"
  5.     "strings"
  6. )
  7.  
  8. type Stack []string
  9.  
  10. // used to create Stacks
  11. func (st *Stack) Push(str string) {
  12.     if str == "   " {
  13.         return
  14.     }
  15.     *st = append(*st, str)
  16. }
  17.  
  18. // used in actual moving of the data between stacks
  19. func (st *Stack) MovePush(str string) {
  20.     s := []string{str}
  21.     *st = append(s, *st...)
  22. }
  23.  
  24. func (st *Stack) Pop() string {
  25.     length := len(*st)
  26.     if length == 0 {
  27.         return ""
  28.     }
  29.     str := (*st)[0]
  30.     *st = (*st)[1:]
  31.     return str
  32. }
  33.  
  34. func parseInput(d []byte) []string {
  35.     strs := strings.Split(string(d), "\n")
  36.     strs = strs[:len(strs)-1]
  37.     return strs
  38. }
  39.  
  40. // helper function to neatly seprate each coloum into
  41. // individual strings
  42. func createSubArrarys(data []string) (newData [][]string) {
  43.     newData = make([][]string, len(data))
  44.     for i := 0; i < len(data); i++ {
  45.         for j := 0; j < len(data[i]); j += 4 {
  46.             newData[i] = append(newData[i], data[i][j:j+3])
  47.         }
  48.     }
  49.     return
  50. }
  51.  
  52. // create arry of integers from instrcution sentences
  53. // since all sentences have the same form
  54. // it is safe to parse line by line
  55. func creatInstArray(d []byte) [][]int {
  56.     strs := strings.Split(string(d), "\n")
  57.     ints := make([][]int, len(strs))
  58.     for idx, line := range strs {
  59.         substrs := strings.Split(line, " ")
  60.         for _, word := range substrs {
  61.             converted, err := strconv.Atoi(word)
  62.             if err == nil {
  63.                 ints[idx] = append(ints[idx], converted)
  64.             }
  65.  
  66.         }
  67.     }
  68.     return ints
  69. }
  70.  
  71. func createStacks(data [][]string) []Stack {
  72.     stacks := make([]Stack, 9)
  73.     for _, line := range data {
  74.         for i := range line {
  75.             stacks[i].Push(line[i])
  76.         }
  77.     }
  78.  
  79.     return stacks
  80. }
  81.  
  82.  
  83. func move(stacks []*Stack, insts []int) {
  84.     for i := 1; i <= insts[0]; i++ {
  85.         temp := stacks[insts[1]-1].Pop()
  86.         stacks[insts[2]-1].MovePush(temp)
  87.     }
  88. }
  89. func getTops(stacks []Stack, instructions [][]int) string {
  90.     ptrStacks := []*Stack{}
  91.     for i := 0; i < len(stacks); i++ {
  92.         ptrStacks = append(ptrStacks, &stacks[i])
  93.     }
  94.     builder := strings.Builder{}
  95.     replacer := strings.NewReplacer("[", "", "]", "")
  96.     for _, i := range instructions {
  97.         move(ptrStacks, i)
  98.     }
  99.     for _, i := range ptrStacks {
  100.         builder.WriteString(replacer.Replace(i.Pop()))
  101.     }
  102.     return builder.String()
  103. }
  104.  
Tags: aoc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement