Skip to content

Commit

Permalink
Day 13: Point of Incidence
Browse files Browse the repository at this point in the history
  • Loading branch information
abeltay committed Dec 13, 2023
1 parent 181c31a commit 3bde1d6
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 0 deletions.
133 changes: 133 additions & 0 deletions 13/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package main

import (
"bufio"
"fmt"
"os"
)

func parseFile(filename string) []string {
file, err := os.Open(filename)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer file.Close()

var arr []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
t := scanner.Text()
arr = append(arr, t)
}
return arr
}

func parseInput(filename string) [][][]string {
in := parseFile(filename)

var out [][][]string
var arr [][]string
for _, v := range in {
if v == "" {
out = append(out, arr)
arr = make([][]string, 0)
continue
}
var row []string
for _, n := range v {
row = append(row, string(n))
}
arr = append(arr, row)
}
out = append(out, arr)
return out
}

func rowMirrored(maps [][]string, margin int) int {
for y := range maps {
if y == 0 {
continue
}
var notMirrored bool
smudge := margin
for offset := 0; offset+y < len(maps) && y-offset-1 >= 0; offset++ {
for x := range maps[y] {
if maps[y+offset][x] != maps[y-offset-1][x] {
if smudge > 0 {
smudge--
} else {
notMirrored = true
break
}
}
}
if notMirrored {
break
}
}
if !notMirrored && smudge == 0 {
return y
}
}
return 0
}

func columnMirrored(maps [][]string, margin int) int {
for x := range maps[0] {
if x == 0 {
continue
}
var notMirrored bool
smudge := margin
for offset := 0; x+offset < len(maps[0]) && x-offset-1 >= 0; offset++ {
for y := range maps {
if maps[y][x+offset] != maps[y][x-offset-1] {
if smudge > 0 {
smudge--
} else {
notMirrored = true
break
}
}
}
if notMirrored {
break
}
}
if !notMirrored && smudge == 0 {
return x
}
}
return 0
}

func sumMap(maps [][]string, margin int) int {
if row := rowMirrored(maps, margin); row != 0 {
return row * 100
}
if col := columnMirrored(maps, margin); col != 0 {
return col
}
return 0
}

func part1(filename string) int {
in := parseInput(filename)
var ans int
for _, v := range in {
s := sumMap(v, 0)
ans += s
}
return ans
}

func part2(filename string) int {
in := parseInput(filename)
var ans int
for _, v := range in {
s := sumMap(v, 1)
ans += s
}
return ans
}
25 changes: 25 additions & 0 deletions 13/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"fmt"
"testing"
)

func TestParts(t *testing.T) {
testFile := "testdata/test.txt"
got := part1(testFile)
want := 405
if got != want {
t.Fatalf("got %v want %v", got, want)
}
got2 := part2(testFile)
want2 := 400
if got2 != want2 {
t.Fatalf("got %v want %v", got2, want2)
}

fmt.Println("Running input.txt")
// actualFile := "testdata/input.txt"
// fmt.Println("Answer for part 1:", part1(actualFile))
// fmt.Println("Answer for part 2:", part2(actualFile))
}
15 changes: 15 additions & 0 deletions 13/testdata/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.

#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#

0 comments on commit 3bde1d6

Please sign in to comment.