Skip to content

Commit

Permalink
Day 13
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytro-spivakov committed Oct 23, 2024
1 parent 2b7f3af commit f6db22d
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ A small practice project created to get familiar with Go syntax.
- [x] day 10
- [x] day 11
- [x] day 12
- [ ] day 13
- [x] day 13
- [ ] day 14
- [ ] day 15
- [ ] day 16
Expand Down
125 changes: 113 additions & 12 deletions day13/day13.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"fmt"
"log"
"os"
"slices"
"strings"
// "slices"
// "strings"
)

func Solution1(filepath string) int {
Expand Down Expand Up @@ -40,7 +40,109 @@ func Solution1(filepath string) int {
}

func Solution2(filepath string) int {
return -1
file, err := os.Open(filepath)
if err != nil {
log.Fatalf("Failed to open the input file with: %v\n", err.Error())
}

result := 0
scanner := bufio.NewScanner(file)
var currentMatrix []string
for scanner.Scan() {
currentLine := scanner.Text()

if len(currentLine) == 0 {
result += calcReflection2(currentMatrix)
currentMatrix = make([]string, 0)
continue
}

currentMatrix = append(currentMatrix, currentLine)
}
if len(currentMatrix) != 0 {
result += calcReflection2(currentMatrix)
}
if err = scanner.Err(); err != nil {
log.Fatalf("Error during input file read: %v\n", err.Error())
}

return result
}

func calcReflection2(m []string) int {
// vertical reflection line
for i := len(m) - 1; i >= 1; i-- {
step := 0
diff := 0
for {
if diff > 1 {
break
}
// imaginary reflection line is between upIdx=i-1 and downIdx=i, then we start with offset 0 and just keep increasing it by 1 on each iteration
// we keep walking until:
// a) success - we've reached one of the ends => return
// b) failure - we've encountered m[upIdx] != m[downIdx] => break and move onto the next i
upIdx := i - 1 - step
downIdx := i + step

if upIdx < 0 || downIdx >= len(m) {
// new reflections only
if diff != 1 {
break
}
// i == number of rows above the current i
// separator := strings.Repeat("-", len(m[0]))
// m = slices.Insert(m, i, separator)
// printMatrix(m)
return 100 * i
}

for j := 0; j < len(m[0]); j++ {
if m[upIdx][j] != m[downIdx][j] {
diff++
}
}

step++
}
}

// horizontal reflection line
for i := len(m[0]) - 1; i >= 1; i-- {
step := 0
diff := 0
for {
if diff > 1 {
break
}
leftIdx := i - 1 - step
rightIdx := i + step

if leftIdx < 0 || rightIdx >= len(m[0]) {
// new reflections only
if diff != 1 {
break
}
// separator := "|"
// for j := 0; j < len(m); j++ {
// m[j] = m[j][:i] + separator + m[j][i:]
// }
// printMatrix(m)
// i == number of cols left of i
return i
}

for j := 0; j < len(m); j++ {
currentRow := m[j]
if currentRow[leftIdx] != currentRow[rightIdx] {
diff++
}
}
step++
}
}

return 0
}

func calcReflection(m []string) int {
Expand All @@ -57,14 +159,13 @@ func calcReflection(m []string) int {

if upIdx < 0 || downIdx >= len(m) {
// i == number of rows above the current i
separator := strings.Repeat("-", len(m[0]))
m = slices.Insert(m, i, separator)
printMatrix(m)
// separator := strings.Repeat("-", len(m[0]))
// m = slices.Insert(m, i, separator)
// printMatrix(m)
return 100 * i
}

if m[upIdx] != m[downIdx] {
fmt.Printf("i=%d, step=%d: %s != %s\n", i, step, m[upIdx], m[downIdx])
break
}
step++
Expand All @@ -79,11 +180,11 @@ func calcReflection(m []string) int {
rightIdx := i + step

if leftIdx < 0 || rightIdx >= len(m[0]) {
separator := "|"
for j := 0; j < len(m); j++ {
m[j] = m[j][:i] + separator + m[j][i:]
}
printMatrix(m)
// separator := "|"
// for j := 0; j < len(m); j++ {
// m[j] = m[j][:i] + separator + m[j][i:]
// }
// printMatrix(m)
// i == number of cols left of i
return i
}
Expand Down
16 changes: 16 additions & 0 deletions day13/day13_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,19 @@ func TestSolution1(t *testing.T) {
}
}
}

func TestSolution2(t *testing.T) {
cases := map[string]int{
"test_input11": 400,
}

for input, expectedResult := range cases {
result := Solution2(input)

if result == expectedResult {
fmt.Printf("Solution2()=%d, OK\n", result)
} else {
t.Fatalf("Solution2()=%d, expecting %d, FAIL\n", result, expectedResult)
}
}
}

0 comments on commit f6db22d

Please sign in to comment.