From 462d12f50287df9ed4b195036f106d01bbcd843b Mon Sep 17 00:00:00 2001 From: Mikayla Dobson Date: Sun, 16 Jul 2023 11:44:52 -0500 Subject: [PATCH] day five --- dayfive.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 16 +++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 dayfive.go diff --git a/dayfive.go b/dayfive.go new file mode 100644 index 0000000..91da83b --- /dev/null +++ b/dayfive.go @@ -0,0 +1,58 @@ +package main + +import "fmt" + +// define stack +type Stack[T any] struct { + elements []T +} + +// add methods +func (s *Stack[T]) Push(elements ...T) { + s.elements = append(s.elements, elements...) +} + +func (s *Stack[T]) Pop() T { + top := s.elements[len(s.elements)-1] + s.elements = s.elements[:len(s.elements)-1] + return top +} + +func (s *Stack[T]) Peek() T { + return s.elements[len(s.elements)-1] +} + +func dayFive(instructions [4][3]int) [3]rune { + // initialize variables + stackOne := Stack[rune]{} + stackOne.Push('Z', 'N') + + stackTwo := Stack[rune]{} + stackTwo.Push('M', 'C', 'D') + + stackThree := Stack[rune]{} + stackThree.Push('P') + + stacks := [3]*Stack[rune]{&stackOne, &stackTwo, &stackThree} + + // initial state + fmt.Println(*stacks[0], *stacks[1], *stacks[2]) + + // perform each instruction + for _, instruction := range instructions { + sourceStack := stacks[instruction[1]-1] + targetStack := stacks[instruction[2]-1] + numToMove := instruction[0] + + for i := 0; i < numToMove; i++ { + // remove the thing before attempting to move it + thingToMove := sourceStack.Pop() + targetStack.Push(thingToMove) + } + + // log after each step + fmt.Println(*stacks[0], *stacks[1], *stacks[2]) + } + + return [3]rune{stackOne.Peek(), stackTwo.Peek(), stackThree.Peek()} +} diff --git a/main.go b/main.go index 9cae855..72daa8c 100644 --- a/main.go +++ b/main.go @@ -86,4 +86,20 @@ func main() { var dayFourResult = dayFour(dayFourInput) fmt.Println(dayFourResult) + + + /** DAY FIVE */ + fmt.Println("\nDAY FIVE") + + dayFiveInput := [4][3]int{ + {1, 2, 1}, + {3, 1, 3}, + {2, 2, 1}, + {1, 1, 2}, + } + + var dayFiveResult = dayFive(dayFiveInput) + for _, r := range dayFiveResult { + fmt.Printf("%c ", r) + } }