-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
88 lines (79 loc) · 1.61 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"fmt"
aoc "github.com/shraddhaag/aoc/library"
)
func main() {
input := aoc.ReadFileLineByLine("input.txt")
keys, locks := getKeysAndLocks(input)
fmt.Println(checkAllLockAndKeys(locks, keys))
}
func getKeysAndLocks(input []string) (keys [][]int, locks [][]int) {
current := []string{}
for _, line := range input {
if len(line) == 0 && len(current) != 0 {
pattern, isKey := processPattern(current)
if isKey {
keys = append(keys, pattern)
} else {
locks = append(locks, pattern)
}
current = []string{}
continue
}
current = append(current, line)
}
pattern, isKey := processPattern(current)
if isKey {
keys = append(keys, pattern)
} else {
locks = append(locks, pattern)
}
return
}
func processPattern(input []string) (heights []int, isKey bool) {
switch input[0][0] {
case '.':
// this is a key
isKey = true
for i := 0; i < len(input[0]); i++ {
for j := 1; j < len(input); j++ {
if input[j][i] == '#' {
heights = append(heights, j-1)
break
}
}
}
case '#':
// this is a lock
for i := 0; i < len(input[0]); i++ {
for j := 1; j < len(input); j++ {
if input[j][i] == '.' {
heights = append(heights, j-1)
break
}
}
}
}
// fmt.Println(input, heights)
return
}
func isLockAndKeyFit(lock, key []int) bool {
for index, lockHeight := range lock {
if key[index] < lockHeight {
return false
}
}
return true
}
func checkAllLockAndKeys(locks, keys [][]int) int {
count := 0
for _, lock := range locks {
for _, key := range keys {
if isLockAndKeyFit(lock, key) {
count++
}
}
}
return count
}