You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
1.5 KiB
95 lines
1.5 KiB
12 months ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"bufio"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
func isdigit(b byte) bool {
|
||
|
return b >= 48 && b <= 57
|
||
|
}
|
||
|
|
||
|
func engineparts(buf *bufio.Reader) []int {
|
||
|
parts := make([]int, 0, 150)
|
||
|
|
||
|
outer:
|
||
|
for {
|
||
|
head, err := buf.ReadByte()
|
||
|
if err != nil {
|
||
|
return parts
|
||
|
}
|
||
|
|
||
|
if isdigit(head) {
|
||
|
// start recording digits
|
||
|
b := make([]byte, 0, 10)
|
||
|
b = append(b, head)
|
||
|
|
||
|
// continue reading until number is exhausted
|
||
|
for {
|
||
|
digit, err := buf.ReadByte()
|
||
|
if !isdigit(digit) || err == io.EOF {
|
||
|
value, _ := strconv.Atoi(string(b))
|
||
|
for range b {
|
||
|
parts = append(parts, value)
|
||
|
}
|
||
|
|
||
|
buf.UnreadByte()
|
||
|
continue outer
|
||
|
}
|
||
|
|
||
|
b = append(b, digit)
|
||
|
}
|
||
|
|
||
|
} else if head != '.' {
|
||
|
// head is a symbol
|
||
|
parts = append(parts, -1)
|
||
|
|
||
|
} else {
|
||
|
// head is a '.'
|
||
|
parts = append(parts, 0)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
type day03 struct{}
|
||
|
|
||
|
func (d day03) solve1(r io.Reader) string {
|
||
|
sum := 0
|
||
|
parts := make([][]int, 0, 100)
|
||
|
scanner := bufio.NewScanner(r)
|
||
|
|
||
|
for scanner.Scan() {
|
||
|
line := scanner.Text()
|
||
|
buf := bufio.NewReader(strings.NewReader(line))
|
||
|
lineparts := engineparts(buf)
|
||
|
parts = append(parts, lineparts)
|
||
|
}
|
||
|
|
||
|
for i := 1; i < len(parts)-1; i++ {
|
||
|
for j := 1; j < len(parts[i])-1; j++ {
|
||
|
cursor := parts[i][j]
|
||
|
if cursor == -1 {
|
||
|
row:
|
||
|
for i2 := -1; i2 <= 1; i2++ {
|
||
|
for j2 := -1; j2 <= 1; j2++ {
|
||
|
val := parts[i+i2][j+j2]
|
||
|
if val > 0 {
|
||
|
sum += val
|
||
|
continue row
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return fmt.Sprintf("%d", sum)
|
||
|
}
|
||
|
|
||
|
func (d day03) solve2(r io.Reader) string {
|
||
|
return ""
|
||
|
}
|