-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13p1.go
93 lines (79 loc) · 1.52 KB
/
13p1.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
89
90
91
92
93
package main
import (
"fmt"
"aoc2023/utils"
)
func D13P1() {
lines := utils.ReadLines("inputs/13.txt")
patterns := [][]string{}
currentPattern := []string{}
for _, line := range lines {
if line == "" {
patterns = append(patterns, currentPattern)
currentPattern = []string{}
continue
}
currentPattern = append(currentPattern, line)
}
patterns = append(patterns, currentPattern)
patternScoreSum := 0
for _, pattern := range patterns {
patternScoreSum += getPatternScore(pattern, 0)
}
fmt.Printf("Total pattern score: %d\n", patternScoreSum)
}
func getPatternScore(pattern []string, answerToIgnore int) int {
for i := 0; i < len(pattern); i++ {
if i == 0 {
continue
}
before := i - 1
after := i
isMatch := true
for before >= 0 && after < len(pattern) {
if pattern[before] == pattern[after] {
before--
after++
continue
}
isMatch = false
break
}
if isMatch {
if i*100 != answerToIgnore {
return i * 100
}
}
}
columns := []string{}
for i := 0; i < len(pattern[0]); i++ {
column := ""
for j := 0; j < len(pattern); j++ {
column += string(pattern[j][i])
}
columns = append(columns, column)
}
for i := 0; i < len(columns); i++ {
if i == 0 {
continue
}
before := i - 1
after := i
isMatch := true
for before >= 0 && after < len(columns) {
if columns[before] == columns[after] {
before--
after++
continue
}
isMatch = false
break
}
if isMatch {
if i != answerToIgnore {
return i
}
}
}
return 0
}