From 60f835ad4f1bd40cf207805723e34873931843ef Mon Sep 17 00:00:00 2001 From: Enrico Candino Date: Mon, 5 Dec 2022 19:10:11 +0100 Subject: [PATCH 1/2] wip --- 2022/day05/enrichman/input.txt | 512 +++++++++++++++++++++++++++++ 2022/day05/enrichman/main.go | 18 + 2022/day05/enrichman/parts.go | 80 +++++ 2022/day05/enrichman/parts_test.go | 62 ++++ 4 files changed, 672 insertions(+) create mode 100644 2022/day05/enrichman/input.txt create mode 100644 2022/day05/enrichman/main.go create mode 100644 2022/day05/enrichman/parts.go create mode 100644 2022/day05/enrichman/parts_test.go diff --git a/2022/day05/enrichman/input.txt b/2022/day05/enrichman/input.txt new file mode 100644 index 0000000..e34750c --- /dev/null +++ b/2022/day05/enrichman/input.txt @@ -0,0 +1,512 @@ +[N] [G] [Q] +[H] [B] [B] [R] [H] +[S] [N] [Q] [M] [T] [Z] +[J] [T] [R] [V] [H] [R] [S] +[F] [Q] [W] [T] [V] [J] [V] [M] +[W] [P] [V] [S] [F] [B] [Q] [J] [H] +[T] [R] [Q] [B] [D] [D] [B] [N] [N] +[D] [H] [L] [N] [N] [M] [D] [D] [B] + 1 2 3 4 5 6 7 8 9 + +move 3 from 1 to 2 +move 1 from 7 to 1 +move 1 from 6 to 5 +move 5 from 5 to 9 +move 2 from 5 to 2 +move 1 from 6 to 8 +move 1 from 5 to 7 +move 5 from 4 to 6 +move 1 from 7 to 6 +move 1 from 2 to 4 +move 5 from 2 to 6 +move 2 from 1 to 5 +move 2 from 1 to 9 +move 16 from 6 to 4 +move 6 from 8 to 3 +move 7 from 2 to 4 +move 5 from 9 to 3 +move 1 from 1 to 4 +move 1 from 1 to 3 +move 3 from 7 to 4 +move 2 from 5 to 4 +move 31 from 4 to 8 +move 22 from 8 to 4 +move 9 from 3 to 6 +move 7 from 9 to 5 +move 4 from 5 to 6 +move 6 from 3 to 2 +move 2 from 6 to 7 +move 5 from 2 to 7 +move 1 from 2 to 4 +move 1 from 7 to 5 +move 4 from 5 to 4 +move 2 from 6 to 9 +move 2 from 4 to 6 +move 7 from 6 to 4 +move 2 from 6 to 1 +move 1 from 6 to 8 +move 8 from 8 to 1 +move 1 from 7 to 6 +move 4 from 1 to 5 +move 9 from 4 to 8 +move 4 from 1 to 7 +move 3 from 5 to 3 +move 2 from 1 to 9 +move 1 from 3 to 2 +move 1 from 9 to 8 +move 1 from 2 to 1 +move 1 from 1 to 8 +move 1 from 5 to 1 +move 2 from 3 to 1 +move 2 from 6 to 9 +move 19 from 4 to 1 +move 4 from 4 to 2 +move 6 from 1 to 4 +move 1 from 2 to 4 +move 4 from 4 to 3 +move 7 from 7 to 3 +move 7 from 8 to 2 +move 2 from 7 to 4 +move 3 from 2 to 1 +move 8 from 8 to 2 +move 3 from 9 to 1 +move 2 from 9 to 1 +move 10 from 2 to 7 +move 4 from 3 to 1 +move 1 from 8 to 3 +move 1 from 4 to 5 +move 1 from 3 to 6 +move 1 from 2 to 1 +move 10 from 1 to 3 +move 1 from 4 to 7 +move 1 from 6 to 4 +move 7 from 3 to 2 +move 5 from 2 to 8 +move 11 from 7 to 2 +move 3 from 4 to 3 +move 1 from 4 to 3 +move 5 from 8 to 9 +move 17 from 2 to 4 +move 11 from 1 to 5 +move 4 from 1 to 3 +move 5 from 9 to 2 +move 4 from 2 to 1 +move 3 from 5 to 7 +move 6 from 5 to 3 +move 1 from 5 to 8 +move 6 from 1 to 8 +move 3 from 8 to 5 +move 1 from 1 to 4 +move 1 from 7 to 2 +move 15 from 3 to 4 +move 1 from 1 to 3 +move 10 from 3 to 9 +move 2 from 7 to 4 +move 1 from 2 to 8 +move 21 from 4 to 9 +move 1 from 2 to 3 +move 1 from 8 to 1 +move 9 from 4 to 2 +move 1 from 1 to 5 +move 5 from 2 to 7 +move 2 from 8 to 5 +move 1 from 8 to 1 +move 2 from 2 to 8 +move 2 from 4 to 9 +move 24 from 9 to 5 +move 3 from 4 to 1 +move 2 from 2 to 5 +move 12 from 5 to 1 +move 10 from 1 to 5 +move 23 from 5 to 6 +move 8 from 9 to 1 +move 3 from 8 to 1 +move 1 from 1 to 2 +move 1 from 3 to 7 +move 11 from 6 to 1 +move 1 from 2 to 4 +move 6 from 6 to 8 +move 4 from 6 to 7 +move 1 from 7 to 3 +move 1 from 3 to 4 +move 23 from 1 to 8 +move 1 from 4 to 2 +move 1 from 2 to 1 +move 1 from 6 to 7 +move 6 from 5 to 3 +move 1 from 7 to 8 +move 1 from 1 to 8 +move 1 from 9 to 3 +move 6 from 7 to 2 +move 3 from 5 to 9 +move 5 from 2 to 3 +move 28 from 8 to 3 +move 4 from 1 to 9 +move 5 from 9 to 5 +move 2 from 8 to 5 +move 1 from 9 to 4 +move 2 from 7 to 5 +move 1 from 4 to 2 +move 1 from 4 to 8 +move 2 from 8 to 3 +move 6 from 5 to 2 +move 1 from 7 to 2 +move 39 from 3 to 2 +move 2 from 3 to 8 +move 1 from 9 to 6 +move 2 from 2 to 9 +move 2 from 9 to 6 +move 1 from 8 to 1 +move 1 from 1 to 6 +move 5 from 6 to 9 +move 2 from 5 to 8 +move 20 from 2 to 4 +move 2 from 4 to 8 +move 2 from 8 to 3 +move 3 from 3 to 1 +move 22 from 2 to 5 +move 2 from 9 to 1 +move 3 from 1 to 7 +move 1 from 2 to 6 +move 1 from 2 to 9 +move 1 from 1 to 8 +move 2 from 7 to 9 +move 1 from 6 to 8 +move 1 from 2 to 7 +move 1 from 1 to 3 +move 1 from 9 to 8 +move 1 from 8 to 5 +move 3 from 8 to 7 +move 3 from 7 to 8 +move 15 from 4 to 1 +move 1 from 4 to 3 +move 10 from 1 to 6 +move 3 from 8 to 1 +move 5 from 9 to 4 +move 7 from 5 to 1 +move 4 from 6 to 3 +move 15 from 5 to 2 +move 4 from 6 to 4 +move 7 from 2 to 1 +move 6 from 4 to 6 +move 1 from 5 to 9 +move 1 from 5 to 7 +move 1 from 3 to 5 +move 11 from 1 to 8 +move 3 from 4 to 6 +move 4 from 1 to 5 +move 1 from 2 to 5 +move 2 from 8 to 3 +move 11 from 6 to 1 +move 1 from 3 to 7 +move 1 from 9 to 8 +move 6 from 5 to 8 +move 3 from 8 to 4 +move 1 from 4 to 5 +move 3 from 3 to 1 +move 9 from 8 to 2 +move 2 from 1 to 5 +move 11 from 2 to 5 +move 1 from 3 to 6 +move 2 from 8 to 5 +move 3 from 4 to 6 +move 1 from 8 to 3 +move 2 from 1 to 9 +move 1 from 3 to 8 +move 16 from 5 to 7 +move 3 from 1 to 6 +move 1 from 3 to 5 +move 1 from 6 to 7 +move 1 from 9 to 4 +move 1 from 5 to 4 +move 1 from 3 to 2 +move 1 from 1 to 2 +move 3 from 4 to 9 +move 1 from 2 to 7 +move 2 from 8 to 3 +move 6 from 2 to 8 +move 11 from 1 to 3 +move 6 from 3 to 1 +move 4 from 3 to 2 +move 2 from 3 to 1 +move 1 from 1 to 3 +move 4 from 8 to 4 +move 4 from 8 to 2 +move 11 from 7 to 2 +move 9 from 7 to 5 +move 1 from 7 to 3 +move 4 from 5 to 7 +move 14 from 2 to 3 +move 17 from 3 to 7 +move 2 from 5 to 2 +move 1 from 5 to 7 +move 1 from 5 to 6 +move 4 from 6 to 7 +move 8 from 1 to 2 +move 2 from 6 to 4 +move 1 from 6 to 8 +move 6 from 4 to 1 +move 1 from 8 to 5 +move 6 from 7 to 8 +move 5 from 8 to 3 +move 12 from 2 to 1 +move 1 from 8 to 4 +move 4 from 3 to 1 +move 4 from 2 to 4 +move 3 from 9 to 3 +move 3 from 3 to 2 +move 1 from 3 to 2 +move 3 from 4 to 1 +move 2 from 5 to 7 +move 22 from 1 to 8 +move 17 from 8 to 6 +move 21 from 7 to 6 +move 3 from 2 to 8 +move 3 from 1 to 5 +move 3 from 5 to 2 +move 2 from 4 to 6 +move 7 from 6 to 5 +move 1 from 9 to 4 +move 14 from 6 to 4 +move 5 from 8 to 3 +move 1 from 6 to 3 +move 3 from 3 to 9 +move 2 from 9 to 1 +move 2 from 7 to 1 +move 16 from 6 to 8 +move 2 from 6 to 7 +move 1 from 2 to 7 +move 1 from 3 to 8 +move 7 from 4 to 1 +move 2 from 7 to 2 +move 4 from 4 to 7 +move 5 from 2 to 4 +move 1 from 7 to 3 +move 3 from 5 to 8 +move 1 from 7 to 5 +move 12 from 1 to 6 +move 3 from 7 to 2 +move 7 from 4 to 2 +move 3 from 3 to 2 +move 1 from 4 to 2 +move 1 from 9 to 8 +move 8 from 6 to 8 +move 12 from 2 to 4 +move 5 from 5 to 2 +move 11 from 4 to 9 +move 3 from 6 to 3 +move 2 from 4 to 2 +move 4 from 2 to 6 +move 5 from 2 to 8 +move 12 from 8 to 4 +move 20 from 8 to 5 +move 13 from 5 to 3 +move 1 from 8 to 5 +move 5 from 5 to 9 +move 16 from 9 to 1 +move 9 from 4 to 5 +move 12 from 3 to 9 +move 5 from 6 to 5 +move 9 from 9 to 7 +move 14 from 1 to 4 +move 14 from 4 to 1 +move 15 from 5 to 7 +move 4 from 8 to 2 +move 3 from 4 to 3 +move 3 from 1 to 8 +move 1 from 5 to 9 +move 1 from 5 to 3 +move 3 from 9 to 8 +move 4 from 3 to 4 +move 1 from 4 to 6 +move 20 from 7 to 2 +move 2 from 3 to 8 +move 3 from 7 to 2 +move 4 from 2 to 1 +move 1 from 6 to 7 +move 3 from 4 to 2 +move 2 from 2 to 3 +move 4 from 3 to 4 +move 1 from 8 to 1 +move 3 from 8 to 1 +move 2 from 7 to 8 +move 1 from 4 to 5 +move 14 from 2 to 5 +move 6 from 1 to 5 +move 1 from 4 to 3 +move 15 from 1 to 4 +move 1 from 8 to 2 +move 1 from 9 to 5 +move 4 from 8 to 7 +move 13 from 5 to 6 +move 1 from 8 to 1 +move 2 from 7 to 9 +move 12 from 6 to 4 +move 1 from 3 to 6 +move 1 from 1 to 6 +move 4 from 5 to 2 +move 5 from 5 to 6 +move 2 from 6 to 2 +move 1 from 7 to 5 +move 2 from 6 to 9 +move 1 from 5 to 9 +move 16 from 2 to 5 +move 17 from 4 to 1 +move 3 from 1 to 3 +move 1 from 2 to 6 +move 2 from 6 to 1 +move 3 from 3 to 1 +move 14 from 1 to 8 +move 3 from 5 to 2 +move 4 from 8 to 2 +move 3 from 4 to 5 +move 15 from 5 to 3 +move 1 from 7 to 6 +move 3 from 1 to 8 +move 2 from 3 to 7 +move 1 from 1 to 2 +move 1 from 7 to 6 +move 4 from 2 to 8 +move 2 from 6 to 2 +move 1 from 7 to 6 +move 3 from 8 to 2 +move 12 from 8 to 6 +move 1 from 5 to 6 +move 3 from 2 to 5 +move 2 from 2 to 5 +move 4 from 6 to 5 +move 4 from 3 to 5 +move 1 from 8 to 4 +move 11 from 6 to 4 +move 6 from 3 to 1 +move 2 from 9 to 8 +move 20 from 4 to 5 +move 1 from 4 to 9 +move 2 from 3 to 8 +move 1 from 3 to 8 +move 17 from 5 to 8 +move 5 from 5 to 9 +move 9 from 5 to 1 +move 2 from 6 to 7 +move 23 from 8 to 2 +move 2 from 7 to 5 +move 3 from 9 to 4 +move 16 from 2 to 4 +move 11 from 1 to 8 +move 4 from 5 to 8 +move 11 from 2 to 6 +move 2 from 6 to 1 +move 5 from 9 to 5 +move 5 from 5 to 6 +move 5 from 8 to 6 +move 1 from 6 to 7 +move 7 from 8 to 1 +move 12 from 1 to 2 +move 1 from 9 to 5 +move 1 from 1 to 3 +move 1 from 1 to 4 +move 1 from 5 to 3 +move 1 from 3 to 6 +move 1 from 8 to 2 +move 18 from 6 to 2 +move 1 from 6 to 2 +move 2 from 8 to 3 +move 3 from 3 to 8 +move 18 from 4 to 9 +move 11 from 9 to 2 +move 2 from 9 to 6 +move 2 from 4 to 1 +move 1 from 1 to 5 +move 1 from 5 to 4 +move 1 from 4 to 8 +move 42 from 2 to 1 +move 3 from 9 to 3 +move 1 from 8 to 1 +move 1 from 3 to 4 +move 3 from 8 to 7 +move 1 from 4 to 1 +move 2 from 3 to 2 +move 17 from 1 to 6 +move 15 from 6 to 3 +move 2 from 9 to 7 +move 1 from 3 to 6 +move 2 from 7 to 6 +move 2 from 2 to 4 +move 1 from 2 to 3 +move 1 from 4 to 9 +move 1 from 4 to 1 +move 1 from 6 to 3 +move 20 from 1 to 9 +move 6 from 1 to 9 +move 7 from 9 to 3 +move 20 from 9 to 1 +move 1 from 6 to 7 +move 2 from 6 to 7 +move 1 from 6 to 5 +move 1 from 6 to 8 +move 4 from 7 to 3 +move 3 from 7 to 2 +move 1 from 6 to 4 +move 1 from 2 to 1 +move 1 from 4 to 9 +move 21 from 3 to 2 +move 5 from 3 to 8 +move 1 from 5 to 1 +move 2 from 8 to 7 +move 4 from 8 to 3 +move 4 from 2 to 5 +move 19 from 2 to 3 +move 1 from 9 to 2 +move 23 from 3 to 2 +move 2 from 7 to 4 +move 3 from 5 to 9 +move 16 from 2 to 1 +move 1 from 5 to 4 +move 1 from 9 to 3 +move 2 from 3 to 8 +move 3 from 4 to 6 +move 1 from 6 to 2 +move 1 from 8 to 6 +move 5 from 2 to 6 +move 7 from 6 to 5 +move 4 from 2 to 6 +move 6 from 5 to 9 +move 1 from 8 to 4 +move 18 from 1 to 9 +move 1 from 5 to 2 +move 9 from 9 to 4 +move 5 from 6 to 3 +move 9 from 4 to 1 +move 4 from 9 to 2 +move 1 from 4 to 8 +move 1 from 8 to 3 +move 7 from 1 to 8 +move 6 from 3 to 2 +move 10 from 2 to 9 +move 21 from 1 to 8 +move 1 from 2 to 8 +move 19 from 8 to 4 +move 1 from 8 to 3 +move 16 from 4 to 8 +move 1 from 4 to 2 +move 2 from 1 to 5 +move 1 from 2 to 3 +move 1 from 4 to 5 +move 1 from 4 to 8 +move 2 from 1 to 3 +move 3 from 3 to 2 +move 5 from 9 to 1 +move 1 from 3 to 4 +move 4 from 9 to 4 +move 2 from 1 to 9 +move 2 from 2 to 5 +move 1 from 2 to 7 +move 3 from 1 to 7 +move 10 from 8 to 6 +move 4 from 8 to 5 +move 3 from 4 to 3 +move 3 from 3 to 4 +move 1 from 9 to 8 +move 2 from 7 to 2 +move 1 from 2 to 1 +move 4 from 9 to 3 diff --git a/2022/day05/enrichman/main.go b/2022/day05/enrichman/main.go new file mode 100644 index 0000000..ee2136b --- /dev/null +++ b/2022/day05/enrichman/main.go @@ -0,0 +1,18 @@ +package main + +import ( + "fmt" + "os" + "strings" + + "github.com/golangroma/aoc/utils" +) + +func main() { + content, err := os.ReadFile("input.txt") + utils.CheckErr(err) + lines := strings.Split(string(content), "\n") + + fmt.Printf("Part 1: %v\n", PartOne(lines)) + fmt.Printf("Part 2: %v\n", PartTwo(lines)) +} diff --git a/2022/day05/enrichman/parts.go b/2022/day05/enrichman/parts.go new file mode 100644 index 0000000..50a8519 --- /dev/null +++ b/2022/day05/enrichman/parts.go @@ -0,0 +1,80 @@ +package main + +import ( + "fmt" + "strconv" + "strings" + + "github.com/golangroma/aoc/utils" +) + +type Stacks []Stack + +type Stack []Crate + +type Crate string + +func PartOne(input []string) string { + parseInput(input) + return "" +} + +func PartTwo(input []string) string { + return "" +} + +func parseInput(input []string) { + stackInput := []string{} + //arrangementsInput := []string{} + + for i, line := range input { + if line == "" { + stackInput = input[0:i] + //arrangementsInput = input[i+1:] + break + } + } + + //fmt.Println(stackInput, arrangementsInput) + + printDrawing(stackInput) + + var stacks Stacks + for i := len(stackInput) - 1; i >= 0; i-- { + if i == len(stackInput)-1 { + stacksIDs := strings.Fields(stackInput[i]) + lastStackIDStr := stacksIDs[len(stacksIDs)-1] + lastStackID, err := strconv.Atoi(lastStackIDStr) + utils.CheckErr(err) + + stacks = make(Stacks, lastStackID) + continue + } + + cratesLine := strings.Fields(stackInput[i]) + fmt.Println(cratesLine) + for _, crate := range cratesLine { + if stacks[i] == nil { + stacks[i] = Stack{} + } + stack := stacks[i] + stack = append(stack, Crate(crate)) + stacks[i] = stack + } + + fmt.Println(stacks[1]) + } + fmt.Println(len(stacks)) +} + +func printDrawing(drawing []string) { + for i, line := range drawing { + if strings.HasPrefix(line, " ") { + line = strings.Replace(line, " ", "[-] ", 1) + } + line = strings.ReplaceAll(line, " ", " [-]") + + drawing[i] = line + fmt.Println(line) + } +} diff --git a/2022/day05/enrichman/parts_test.go b/2022/day05/enrichman/parts_test.go new file mode 100644 index 0000000..4bba564 --- /dev/null +++ b/2022/day05/enrichman/parts_test.go @@ -0,0 +1,62 @@ +package main + +import ( + "strings" + "testing" +) + +var sample string = ` [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` + +func TestPartOne(t *testing.T) { + tt := []struct { + name string + input string + expected string + }{ + { + name: "example", + input: sample, + expected: "not-yet-implemented", + }, + } + + for _, tc := range tt { + t.Run(tc.name, func(t *testing.T) { + input := strings.Split(tc.input, "\n") + if got := PartOne(input); got != tc.expected { + t.Errorf("PartOne() = %v, want %v", got, tc.expected) + } + }) + } +} + +func TestPartTwo(t *testing.T) { + tt := []struct { + name string + input string + expected string + }{ + { + name: "example", + input: sample, + expected: "not-yet-implemented", + }, + } + + for _, tc := range tt { + t.Run(tc.name, func(t *testing.T) { + input := strings.Split(tc.input, "\n") + if got := PartTwo(input); got != tc.expected { + t.Errorf("PartTwo() = %v, want %v", got, tc.expected) + } + }) + } +} From 92febe5b72bd8d8be5f8cda4bfb098d8f2d26d9c Mon Sep 17 00:00:00 2001 From: Enrico Candino Date: Tue, 6 Dec 2022 13:46:56 +0100 Subject: [PATCH 2/2] solved --- 2022/day05/enrichman/parts.go | 187 +++++++++++++++++++++++++---- 2022/day05/enrichman/parts_test.go | 4 +- 2 files changed, 163 insertions(+), 28 deletions(-) diff --git a/2022/day05/enrichman/parts.go b/2022/day05/enrichman/parts.go index 50a8519..366def8 100644 --- a/2022/day05/enrichman/parts.go +++ b/2022/day05/enrichman/parts.go @@ -8,63 +8,189 @@ import ( "github.com/golangroma/aoc/utils" ) -type Stacks []Stack +type Stack struct { + ID int + Crates []*Crate +} + +func (s *Stack) AddCrate(v string) { + crate := Crate(v) + s.Crates = append(s.Crates, &crate) +} + +func (s *Stack) Pick() *Crate { + top := s.Crates[len(s.Crates)-1] + s.Crates = s.Crates[:len(s.Crates)-1] + return top +} -type Stack []Crate +func (s *Stack) Push(c *Crate) { + s.Crates = append(s.Crates, c) +} + +func (s *Stack) String() string { + var str string + for _, c := range s.Crates { + str += fmt.Sprint(string(*c)) + } + return fmt.Sprintf("Stack{ID: %d, Crates{%s}}", s.ID, str) +} type Crate string +type Arrangement struct { + Moves int + From int + To int +} + +type Crane interface { + Move() +} + +type SimpleCrane struct { + Stacks []*Stack + Arrangements []*Arrangement +} + +func (sc *SimpleCrane) Move() { + for _, arrangement := range sc.Arrangements { + for m := 0; m < arrangement.Moves; m++ { + crate := sc.Stacks[arrangement.From-1].Pick() + sc.Stacks[arrangement.To-1].Push(crate) + } + } +} + +type SuperCrane struct { + Stacks []*Stack + Arrangements []*Arrangement +} + +func (sc *SuperCrane) Move() { + for _, arrangement := range sc.Arrangements { + + ss := sc.Stacks[arrangement.From-1] + ssTo := sc.Stacks[arrangement.To-1] + + fmt.Println(ss, ssTo) + fmt.Printf("%+v\n", arrangement) + + crates := ss.Crates[len(ss.Crates)-arrangement.Moves:] + ss.Crates = ss.Crates[:len(ss.Crates)-arrangement.Moves] + ssTo.Crates = append(ssTo.Crates, crates...) + + fmt.Println(ss, ssTo) + } +} + +func NewArrangement(s string) *Arrangement { + conv := func(val string) int { + i, err := strconv.Atoi(val) + utils.CheckErr(err) + return i + } + + arr := strings.Fields(s) + if len(arr) > 5 { + return &Arrangement{ + Moves: conv(arr[1]), + From: conv(arr[3]), + To: conv(arr[5]), + } + } + return nil +} + func PartOne(input []string) string { - parseInput(input) - return "" + stackInput, arrangementsInput := splitInput(input) + + stacks := buildStack(stackInput) + arrangements := buildArrangements(arrangementsInput) + + crane := &SimpleCrane{ + Stacks: stacks, + Arrangements: arrangements, + } + crane.Move() + + return printTopStacks(crane.Stacks) } func PartTwo(input []string) string { - return "" + stackInput, arrangementsInput := splitInput(input) + + stacks := buildStack(stackInput) + arrangements := buildArrangements(arrangementsInput) + + crane := &SuperCrane{ + Stacks: stacks, + Arrangements: arrangements, + } + crane.Move() + + return printTopStacks(crane.Stacks) } -func parseInput(input []string) { +func splitInput(input []string) ([]string, []string) { stackInput := []string{} - //arrangementsInput := []string{} + arrangementsInput := []string{} for i, line := range input { if line == "" { stackInput = input[0:i] - //arrangementsInput = input[i+1:] + arrangementsInput = input[i+1:] break } } - //fmt.Println(stackInput, arrangementsInput) + return stackInput, arrangementsInput +} +func buildStack(stackInput []string) []*Stack { printDrawing(stackInput) - var stacks Stacks + var stacks []*Stack + + // start from the bottom of the drawing stack for i := len(stackInput) - 1; i >= 0; i-- { - if i == len(stackInput)-1 { - stacksIDs := strings.Fields(stackInput[i]) - lastStackIDStr := stacksIDs[len(stacksIDs)-1] - lastStackID, err := strconv.Atoi(lastStackIDStr) - utils.CheckErr(err) - stacks = make(Stacks, lastStackID) + // first line is the ID + if i == len(stackInput)-1 { + // init stacks + stacksNum := len(strings.Fields(stackInput[i])) + stacks = make([]*Stack, stacksNum) + for s := 0; s < stacksNum; s++ { + stacks[s] = &Stack{ID: s + 1, Crates: []*Crate{}} + } continue } - cratesLine := strings.Fields(stackInput[i]) - fmt.Println(cratesLine) - for _, crate := range cratesLine { - if stacks[i] == nil { - stacks[i] = Stack{} + stackLine := strings.Fields(stackInput[i]) + + for c, crate := range stackLine { + if crate == "[-]" { + continue } - stack := stacks[i] - stack = append(stack, Crate(crate)) - stacks[i] = stack + stacks[c].AddCrate(crate) } + } + + return stacks +} - fmt.Println(stacks[1]) +func buildArrangements(arrangementsInput []string) []*Arrangement { + arrangements := []*Arrangement{} + + for _, in := range arrangementsInput { + arrangement := NewArrangement(in) + if arrangement == nil { + continue + } + arrangements = append(arrangements, arrangement) } - fmt.Println(len(stacks)) + + return arrangements } func printDrawing(drawing []string) { @@ -78,3 +204,12 @@ func printDrawing(drawing []string) { fmt.Println(line) } } + +func printTopStacks(stacks []*Stack) string { + builder := strings.Builder{} + for _, s := range stacks { + cr := *s.Pick() + builder.WriteByte([]byte(cr)[1]) + } + return builder.String() +} diff --git a/2022/day05/enrichman/parts_test.go b/2022/day05/enrichman/parts_test.go index 4bba564..e69c06e 100644 --- a/2022/day05/enrichman/parts_test.go +++ b/2022/day05/enrichman/parts_test.go @@ -24,7 +24,7 @@ func TestPartOne(t *testing.T) { { name: "example", input: sample, - expected: "not-yet-implemented", + expected: "CMZ", }, } @@ -47,7 +47,7 @@ func TestPartTwo(t *testing.T) { { name: "example", input: sample, - expected: "not-yet-implemented", + expected: "MCD", }, }