From e9ee54b0d9f346c1f67ebc1e0d8c65a1946516be Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Tue, 26 Oct 2021 23:48:48 +0200 Subject: [PATCH] day21; a little bit hacky, but working --- day21/day.go | 114 +++++++++++++++++++++++++++++++++++++++++++++++++ inputs/21 | 101 +++++++++++++++++++++++++++++++++++++++++++ inputs/21_test | 9 ++++ main.go | 3 +- 4 files changed, 226 insertions(+), 1 deletion(-) create mode 100644 day21/day.go create mode 100644 inputs/21 create mode 100644 inputs/21_test diff --git a/day21/day.go b/day21/day.go new file mode 100644 index 0000000..d5a9420 --- /dev/null +++ b/day21/day.go @@ -0,0 +1,114 @@ +package day21 + +import ( + "bytes" + "fmt" + "strconv" + "strings" + "tools" +) + +var revIndex = map[int]int{ + 0: 1, + 1: 1, + 2: 6, + 3: 2, + 4: 7, + 5: 3, + 6: 8, + 7: 4, +} + +func scramble(s string, instr []string, reverse bool) string { + sb := []byte(s) + for _, inst := range instr { + parts := strings.Split(inst, " ") + if parts[0] == "swap" && parts[1] == "position" { + x, _ := strconv.Atoi(parts[2]) + y, _ := strconv.Atoi(parts[5]) + sb[x], sb[y] = sb[y], sb[x] + } else if parts[0] == "swap" && parts[1] == "letter" { + x := bytes.Index(sb, []byte(parts[2])) + y := bytes.Index(sb, []byte(parts[5])) + sb[x], sb[y] = sb[y], sb[x] + } else if parts[0] == "rotate" && parts[1] == "left" { + x, _ := strconv.Atoi(parts[2]) + if reverse { + sb = append(sb[len(sb)-(x%len(sb)):], sb[:len(sb)-(x%len(sb))]...) + } else { + sb = append(sb[x%len(sb):], sb[:x%len(sb)]...) + } + } else if parts[0] == "rotate" && parts[1] == "right" { + x, _ := strconv.Atoi(parts[2]) + if reverse { + sb = append(sb[x%len(sb):], sb[:x%len(sb)]...) + } else { + sb = append(sb[len(sb)-(x%len(sb)):], sb[:len(sb)-(x%len(sb))]...) + } + } else if parts[0] == "rotate" && parts[1] == "based" { + var x int + if reverse { + x = bytes.Index(sb, []byte(parts[6])) + x = revIndex[x] + sb = append(sb[x%len(sb):], sb[:x%len(sb)]...) + } else { + x = bytes.Index(sb, []byte(parts[6])) + if x >= 4 { + x++ + } + x++ + sb = append(sb[len(sb)-(x%len(sb)):], sb[:len(sb)-(x%len(sb))]...) + } + } else if parts[0] == "reverse" && parts[1] == "positions" { + x, _ := strconv.Atoi(parts[2]) + y, _ := strconv.Atoi(parts[4]) + var ns []byte + for i := 0; i < x; i++ { + ns = append(ns, sb[i]) + } + for i := y; i >= x; i-- { + ns = append(ns, sb[i]) + } + sb = append(ns, sb[y+1:]...) + } else if parts[0] == "move" && parts[1] == "position" { + x, _ := strconv.Atoi(parts[2]) + y, _ := strconv.Atoi(parts[5]) + if reverse { + c := sb[y] + sb = append(sb[:y], sb[y+1:]...) + sb = append(sb[:x+1], sb[x:]...) + sb[x] = c + } else { + c := sb[x] + sb = append(sb[:x], sb[x+1:]...) + sb = append(sb[:y+1], sb[y:]...) + sb[y] = c + } + } else { + fmt.Println("ERROR: unknown instruction:", inst) + } + } + + return string(sb) +} + +func Part1(puzzle tools.AoCPuzzle) interface{} { + inp := puzzle.GetInputArray() + startString := inp[0] + password := scramble(startString, inp[1:], false) + + return password +} + +func Part2(puzzle tools.AoCPuzzle) interface{} { + inp := puzzle.GetInputArray() + var rinp []string + for x := len(inp) - 1; x > 0; x-- { + rinp = append(rinp, inp[x]) + } + startString := "fbgdceah" + //startString := "gfdhebac" + password := scramble(startString, rinp, true) + + return password +} diff --git a/inputs/21 b/inputs/21 new file mode 100644 index 0000000..d4a0fb1 --- /dev/null +++ b/inputs/21 @@ -0,0 +1,101 @@ +abcdefgh +reverse positions 1 through 6 +rotate based on position of letter a +swap position 4 with position 1 +reverse positions 1 through 5 +move position 5 to position 7 +swap position 4 with position 0 +swap position 4 with position 6 +rotate based on position of letter a +swap position 0 with position 2 +move position 5 to position 2 +move position 7 to position 1 +swap letter d with letter c +swap position 5 with position 3 +reverse positions 3 through 7 +rotate based on position of letter d +swap position 7 with position 5 +rotate based on position of letter f +swap position 4 with position 1 +swap position 3 with position 6 +reverse positions 4 through 7 +rotate based on position of letter c +move position 0 to position 5 +swap position 7 with position 4 +rotate based on position of letter f +reverse positions 1 through 3 +move position 5 to position 3 +rotate based on position of letter g +reverse positions 2 through 5 +rotate right 0 steps +rotate left 0 steps +swap letter f with letter b +rotate based on position of letter h +move position 1 to position 3 +reverse positions 3 through 6 +rotate based on position of letter h +swap position 4 with position 3 +swap letter b with letter h +swap letter a with letter h +reverse positions 1 through 6 +swap position 3 with position 6 +swap letter e with letter d +swap letter e with letter h +swap position 1 with position 5 +rotate based on position of letter a +reverse positions 4 through 5 +swap position 0 with position 4 +reverse positions 0 through 3 +move position 7 to position 2 +swap letter e with letter c +swap position 3 with position 4 +rotate left 3 steps +rotate left 7 steps +rotate based on position of letter e +reverse positions 5 through 6 +move position 1 to position 5 +move position 1 to position 2 +rotate left 1 step +move position 7 to position 6 +rotate left 0 steps +reverse positions 5 through 6 +reverse positions 3 through 7 +swap letter d with letter e +rotate right 3 steps +swap position 2 with position 1 +swap position 5 with position 7 +swap letter h with letter d +swap letter c with letter d +rotate based on position of letter d +swap letter d with letter g +reverse positions 0 through 1 +rotate right 0 steps +swap position 2 with position 3 +rotate left 4 steps +rotate left 5 steps +move position 7 to position 0 +rotate right 1 step +swap letter g with letter f +rotate based on position of letter a +rotate based on position of letter b +swap letter g with letter e +rotate right 4 steps +rotate based on position of letter h +reverse positions 3 through 5 +swap letter h with letter e +swap letter g with letter a +rotate based on position of letter c +reverse positions 0 through 4 +rotate based on position of letter e +reverse positions 4 through 7 +rotate left 4 steps +swap position 0 with position 6 +reverse positions 1 through 6 +rotate left 2 steps +swap position 5 with position 3 +swap letter b with letter d +swap letter b with letter d +rotate based on position of letter d +rotate based on position of letter c +rotate based on position of letter h +move position 4 to position 7 \ No newline at end of file diff --git a/inputs/21_test b/inputs/21_test new file mode 100644 index 0000000..4654418 --- /dev/null +++ b/inputs/21_test @@ -0,0 +1,9 @@ +abcde +swap position 4 with position 0 +swap letter d with letter b +reverse positions 0 through 4 +rotate left 1 step +move position 1 to position 4 +move position 3 to position 0 +rotate based on position of letter b +rotate based on position of letter d \ No newline at end of file diff --git a/main.go b/main.go index 832d36f..bce07e4 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,7 @@ import ( "aoc2016/day18" "aoc2016/day19" "aoc2016/day20" + "aoc2016/day21" "aoc2016/day23" "aoc2016/day24" "flag" @@ -59,7 +60,7 @@ func initDayFunctions() { 18: {1: day18.Part1, 2: day18.Part2}, 19: {1: day19.Part1, 2: day19.Part2}, 20: {1: day20.Part1, 2: day20.Part2}, - // 21: {1: day21.Part1, 2: day21.Part2}, + 21: {1: day21.Part1, 2: day21.Part2}, // 22: {1: day22.Part1, 2: day22.Part2}, 23: {1: day23.Part1, 2: day23.Part2}, 24: {1: day24.Part1, 2: day24.Part2},