-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday5.go
61 lines (52 loc) · 1.26 KB
/
day5.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
package main
import (
"fmt"
"os"
"strconv"
"strings"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
crates := [][]string{
{},
{"S", "Z", "P", "D", "L", "B", "F", "C"},
{"N", "V", "G", "P", "H", "W", "B"},
{"F", "W", "B", "J", "G"},
{"G", "J", "N", "F", "L", "W", "C", "S"},
{"W", "J", "L", "T", "P", "M", "S", "H"},
{"B", "C", "W", "G", "F", "S"},
{"H", "T", "P", "M", "Q", "B", "W"},
{"F", "S", "W", "T"},
{"N", "C", "R"},
}
input, err := os.ReadFile("./input/input5.txt")
check(err)
lines := strings.Split(string(input), "\r\n")
for _, line := range lines {
instructions := strings.Split(line, " ")
moves, _ := strconv.Atoi(instructions[1])
from, _ := strconv.Atoi(instructions[3])
to, _ := strconv.Atoi(instructions[5])
if false { // part 1
for i := 0; i < moves; i++ {
pop := len(crates[from]) - 1
crates[to] = append(crates[to], crates[from][pop])
crates[from] = crates[from][:pop]
}
} else { // part 2
pop := len(crates[from])
crates[to] = append(crates[to], crates[from][pop-moves:pop]...)
crates[from] = crates[from][:pop-moves]
}
}
result := ""
for i := 1; i < len(crates); i++ {
pop := len(crates[i]) - 1
result += crates[i][pop]
}
fmt.Println("solution: ", result)
}