Skip to content
This repository has been archived by the owner on Dec 28, 2024. It is now read-only.

Commit

Permalink
Add solution day 25 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
terminalnode committed Dec 25, 2024
1 parent cf5e45c commit 0e9ea6f
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | ⭐ ⭐ | | |
27 changes: 26 additions & 1 deletion solutions/day25/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
72 changes: 72 additions & 0 deletions solutions/day25/parse.go
Original file line number Diff line number Diff line change
@@ -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, ",")
}

0 comments on commit 0e9ea6f

Please sign in to comment.