mirror of
https://github.com/onyx-and-iris/aoc2023.git
synced 2024-11-15 15:10:49 +00:00
81 lines
1.4 KiB
Go
81 lines
1.4 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"bufio"
|
||
|
"log"
|
||
|
"os"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
// readlines reads lines from stdin.
|
||
|
// returns input 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
|
||
|
}
|
||
|
|
||
|
func parseInput(lines []string) {
|
||
|
x := 0
|
||
|
runes = make([][]rune, len(lines))
|
||
|
for i, line := range lines {
|
||
|
for j, r := range line {
|
||
|
runes[i] = append(runes[i], r)
|
||
|
if r == '#' {
|
||
|
galaxies = append(galaxies, newGalaxy(x, j, i))
|
||
|
x++
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
for i := range lines {
|
||
|
if strings.Count(lines[i], "#") == 0 {
|
||
|
empty["row"] = append(empty["row"], i)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
f := func(i int) bool {
|
||
|
for _, line := range lines {
|
||
|
if line[i] == '#' {
|
||
|
return false
|
||
|
}
|
||
|
}
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
for i := range lines[0] {
|
||
|
if f(i) {
|
||
|
empty["col"] = append(empty["col"], i)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// inCompared returns true if both i, j coords are in compared
|
||
|
func inCompared(i, j int, compared [][]int) bool {
|
||
|
for _, comp := range compared {
|
||
|
if contains(comp, i) && contains(comp, j) {
|
||
|
return true
|
||
|
}
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
// contains returns true if a slice of elements contains a given element
|
||
|
func contains[T comparable](elems []T, v T) bool {
|
||
|
for _, s := range elems {
|
||
|
if v == s {
|
||
|
return true
|
||
|
}
|
||
|
}
|
||
|
return false
|
||
|
}
|