commit3e718f70712e0c0650e07de752ab8f8a5b4ea8e3parent108438460de286f5e39fbc26d44b49d3ffb83492Author:bsandro <email@bsandro.tech>Date:Mon, 5 Dec 2022 08:01:55 +0000 Day 05Diffstat:

A | day05/input.txt | | | 514 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |

A | day05/main.go | | | 129 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |

A | day05/sample.txt | | | 9 | +++++++++ |

A | day05/task.txt | | | 109 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |

4 files changed, 761 insertions(+), 0 deletions(-)diff --git a/day05/input.txt b/day05/input.txt@@ -0,0 +1,514 @@ +[V] [B] [F] +[N] [Q] [W] [R] [B] +[F] [D] [S] [B] [L] [P] +[S] [J] [C] [F] [C] [D] [G] +[M] [M] [H] [L] [P] [N] [P] [V] +[P] [L] [D] [C] [T] [Q] [R] [S] [J] +[H] [R] [Q] [S] [V] [R] [V] [Z] [S] +[J] [S] [N] [R] [M] [T] [G] [C] [D] + 1 2 3 4 5 6 7 8 9 + +move 1 from 8 to 4 +move 1 from 7 to 8 +move 1 from 6 to 3 +move 2 from 6 to 5 +move 8 from 5 to 1 +move 5 from 3 to 8 +move 1 from 7 to 8 +move 8 from 1 to 2 +move 3 from 3 to 9 +move 13 from 8 to 7 +move 2 from 1 to 2 +move 1 from 6 to 2 +move 2 from 1 to 7 +move 4 from 4 to 2 +move 10 from 9 to 4 +move 7 from 4 to 1 +move 1 from 6 to 7 +move 2 from 4 to 5 +move 1 from 5 to 2 +move 1 from 5 to 8 +move 3 from 1 to 5 +move 2 from 4 to 6 +move 2 from 6 to 4 +move 2 from 4 to 5 +move 5 from 1 to 5 +move 1 from 9 to 5 +move 1 from 8 to 5 +move 14 from 2 to 6 +move 12 from 7 to 4 +move 4 from 6 to 7 +move 3 from 6 to 4 +move 4 from 4 to 9 +move 2 from 4 to 6 +move 2 from 9 to 3 +move 9 from 4 to 3 +move 2 from 1 to 6 +move 5 from 7 to 3 +move 4 from 7 to 8 +move 2 from 6 to 7 +move 3 from 6 to 7 +move 10 from 5 to 8 +move 8 from 8 to 9 +move 10 from 9 to 7 +move 12 from 7 to 5 +move 1 from 1 to 5 +move 3 from 7 to 2 +move 10 from 3 to 7 +move 6 from 5 to 7 +move 2 from 6 to 1 +move 12 from 2 to 7 +move 3 from 3 to 1 +move 1 from 6 to 5 +move 10 from 5 to 7 +move 3 from 3 to 4 +move 34 from 7 to 1 +move 2 from 6 to 9 +move 1 from 6 to 3 +move 3 from 1 to 3 +move 1 from 7 to 3 +move 3 from 3 to 6 +move 1 from 4 to 3 +move 22 from 1 to 6 +move 2 from 9 to 7 +move 2 from 4 to 9 +move 12 from 6 to 8 +move 1 from 7 to 6 +move 4 from 8 to 2 +move 1 from 7 to 1 +move 6 from 8 to 9 +move 1 from 2 to 5 +move 1 from 2 to 7 +move 13 from 1 to 2 +move 2 from 3 to 1 +move 4 from 7 to 5 +move 3 from 9 to 4 +move 1 from 1 to 8 +move 4 from 5 to 2 +move 12 from 6 to 2 +move 3 from 1 to 3 +move 1 from 4 to 1 +move 9 from 8 to 5 +move 6 from 5 to 7 +move 2 from 4 to 5 +move 5 from 9 to 6 +move 5 from 3 to 7 +move 30 from 2 to 6 +move 11 from 7 to 9 +move 36 from 6 to 3 +move 10 from 9 to 3 +move 1 from 6 to 5 +move 3 from 5 to 2 +move 2 from 5 to 2 +move 28 from 3 to 4 +move 6 from 4 to 1 +move 1 from 2 to 3 +move 2 from 5 to 2 +move 6 from 1 to 7 +move 1 from 1 to 6 +move 6 from 3 to 5 +move 6 from 7 to 2 +move 1 from 6 to 4 +move 2 from 2 to 6 +move 8 from 2 to 1 +move 3 from 2 to 4 +move 2 from 3 to 4 +move 4 from 3 to 4 +move 1 from 6 to 1 +move 2 from 1 to 8 +move 1 from 6 to 4 +move 1 from 9 to 3 +move 2 from 5 to 4 +move 1 from 8 to 7 +move 1 from 7 to 9 +move 1 from 3 to 5 +move 1 from 8 to 6 +move 34 from 4 to 9 +move 13 from 9 to 8 +move 1 from 8 to 2 +move 1 from 2 to 1 +move 4 from 5 to 1 +move 9 from 8 to 7 +move 11 from 1 to 3 +move 1 from 4 to 2 +move 1 from 6 to 7 +move 1 from 9 to 4 +move 1 from 4 to 1 +move 1 from 5 to 3 +move 5 from 7 to 8 +move 1 from 2 to 5 +move 1 from 5 to 1 +move 21 from 9 to 5 +move 19 from 3 to 4 +move 17 from 4 to 6 +move 2 from 8 to 4 +move 2 from 6 to 8 +move 2 from 6 to 9 +move 2 from 7 to 6 +move 1 from 4 to 9 +move 6 from 5 to 6 +move 1 from 9 to 8 +move 8 from 5 to 7 +move 15 from 6 to 2 +move 1 from 9 to 7 +move 2 from 1 to 6 +move 3 from 4 to 7 +move 1 from 1 to 6 +move 3 from 5 to 4 +move 2 from 5 to 6 +move 2 from 4 to 1 +move 13 from 7 to 8 +move 2 from 6 to 4 +move 3 from 2 to 4 +move 2 from 7 to 6 +move 5 from 4 to 6 +move 4 from 2 to 6 +move 1 from 1 to 9 +move 18 from 8 to 3 +move 1 from 4 to 5 +move 1 from 2 to 7 +move 15 from 3 to 1 +move 1 from 5 to 1 +move 3 from 3 to 4 +move 1 from 5 to 4 +move 1 from 5 to 6 +move 1 from 6 to 8 +move 2 from 8 to 2 +move 3 from 1 to 8 +move 6 from 2 to 8 +move 1 from 7 to 6 +move 12 from 8 to 5 +move 2 from 9 to 6 +move 6 from 1 to 5 +move 9 from 5 to 3 +move 1 from 2 to 8 +move 20 from 6 to 9 +move 3 from 6 to 7 +move 1 from 7 to 1 +move 7 from 3 to 4 +move 2 from 7 to 2 +move 1 from 8 to 7 +move 8 from 4 to 1 +move 11 from 1 to 7 +move 10 from 7 to 6 +move 2 from 4 to 9 +move 21 from 9 to 3 +move 6 from 5 to 9 +move 6 from 3 to 2 +move 1 from 4 to 5 +move 1 from 7 to 9 +move 8 from 3 to 2 +move 9 from 2 to 1 +move 14 from 1 to 6 +move 1 from 1 to 7 +move 4 from 3 to 8 +move 3 from 8 to 7 +move 5 from 7 to 4 +move 3 from 6 to 9 +move 2 from 3 to 7 +move 3 from 5 to 6 +move 1 from 5 to 6 +move 2 from 7 to 9 +move 1 from 8 to 3 +move 22 from 6 to 5 +move 3 from 9 to 4 +move 3 from 6 to 1 +move 5 from 4 to 6 +move 9 from 2 to 8 +move 4 from 6 to 1 +move 1 from 3 to 2 +move 1 from 2 to 3 +move 6 from 8 to 1 +move 2 from 4 to 3 +move 10 from 1 to 7 +move 2 from 8 to 7 +move 1 from 9 to 6 +move 4 from 3 to 5 +move 1 from 8 to 3 +move 4 from 9 to 8 +move 1 from 4 to 3 +move 1 from 3 to 8 +move 3 from 7 to 6 +move 1 from 1 to 5 +move 10 from 5 to 9 +move 5 from 6 to 4 +move 5 from 8 to 5 +move 4 from 9 to 8 +move 3 from 3 to 9 +move 2 from 8 to 6 +move 5 from 7 to 5 +move 1 from 4 to 1 +move 1 from 1 to 2 +move 2 from 8 to 6 +move 1 from 2 to 1 +move 1 from 7 to 2 +move 1 from 1 to 5 +move 28 from 5 to 9 +move 3 from 6 to 1 +move 1 from 6 to 9 +move 1 from 2 to 9 +move 2 from 1 to 2 +move 2 from 7 to 5 +move 1 from 7 to 5 +move 1 from 2 to 5 +move 3 from 1 to 9 +move 1 from 5 to 8 +move 15 from 9 to 2 +move 11 from 9 to 4 +move 11 from 4 to 7 +move 2 from 4 to 1 +move 7 from 7 to 8 +move 1 from 1 to 4 +move 20 from 9 to 1 +move 2 from 7 to 8 +move 1 from 4 to 6 +move 1 from 6 to 2 +move 2 from 7 to 5 +move 1 from 9 to 6 +move 1 from 4 to 9 +move 4 from 5 to 2 +move 1 from 6 to 8 +move 1 from 4 to 9 +move 11 from 8 to 3 +move 1 from 1 to 9 +move 1 from 5 to 9 +move 1 from 2 to 6 +move 4 from 9 to 8 +move 4 from 8 to 7 +move 10 from 1 to 6 +move 7 from 1 to 5 +move 8 from 3 to 4 +move 2 from 3 to 5 +move 3 from 7 to 4 +move 1 from 4 to 5 +move 2 from 1 to 6 +move 9 from 2 to 6 +move 1 from 7 to 9 +move 1 from 3 to 2 +move 7 from 4 to 3 +move 3 from 3 to 7 +move 5 from 2 to 3 +move 1 from 1 to 9 +move 2 from 2 to 7 +move 1 from 4 to 6 +move 3 from 5 to 6 +move 4 from 7 to 6 +move 1 from 7 to 4 +move 1 from 4 to 7 +move 1 from 2 to 8 +move 1 from 7 to 1 +move 27 from 6 to 2 +move 1 from 4 to 1 +move 7 from 5 to 7 +move 1 from 4 to 1 +move 1 from 8 to 3 +move 3 from 7 to 3 +move 2 from 1 to 6 +move 2 from 9 to 1 +move 18 from 2 to 1 +move 2 from 7 to 5 +move 12 from 3 to 4 +move 1 from 5 to 6 +move 3 from 6 to 1 +move 24 from 1 to 8 +move 9 from 2 to 4 +move 3 from 2 to 1 +move 2 from 6 to 3 +move 1 from 6 to 9 +move 1 from 5 to 6 +move 1 from 6 to 2 +move 1 from 1 to 7 +move 1 from 2 to 1 +move 1 from 1 to 2 +move 3 from 7 to 2 +move 2 from 1 to 4 +move 8 from 4 to 5 +move 22 from 8 to 1 +move 1 from 8 to 1 +move 13 from 4 to 1 +move 1 from 8 to 5 +move 3 from 3 to 1 +move 1 from 2 to 7 +move 38 from 1 to 6 +move 27 from 6 to 1 +move 2 from 2 to 9 +move 3 from 9 to 8 +move 2 from 8 to 6 +move 1 from 8 to 3 +move 1 from 2 to 1 +move 1 from 3 to 6 +move 1 from 2 to 3 +move 1 from 7 to 6 +move 7 from 6 to 3 +move 20 from 1 to 4 +move 6 from 1 to 6 +move 17 from 4 to 7 +move 3 from 6 to 5 +move 14 from 7 to 9 +move 8 from 5 to 7 +move 3 from 1 to 6 +move 3 from 3 to 1 +move 2 from 4 to 1 +move 4 from 5 to 1 +move 9 from 6 to 2 +move 3 from 6 to 4 +move 4 from 7 to 8 +move 4 from 1 to 6 +move 2 from 3 to 1 +move 6 from 6 to 7 +move 4 from 8 to 7 +move 4 from 2 to 1 +move 4 from 2 to 3 +move 4 from 9 to 5 +move 8 from 9 to 5 +move 1 from 9 to 5 +move 1 from 2 to 1 +move 16 from 7 to 2 +move 10 from 2 to 9 +move 11 from 9 to 8 +move 4 from 3 to 5 +move 3 from 1 to 4 +move 13 from 5 to 7 +move 10 from 8 to 5 +move 2 from 1 to 5 +move 11 from 7 to 4 +move 2 from 3 to 6 +move 3 from 7 to 6 +move 1 from 3 to 2 +move 1 from 1 to 8 +move 2 from 8 to 4 +move 3 from 1 to 2 +move 4 from 6 to 1 +move 7 from 1 to 9 +move 1 from 6 to 7 +move 2 from 5 to 8 +move 1 from 2 to 9 +move 1 from 7 to 8 +move 5 from 5 to 8 +move 1 from 2 to 3 +move 4 from 2 to 5 +move 17 from 4 to 1 +move 10 from 5 to 9 +move 2 from 4 to 2 +move 2 from 4 to 1 +move 1 from 4 to 9 +move 1 from 3 to 7 +move 1 from 7 to 8 +move 12 from 9 to 2 +move 1 from 2 to 4 +move 1 from 4 to 1 +move 1 from 1 to 9 +move 1 from 8 to 1 +move 8 from 8 to 3 +move 2 from 5 to 1 +move 3 from 1 to 9 +move 1 from 2 to 6 +move 4 from 3 to 7 +move 1 from 7 to 6 +move 10 from 9 to 2 +move 1 from 5 to 9 +move 1 from 9 to 3 +move 17 from 1 to 6 +move 2 from 1 to 2 +move 11 from 6 to 7 +move 2 from 2 to 9 +move 2 from 9 to 5 +move 12 from 7 to 9 +move 20 from 2 to 7 +move 5 from 9 to 5 +move 21 from 7 to 1 +move 2 from 6 to 4 +move 11 from 1 to 4 +move 5 from 4 to 6 +move 1 from 7 to 8 +move 5 from 9 to 3 +move 5 from 2 to 8 +move 3 from 9 to 3 +move 2 from 8 to 7 +move 2 from 1 to 7 +move 10 from 6 to 3 +move 1 from 2 to 6 +move 2 from 8 to 5 +move 1 from 6 to 5 +move 2 from 4 to 9 +move 1 from 4 to 5 +move 8 from 1 to 6 +move 4 from 4 to 8 +move 6 from 8 to 4 +move 21 from 3 to 9 +move 5 from 9 to 2 +move 4 from 7 to 9 +move 22 from 9 to 3 +move 9 from 6 to 4 +move 2 from 2 to 6 +move 2 from 2 to 1 +move 2 from 5 to 7 +move 7 from 5 to 4 +move 22 from 4 to 2 +move 2 from 5 to 4 +move 16 from 2 to 5 +move 2 from 6 to 2 +move 13 from 3 to 4 +move 5 from 5 to 7 +move 15 from 4 to 7 +move 3 from 2 to 3 +move 3 from 2 to 5 +move 1 from 1 to 2 +move 1 from 2 to 4 +move 6 from 5 to 9 +move 4 from 3 to 6 +move 2 from 5 to 9 +move 1 from 2 to 7 +move 1 from 1 to 9 +move 2 from 4 to 5 +move 19 from 7 to 8 +move 1 from 6 to 5 +move 1 from 5 to 1 +move 1 from 9 to 4 +move 5 from 8 to 1 +move 3 from 8 to 1 +move 7 from 5 to 6 +move 3 from 7 to 1 +move 1 from 2 to 5 +move 4 from 9 to 8 +move 2 from 5 to 6 +move 10 from 1 to 4 +move 1 from 7 to 2 +move 6 from 3 to 4 +move 9 from 4 to 3 +move 2 from 2 to 8 +move 2 from 9 to 5 +move 5 from 8 to 3 +move 1 from 1 to 5 +move 2 from 5 to 6 +move 1 from 1 to 7 +move 2 from 9 to 7 +move 8 from 4 to 7 +move 3 from 3 to 9 +move 4 from 6 to 3 +move 1 from 5 to 3 +move 1 from 7 to 2 +move 1 from 2 to 1 +move 1 from 6 to 5 +move 1 from 5 to 2 +move 10 from 7 to 4 +move 10 from 4 to 1 +move 10 from 1 to 8 +move 1 from 9 to 6 +move 1 from 1 to 4 +move 11 from 8 to 1 +move 2 from 9 to 5 +move 5 from 6 to 3 +move 1 from 3 to 8 +move 4 from 1 to 3 +move 5 from 3 to 8 +move 1 from 4 to 7 +move 1 from 7 to 2 +move 13 from 3 to 5 +move 2 from 2 to 1 +move 4 from 3 to 1 +move 4 from 5 to 6 +move 3 from 6 to 2 +move 4 from 5 to 4 +move 8 from 8 to 7 +move 1 from 3 to 9diff --git a/day05/main.go b/day05/main.go@@ -0,0 +1,129 @@ +package main + +import ( + "fmt" + "log" + "os" + "strings" +) + +type Instruction struct { + amount, from, to int +} + +func main() { + if len(os.Args) > 1 { + day05(os.Args[1]) + } else if len(os.Args) == 1 { + fmt.Printf("usage: %s inputfile.txt\n", os.Args[0]) + } else { + fmt.Println("No input data") + } +} + +func day05(input_file string) { + fmt.Printf("day 05 input filename: %s\n", input_file) + input, err := os.ReadFile(input_file) + if err != nil { + log.Fatal(err) + } + crates_in, instructions_in, _ := strings.Cut(string(input), "\n\n") + stacks := parseStacks(crates_in) + instructions := parseInstructions(instructions_in) + + stacks1 := execInstructions1(stacks, instructions) + stacks2 := execInstructions2(stacks, instructions) + + printTopContainers(stacks1) + printTopContainers(stacks2) +} + +func cloneStacks(in [][]string) [][]string { + var out [][]string + for i, _ := range in { + out = append(out, nil) + out[i] = append(out[i], in[i]...) + } + return out +} + +func execInstructions1(stacks [][]string, instructions []Instruction) [][]string { + var out [][]string = cloneStacks(stacks) + for _, in := range instructions { + for i := 0; i < in.amount; i++ { + c := len(out[in.from]) - 1 + out[in.to] = append(out[in.to], out[in.from][c]) + out[in.from] = out[in.from][:c] + } + } + return out +} + +func execInstructions2(stacks [][]string, instructions []Instruction) [][]string { + var out [][]string = cloneStacks(stacks) + for _, in := range instructions { + t := len(out[in.from]) + f := t - in.amount + out[in.to] = append(out[in.to], out[in.from][f:t]...) + out[in.from] = out[in.from][:f] + } + return out +} + +func printTopContainers(stacks [][]string) { + for _, v := range stacks { + fmt.Print(v[len(v)-1]) + } + fmt.Print("\n") +} + +func parseStacks(crates_in string) [][]string { + var stacks [][]string + crates := reverseSlice(strings.Split(crates_in, "\n")) + + // parse stack numbers/amount, line 1 + stacks_ids := strings.Fields(crates[0]) + for i := 0; i < len(stacks_ids); i++ { + stacks = append(stacks, nil) + } + + // parse containers by iterating over symbols in strings + for i := 1; i < len(crates); i++ { + line := crates[i] + for j := 1; j < len(line); j++ { + if isLetter(rune(line[j])) { + stack := j / 4 + stacks[stack] = append(stacks[stack], string(line[j])) + } + } + } + return stacks +} + +func parseInstructions(text string) []Instruction { + var instructions []Instruction + lines := strings.Split(text, "\n") + for _, line := range lines { + var ins Instruction + parsed, err := fmt.Sscanf(line, "move %d from %d to %d", &ins.amount, &ins.from, &ins.to) + if parsed == 3 || err == nil { + // array correction + ins.from -= 1 + ins.to -= 1 + instructions = append(instructions, ins) + } + } + return instructions +} + +func reverseSlice(in []string) []string { + var res []string + for i := len(in) - 1; i >= 0; i-- { + res = append(res, in[i]) + } + return res +} + +func isLetter(in rune) bool { + return (in >= 'a' && in <= 'z') || (in >= 'A' && in <= 'Z') +}diff --git a/day05/sample.txt b/day05/sample.txt@@ -0,0 +1,9 @@ + [D] +[N] [C] +[Z] [M] [P] + 1 2 3 + +move 1 from 2 to 1 +move 3 from 1 to 3 +move 2 from 2 to 1 +move 1 from 1 to 2diff --git a/day05/task.txt b/day05/task.txt@@ -0,0 +1,109 @@ +--- Day 5: Supply Stacks --- + +The expedition can depart as soon as the final supplies have been unloaded from the ships. Supplies are stored in stacks of marked crates, but because the needed supplies are buried under many other crates, the crates need to be rearranged. + +The ship has a giant cargo crane capable of moving crates between stacks. To ensure none of the crates get crushed or fall over, the crane operator will rearrange them in a series of carefully-planned steps. After the crates are rearranged, the desired crates will be at the top of each stack. + +The Elves don't want to interrupt the crane operator during this delicate procedure, but they forgot to ask her which crate will end up where, and they want to be ready to unload them as soon as possible so they can embark. + +They do, however, have a drawing of the starting stacks of crates and the rearrangement procedure (your puzzle input). For example: + + [D] +[N] [C] +[Z] [M] [P] + 1 2 3 + +move 1 from 2 to 1 +move 3 from 1 to 3 +move 2 from 2 to 1 +move 1 from 1 to 2 + +In this example, there are three stacks of crates. Stack 1 contains two crates: crate Z is on the bottom, and crate N is on top. Stack 2 contains three crates; from bottom to top, they are crates M, C, and D. Finally, stack 3 contains a single crate, P. + +Then, the rearrangement procedure is given. In each step of the procedure, a quantity of crates is moved from one stack to a different stack. In the first step of the above rearrangement procedure, one crate is moved from stack 2 to stack 1, resulting in this configuration: + +[D] +[N] [C] +[Z] [M] [P] + 1 2 3 + +In the second step, three crates are moved from stack 1 to stack 3. Crates are moved one at a time, so the first crate to be moved (D) ends up below the second and third crates: + + [Z] + [N] + [C] [D] + [M] [P] + 1 2 3 + +Then, both crates are moved from stack 2 to stack 1. Again, because crates are moved one at a time, crate C ends up below crate M: + + [Z] + [N] +[M] [D] +[C] [P] + 1 2 3 + +Finally, one crate is moved from stack 1 to stack 2: + + [Z] + [N] + [D] +[C] [M] [P] + 1 2 3 + +The Elves just need to know which crate will end up on top of each stack; in this example, the top crates are C in stack 1, M in stack 2, and Z in stack 3, so you should combine these together and give the Elves the message CMZ. + +After the rearrangement procedure completes, what crate ends up on top of each stack? + +Your puzzle answer was SBPQRSCDF. +--- Part Two --- + +As you watch the crane operator expertly rearrange the crates, you notice the process isn't following your prediction. + +Some mud was covering the writing on the side of the crane, and you quickly wipe it away. The crane isn't a CrateMover 9000 - it's a CrateMover 9001. + +The CrateMover 9001 is notable for many new and exciting features: air conditioning, leather seats, an extra cup holder, and the ability to pick up and move multiple crates at once. + +Again considering the example above, the crates begin in the same configuration: + + [D] +[N] [C] +[Z] [M] [P] + 1 2 3 + +Moving a single crate from stack 2 to stack 1 behaves the same as before: + +[D] +[N] [C] +[Z] [M] [P] + 1 2 3 + +However, the action of moving three crates from stack 1 to stack 3 means that those three moved crates stay in the same order, resulting in this new configuration: + + [D] + [N] + [C] [Z] + [M] [P] + 1 2 3 + +Next, as both crates are moved from stack 2 to stack 1, they retain their order as well: + + [D] + [N] +[C] [Z] +[M] [P] + 1 2 3 + +Finally, a single crate is still moved from stack 1 to stack 2, but now it's crate C that gets moved: + + [D] + [N] + [Z] +[M] [C] [P] + 1 2 3 + +In this example, the CrateMover 9001 has put the crates in a totally different order: MCD. + +Before the rearrangement process finishes, update your simulation so that the Elves know where they should stand to be ready to unload the final supplies. After the rearrangement procedure completes, what crate ends up on top of each stack? + +Your puzzle answer was RGLVRCQSB.