Browse Source

day 3 part 1

- consider EOF after digit
 - use position and length of numbers to avoid double counting
master
Alexander Avery 1 year ago
parent
commit
14a0c36f4c
  1. 56
      day03.go

56
day03.go

@ -12,11 +12,17 @@ func isdigit(b byte) bool {
return b >= 48 && b <= 57
}
func engineparts(buf *bufio.Reader) []int {
parts := make([]int, 0, 150)
type enginepart struct {
value int
pos int
raw string
}
func engineparts(buf *bufio.Reader) []enginepart {
parts := make([]enginepart, 0, 150)
outer:
for {
for pos := 0; ; pos++ {
head, err := buf.ReadByte()
if err != nil {
return parts
@ -30,10 +36,15 @@ outer:
// continue reading until number is exhausted
for {
digit, err := buf.ReadByte()
if !isdigit(digit) || err == io.EOF {
if !isdigit(digit) {
value, _ := strconv.Atoi(string(b))
for range b {
parts = append(parts, value)
parts = append(parts, enginepart{value: value, pos: pos, raw: string(b)})
}
pos += len(b) - 1
if err == io.EOF {
return parts
}
buf.UnreadByte()
@ -45,11 +56,11 @@ outer:
} else if head != '.' {
// head is a symbol
parts = append(parts, -1)
parts = append(parts, enginepart{value: -1, pos: pos, raw: string(head)})
} else {
// head is a '.'
parts = append(parts, 0)
parts = append(parts, enginepart{value: 0, pos: pos, raw: string(head)})
}
}
}
@ -58,7 +69,7 @@ type day03 struct{}
func (d day03) solve1(r io.Reader) string {
sum := 0
parts := make([][]int, 0, 100)
parts := make([][]enginepart, 0, 100)
scanner := bufio.NewScanner(r)
for scanner.Scan() {
@ -68,19 +79,26 @@ func (d day03) solve1(r io.Reader) string {
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
for row := range parts {
for item := range parts[row] {
if sym := parts[row][item]; sym.value == -1 {
for i := -1; i < 2; i++ {
for j := -1; j < 2; {
x := row + i
y := item + j
if x >= 0 && y >= 0 && x < len(parts) && y < len(parts[row]) {
part := parts[x][y]
if part.value > 0 {
sum += part.value
j += (part.pos + len(part.raw) - y)
} else {
j++
}
}
}
}
}
}

Loading…
Cancel
Save