-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13p2.go
83 lines (70 loc) · 1.99 KB
/
13p2.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
package main
import (
"fmt"
"aoc2023/utils"
)
func D13P2() {
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 {
oldPatternScore := getPatternScore(pattern, 0)
patternScore := oldPatternScore
x, y := 0, 0
for patternScore == oldPatternScore {
pattern, x, y = getNextPattern(pattern, x, y)
patternScore = getPatternScore(pattern, oldPatternScore)
if patternScore == 0 {
patternScore = oldPatternScore
}
}
patternScoreSum += patternScore
}
fmt.Printf("Total pattern score: %d\n", patternScoreSum)
}
func getNextPattern(pattern []string, x, y int) ([]string, int, int) {
previosCharX, previousCharY := x, y
if x == len(pattern[0]) {
x = 0
y++
}
if x == 0 && y > 0 {
previosCharX = len(pattern[0]) - 1
previousCharY = y - 1
} else if x > 0 {
previosCharX = x - 1
}
if y == len(pattern) {
return pattern, x, y
}
newPattern := make([]string, len(pattern))
if pattern[previousCharY][previosCharX] == '.' {
// Replace with a #
copy(newPattern, pattern)
newPattern[previousCharY] = newPattern[previousCharY][:previosCharX] + "#" + newPattern[previousCharY][previosCharX+1:]
} else if pattern[previousCharY][previosCharX] == '#' {
// Replace with a .
copy(newPattern, pattern)
newPattern[previousCharY] = newPattern[previousCharY][:previosCharX] + "." + newPattern[previousCharY][previosCharX+1:]
}
if pattern[y][x] == '.' {
// Replace with a #
newPattern[y] = newPattern[y][:x] + "#" + newPattern[y][x+1:]
return newPattern, x + 1, y
} else if pattern[y][x] == '#' {
// Replace with a .
newPattern[y] = newPattern[y][:x] + "." + newPattern[y][x+1:]
return newPattern, x + 1, y
}
return pattern, x + 1, y
}