summaryrefslogtreecommitdiff
path: root/day25/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'day25/main.go')
-rw-r--r--day25/main.go64
1 files changed, 64 insertions, 0 deletions
diff --git a/day25/main.go b/day25/main.go
new file mode 100644
index 0000000..907c397
--- /dev/null
+++ b/day25/main.go
@@ -0,0 +1,64 @@
+package main
+
+import (
+ "bufio"
+ "fmt"
+ "os"
+)
+
+func main() {
+ fmt.Println(part1(readData("example.txt")), 3)
+ fmt.Println(part1(readData("data.txt")), 3)
+}
+
+type Data struct {
+ keys [][5]int
+ locks [][5]int
+}
+
+func fits(k, l [5]int) bool {
+ for i := range k {
+ if k[i]+l[i] > 5 {
+ return false
+ }
+ }
+ return true
+}
+
+func part1(data Data) (count int) {
+ for _, k := range data.keys {
+ for _, l := range data.locks {
+ if fits(k, l) {
+ count++
+ }
+ }
+ }
+ return
+}
+
+func readData(fileName string) (data Data) {
+ data = Data{}
+ fp, _ := os.Open(fileName)
+ scanner := bufio.NewScanner(fp)
+
+ for scanner.Scan() {
+ isKey := scanner.Text()[0] == '.'
+ code := [5]int{0, 0, 0, 0, 0}
+ for i := 0; scanner.Scan() && i < 5; i++ {
+ line := scanner.Text()
+ for j := range 5 {
+ if line[j] == '#' {
+ code[j]++
+ }
+ }
+ }
+ scanner.Scan()
+ if isKey {
+ data.keys = append(data.keys, code)
+ } else {
+ data.locks = append(data.locks, code)
+ }
+ }
+
+ return
+}