From 0e9ea6f679b33467bd292333d1430498604e70ee Mon Sep 17 00:00:00 2001 From: Alexander Rundberg Date: Wed, 25 Dec 2024 19:14:16 +0100 Subject: [PATCH] Add solution day 25 part 1 --- README.md | 2 +- solutions/day25/main.go | 27 ++++++++++++++- solutions/day25/parse.go | 72 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 solutions/day25/parse.go diff --git a/README.md b/README.md index f61cfd8..f64a955 100644 --- a/README.md +++ b/README.md @@ -57,5 +57,5 @@ automatically rebuilt and redeployed every time the `common` module or their own | 09 | ⭐ ⭐ | 22 | ⭐ ⭐ | | 10 | ⭐ ⭐ | 23 | ⭐ ⭐ | | 11 | ⭐ ⭐ | 24 | ⭐ ⭐ | -| 12 | ⭐ ⭐ | 25 | | +| 12 | ⭐ ⭐ | 25 | ⭐ | | 13 | ⭐ ⭐ | | | diff --git a/solutions/day25/main.go b/solutions/day25/main.go index 0f0bade..e0f38f9 100644 --- a/solutions/day25/main.go +++ b/solutions/day25/main.go @@ -1,9 +1,34 @@ package main import ( + "fmt" "github.com/terminalnode/adventofcode2024/common" ) func main() { - common.Setup(25, nil, nil) + common.Setup(25, part1, nil) +} + +func part1( + input string, +) string { + keys, locks := parse(input) + count := 0 + for _, key := range keys { + for _, lock := range locks { + allOk := true + for i, keyN := range key.a { + lockN := lock.a[i] + if keyN+lockN > 7 { + allOk = false + break + } + } + if allOk { + count++ + } + } + } + + return fmt.Sprintf("Number of ok key-lock combos: %d", count) } diff --git a/solutions/day25/parse.go b/solutions/day25/parse.go new file mode 100644 index 0000000..b598ab3 --- /dev/null +++ b/solutions/day25/parse.go @@ -0,0 +1,72 @@ +package main + +import ( + "strconv" + "strings" +) + +type entryType int +type keyArray []entry +type lockArray []entry + +const ( + KEY = entryType(iota) + LOCK +) + +type entry struct { + t entryType + a []int + aInv []int + s string + sInv string +} + +func parse( + input string, +) (keyArray, lockArray) { + split := strings.Split(input, "\n\n") + keys := make(keyArray, 0, len(split)) + locks := make(lockArray, 0, len(split)) + + for _, rawEntry := range split { + lines := strings.Split(rawEntry, "\n") + + a := []int{0, 0, 0, 0, 0} + aInv := []int{7, 7, 7, 7, 7} + for _, line := range lines { + for x, ch := range line { + if ch == '#' { + a[x] += 1 + aInv[x] -= 1 + } + } + } + s := arrToString(a) + sInv := arrToString(aInv) + + if lines[0] == "#####" { + locks = append(locks, entry{ + t: LOCK, + a: a, aInv: aInv, + s: s, sInv: sInv}) + } else { + keys = append(keys, entry{ + t: KEY, + a: a, aInv: aInv, + s: s, sInv: sInv}) + } + } + + return keys, locks +} + +func arrToString( + arr []int, +) string { + sArr := make([]string, len(arr)) + for i, n := range arr { + sArr[i] = strconv.Itoa(n) + } + return strings.Join(sArr, ",") +}