mirror of
https://github.com/onyx-and-iris/aoc2023.git
synced 2024-11-15 15:10:49 +00:00
72 lines
1.4 KiB
Go
72 lines
1.4 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"bufio"
|
||
|
"log"
|
||
|
"os"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
"unicode"
|
||
|
)
|
||
|
|
||
|
// 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 parselines(lines []string) {
|
||
|
f := func(c rune) bool {
|
||
|
return !unicode.IsDigit(c)
|
||
|
}
|
||
|
|
||
|
records = make([]record, len(lines))
|
||
|
for i, line := range lines {
|
||
|
record := newRecord()
|
||
|
for _, r := range line {
|
||
|
switch r {
|
||
|
case '.':
|
||
|
record.springs = append(record.springs, newSpring(OPERATIONAL))
|
||
|
case '#':
|
||
|
record.springs = append(record.springs, newSpring(DAMAGED))
|
||
|
case '?':
|
||
|
record.springs = append(record.springs, newSpring(UNKNOWN))
|
||
|
}
|
||
|
}
|
||
|
record.format = convertToInts(strings.FieldsFunc(line, f))
|
||
|
|
||
|
records[i] = record
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// convertToInts converts a string representing ints to an array of ints
|
||
|
func convertToInts(data []string) []int {
|
||
|
nums := []int{}
|
||
|
for _, elem := range data {
|
||
|
n, _ := strconv.Atoi(elem)
|
||
|
nums = append(nums, n)
|
||
|
}
|
||
|
return nums
|
||
|
}
|
||
|
|
||
|
// 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
|
||
|
}
|