aoc2023/day-13/util.go

58 lines
951 B
Go
Raw Normal View History

2023-12-14 03:44:14 +00:00
package main
import (
"bufio"
"log"
"os"
)
// readlines reads lines from stdin.
// it returns them as an array of strings
func readlines() []string {
lines := []string{}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return lines
}
// parselines stores each image into an images struct
func parselines(lines []string) {
addImage := func(i int) int {
image := newImg()
for ; i < len(lines); i++ {
if len(lines[i]) == 0 {
break
}
image.raw = append(image.raw, lines[i])
}
images.img = append(images.img, image)
return i
}
images = newImages()
for i := 0; i < len(lines); i++ {
next := addImage(i)
i = next
}
}
// numDiffs returns the number of difference between two strings
func numDiffs(a, b string) int {
diff := 0
for i := range a {
if a[i] != b[i] {
diff++
}
}
return diff
}