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

Commit 0e9ea6f

Browse files
committed
Add solution day 25 part 1
1 parent cf5e45c commit 0e9ea6f

File tree

3 files changed

+99
-2
lines changed

3 files changed

+99
-2
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ automatically rebuilt and redeployed every time the `common` module or their own
5757
| 09 | ⭐ ⭐ | 22 | ⭐ ⭐ |
5858
| 10 | ⭐ ⭐ | 23 | ⭐ ⭐ |
5959
| 11 | ⭐ ⭐ | 24 | ⭐ ⭐ |
60-
| 12 | ⭐ ⭐ | 25 | |
60+
| 12 | ⭐ ⭐ | 25 | |
6161
| 13 | ⭐ ⭐ | | |

solutions/day25/main.go

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,34 @@
11
package main
22

33
import (
4+
"fmt"
45
"github.com/terminalnode/adventofcode2024/common"
56
)
67

78
func main() {
8-
common.Setup(25, nil, nil)
9+
common.Setup(25, part1, nil)
10+
}
11+
12+
func part1(
13+
input string,
14+
) string {
15+
keys, locks := parse(input)
16+
count := 0
17+
for _, key := range keys {
18+
for _, lock := range locks {
19+
allOk := true
20+
for i, keyN := range key.a {
21+
lockN := lock.a[i]
22+
if keyN+lockN > 7 {
23+
allOk = false
24+
break
25+
}
26+
}
27+
if allOk {
28+
count++
29+
}
30+
}
31+
}
32+
33+
return fmt.Sprintf("Number of ok key-lock combos: %d", count)
934
}

solutions/day25/parse.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package main
2+
3+
import (
4+
"strconv"
5+
"strings"
6+
)
7+
8+
type entryType int
9+
type keyArray []entry
10+
type lockArray []entry
11+
12+
const (
13+
KEY = entryType(iota)
14+
LOCK
15+
)
16+
17+
type entry struct {
18+
t entryType
19+
a []int
20+
aInv []int
21+
s string
22+
sInv string
23+
}
24+
25+
func parse(
26+
input string,
27+
) (keyArray, lockArray) {
28+
split := strings.Split(input, "\n\n")
29+
keys := make(keyArray, 0, len(split))
30+
locks := make(lockArray, 0, len(split))
31+
32+
for _, rawEntry := range split {
33+
lines := strings.Split(rawEntry, "\n")
34+
35+
a := []int{0, 0, 0, 0, 0}
36+
aInv := []int{7, 7, 7, 7, 7}
37+
for _, line := range lines {
38+
for x, ch := range line {
39+
if ch == '#' {
40+
a[x] += 1
41+
aInv[x] -= 1
42+
}
43+
}
44+
}
45+
s := arrToString(a)
46+
sInv := arrToString(aInv)
47+
48+
if lines[0] == "#####" {
49+
locks = append(locks, entry{
50+
t: LOCK,
51+
a: a, aInv: aInv,
52+
s: s, sInv: sInv})
53+
} else {
54+
keys = append(keys, entry{
55+
t: KEY,
56+
a: a, aInv: aInv,
57+
s: s, sInv: sInv})
58+
}
59+
}
60+
61+
return keys, locks
62+
}
63+
64+
func arrToString(
65+
arr []int,
66+
) string {
67+
sArr := make([]string, len(arr))
68+
for i, n := range arr {
69+
sArr[i] = strconv.Itoa(n)
70+
}
71+
return strings.Join(sArr, ",")
72+
}

0 commit comments

Comments
 (0)