Skip to content

Commit

Permalink
Day 13: part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytro-spivakov committed Oct 23, 2024
1 parent e2b6984 commit 2b7f3af
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 0 deletions.
115 changes: 115 additions & 0 deletions day13/day13.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package day13

import (
"bufio"
"fmt"
"log"
"os"
"slices"
"strings"
)

func Solution1(filepath string) int {
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 += calcReflection(currentMatrix)
currentMatrix = make([]string, 0)
continue
}

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

return result
}

func Solution2(filepath string) int {
return -1
}

func calcReflection(m []string) int {
// vertical reflection line
for i := 1; i < len(m); i++ {
step := 0
for {
// 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) {
// 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
}

if m[upIdx] != m[downIdx] {
fmt.Printf("i=%d, step=%d: %s != %s\n", i, step, m[upIdx], m[downIdx])
break
}
step++
}
}

// horizontal reflection line
for i := 1; i < len(m[0]); i++ {
step := 0
for {
leftIdx := i - 1 - step
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)
// i == number of cols left of i
return i
}

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

return 0
}

func printMatrix(m []string) {
fmt.Println("-----MATRIX START-----")
for _, row := range m {
fmt.Println(row)
}
fmt.Println("------MATRIX END------")
}
23 changes: 23 additions & 0 deletions day13/day13_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package day13

import (
"fmt"
"testing"
)

func TestSolution1(t *testing.T) {
cases := map[string]int{
"test_input11": 405,
"test_input12": 1600,
}

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

if result == expectedResult {
fmt.Printf("Solution1()=%d, OK\n", result)
} else {
t.Fatalf("Solution1()=%d, expecting %d, FAIL\n", result, expectedResult)
}
}
}
15 changes: 15 additions & 0 deletions day13/test_input11
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.

#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#
17 changes: 17 additions & 0 deletions day13/test_input12
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
####.#.#......#
#.####.#..##.#.
###.###.###...#
####.#.##....##
.###..#.##..###
#....#....###.#
#...###..#.....
##..###..#.....
#....#....###.#
.###..#.##..###
####.#.##....##
###.###.###...#
#.####.#..##.#.
####.#.#......#
#...#.#.#..#.#.
#.##.##.#....##
#.##.##.#....##
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"advent-of-code/day10"
"advent-of-code/day11"
"advent-of-code/day12"
"advent-of-code/day13"
"fmt"
"time"
)
Expand Down Expand Up @@ -60,4 +61,7 @@ func main() {

runSolution(12, 1, func() int { return day12.Solution1("inputs/day12") })
runSolution(12, 2, func() int { return day12.Solution2("inputs/day12") })

runSolution(13, 1, func() int { return day13.Solution1("inputs/day13") })
runSolution(13, 2, func() int { return day13.Solution2("inputs/day13") })
}

0 comments on commit 2b7f3af

Please sign in to comment.