This repository has been archived by the owner on Dec 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cf5e45c
commit 0e9ea6f
Showing
3 changed files
with
99 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ",") | ||
} |