|
@@ -41,7 +41,7 @@ func readInput(file *os.File) []Pattern {
|
|
return patterns
|
|
return patterns
|
|
}
|
|
}
|
|
|
|
|
|
-func checkVertical(index int, note [][]byte, width int) bool {
|
|
|
|
|
|
+func checkVertical(index int, note [][]byte, width int, canMissOne bool) bool {
|
|
left := 0
|
|
left := 0
|
|
prev := index - 1
|
|
prev := index - 1
|
|
right := index + prev
|
|
right := index + prev
|
|
@@ -56,6 +56,11 @@ func checkVertical(index int, note [][]byte, width int) bool {
|
|
|
|
|
|
for i := range note {
|
|
for i := range note {
|
|
if note[i][prev] != note[i][index] {
|
|
if note[i][prev] != note[i][index] {
|
|
|
|
+ if canMissOne {
|
|
|
|
+ canMissOne = false
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+
|
|
return false
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -67,7 +72,7 @@ func checkVertical(index int, note [][]byte, width int) bool {
|
|
return true
|
|
return true
|
|
}
|
|
}
|
|
|
|
|
|
-func checkHorizontal(index int, note [][]byte, height int, width int) bool {
|
|
|
|
|
|
+func checkHorizontal(index int, note [][]byte, height int, width int, canMissOne bool) bool {
|
|
up := 0
|
|
up := 0
|
|
prev := index - 1
|
|
prev := index - 1
|
|
|
|
|
|
@@ -83,6 +88,11 @@ func checkHorizontal(index int, note [][]byte, height int, width int) bool {
|
|
|
|
|
|
for i := range note[index] {
|
|
for i := range note[index] {
|
|
if note[index][i] != note[prev][i] {
|
|
if note[index][i] != note[prev][i] {
|
|
|
|
+ if canMissOne {
|
|
|
|
+ canMissOne = false
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+
|
|
return false
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -102,7 +112,7 @@ func part1(patterns []Pattern) int {
|
|
|
|
|
|
gotVertical := false
|
|
gotVertical := false
|
|
for index := 1; index < width; index++ {
|
|
for index := 1; index < width; index++ {
|
|
- if checkVertical(index, patterns[i].note, width) {
|
|
|
|
|
|
+ if checkVertical(index, patterns[i].note, width, false) {
|
|
result += index
|
|
result += index
|
|
gotVertical = true
|
|
gotVertical = true
|
|
break
|
|
break
|
|
@@ -114,7 +124,7 @@ func part1(patterns []Pattern) int {
|
|
}
|
|
}
|
|
|
|
|
|
for index := 1; index < height; index++ {
|
|
for index := 1; index < height; index++ {
|
|
- if checkHorizontal(index, patterns[i].note, height, width) {
|
|
|
|
|
|
+ if checkHorizontal(index, patterns[i].note, height, width, false) {
|
|
result += index * 100
|
|
result += index * 100
|
|
break
|
|
break
|
|
}
|
|
}
|
|
@@ -124,6 +134,38 @@ func part1(patterns []Pattern) int {
|
|
return result
|
|
return result
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func part2(patterns []Pattern) int {
|
|
|
|
+ var result int
|
|
|
|
+ for i := range patterns {
|
|
|
|
+ width := len(patterns[i].note[0])
|
|
|
|
+ height := len(patterns[i].note)
|
|
|
|
+
|
|
|
|
+ vertical := 0
|
|
|
|
+ for index := 1; index < width; index++ {
|
|
|
|
+ if checkVertical(index, patterns[i].note, width, true) {
|
|
|
|
+ vertical = index
|
|
|
|
+ break
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ horizontal := 0
|
|
|
|
+ for index := 1; index < height; index++ {
|
|
|
|
+ if checkHorizontal(index, patterns[i].note, height, width, true) {
|
|
|
|
+ horizontal = index
|
|
|
|
+ break
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if horizontal > 0 {
|
|
|
|
+ result += horizontal * 100
|
|
|
|
+ } else if vertical > 0 {
|
|
|
|
+ result += vertical
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return result
|
|
|
|
+}
|
|
|
|
+
|
|
func main() {
|
|
func main() {
|
|
if len(os.Args) < 2 {
|
|
if len(os.Args) < 2 {
|
|
log.Fatal("You need to specify a file!")
|
|
log.Fatal("You need to specify a file!")
|
|
@@ -138,4 +180,5 @@ func main() {
|
|
|
|
|
|
patterns := readInput(file)
|
|
patterns := readInput(file)
|
|
fmt.Println("Part1:", part1(patterns))
|
|
fmt.Println("Part1:", part1(patterns))
|
|
|
|
+ fmt.Println("Part2:", part2(patterns))
|
|
}
|
|
}
|