-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise.go
63 lines (51 loc) · 1.25 KB
/
exercise.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package exercises
import (
"github.com/asphaltbuffet/advent-of-code/internal/common"
)
// Exercise for Advent of Code 2023 day 3.
type Exercise struct {
common.BaseExercise
}
// One returns the answer to the first part of the exercise.
func (e Exercise) One(instr string) (any, error) {
// logger.Level = log.DebugLevel
s := New(instr)
// for each part, check if the bounds are a symbol
var sum int
for _, p := range s.parts {
p := p
for _, b := range p.bounds {
if s.isSymbol[b] {
// fmt.Printf("part[%d] %q has a symbol in its bounds at %s\n", p.id, p.value, b)
sum += p.number
break
}
}
}
return sum, nil
}
// Two returns the answer to the second part of the exercise.
func (e Exercise) Two(instr string) (any, error) {
// logger.Level = log.DebugLevel
s := New(instr)
counts := make(map[point][]int)
// for each part, check if the bounds are a symbol
for gp := range s.isSymbol {
for _, p := range s.parts {
for _, b := range p.bounds {
if b.x != gp.x || b.y != gp.y {
continue
}
// fmt.Printf("symbol %s has part %d\n", gp, p.number)
counts[gp] = append(counts[gp], p.number)
}
}
}
var sum int
for _, c := range counts {
if len(c) == 2 {
sum += c[0] * c[1]
}
}
return sum, nil
}