-
Notifications
You must be signed in to change notification settings - Fork 0
/
day05.go
116 lines (97 loc) · 2.7 KB
/
day05.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"fmt"
"os"
"strconv"
"strings"
)
type stack []string
type compartment map[int]stack
type step struct {
from, to, count int
}
type procedure []step
func main() {
input, err := os.ReadFile("input.txt")
if err != nil {
os.Exit(1)
}
fmt.Printf("Part 1: %v\n", SolveDay05Part1(string(input)))
fmt.Printf("Part 2: %v\n", SolveDay05Part2(string(input)))
}
// SolveDay05Part1 returns the crate which are in top of each stack
func SolveDay05Part1(input string) string {
c, p := parseInput(input)
for i := range p {
for x := 0; x < p[i].count; x++ {
var item string
c[p[i].from], item = popLatestFromStack(c[p[i].from])
c[p[i].to] = append(c[p[i].to], item)
}
}
var solution string
for i := 1; i <= len(c); i++ {
_, r := popLatestFromStack(c[i])
solution += r
}
return solution
}
// SolveDay05Part2 returns the crate which are in top of each stack
func SolveDay05Part2(input string) string {
c, p := parseInput(input)
for i := range p {
var items stack
c[p[i].from], items = popMultipleFromStack(c[p[i].from], p[i].count)
c[p[i].to] = append(c[p[i].to], items...)
}
var solution string
for i := 1; i <= len(c); i++ {
_, r := popLatestFromStack(c[i])
solution += r
}
return solution
}
// parseInput return a compartment and procedure for the input
func parseInput(input string) (compartment, procedure) {
inputSplit := strings.Split(input, "\n\n")
cargoLines := strings.Split(inputSplit[0], "\n")
procedureLines := strings.Split(inputSplit[1], "\n")
comp := make(compartment)
pro := make(procedure, 0)
for i := range cargoLines[len(cargoLines)-1] {
if cargoLines[len(cargoLines)-1][i] == ' ' {
continue
}
compartmentInt, _ := strconv.Atoi(string(cargoLines[len(cargoLines)-1][i]))
comp[compartmentInt] = make([]string, 0)
for x := len(cargoLines) - 2; x >= 0; x-- {
if cargoLines[x][i] == ' ' {
continue
}
comp[compartmentInt] = append(comp[compartmentInt], string(cargoLines[x][i]))
}
}
for i := range procedureLines {
s := step{}
lineSplit := strings.Split(procedureLines[i], " ")
if len(lineSplit) != 6 {
continue
}
s.count, _ = strconv.Atoi(lineSplit[1])
s.from, _ = strconv.Atoi(lineSplit[3])
s.to, _ = strconv.Atoi(lineSplit[5])
pro = append(pro, s)
}
return comp, pro
}
// popLatestFromStack returns the new stack without the last item and returns last item
func popLatestFromStack(s stack) (stack, string) {
newStack := s[:len(s)-1]
return newStack, s[len(s)-1]
}
// popMultipleFromStack returns the new stack without the count last item and returns last count items
func popMultipleFromStack(s stack, count int) (stack, stack) {
newStack := s[:len(s)-count]
popStack := s[len(s)-count:]
return newStack, popStack
}