-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
74 lines (58 loc) · 1.22 KB
/
main.go
File metadata and controls
74 lines (58 loc) · 1.22 KB
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
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"regexp"
"strings"
"github.com/danvolchek/AdventOfCode/lib"
)
var targetProperties = map[string]int{
"children": 3,
"cats": 7,
"samoyeds": 2,
"pomeranians": 3,
"akitas": 0,
"vizslas": 0,
"goldfish": 5,
"trees": 3,
"cars": 2,
"perfumes": 1,
}
type constraint struct {
num int
properties map[string]int
}
func (c constraint) matchesTarget() bool {
for key, val := range c.properties {
if targetProperties[key] != val {
return false
}
}
return true
}
var parseRegexp = regexp.MustCompile(`Sue (\d+): (.+)`)
func parse(matches []string) constraint {
properties := make(map[string]int)
rawProps := strings.Split(matches[1], ", ")
for _, property := range rawProps {
parsedProp := strings.Split(property, ": ")
properties[parsedProp[0]] = lib.Atoi(parsedProp[1])
}
return constraint{
num: lib.Atoi(matches[0]),
properties: properties,
}
}
func solve(sues []constraint) int {
for _, sue := range sues {
if sue.matchesTarget() {
return sue.num
}
}
return -1
}
func main() {
solver := lib.Solver[[]constraint, int]{
ParseF: lib.ParseLine(lib.ParseRegexp(parseRegexp, parse)),
SolveF: solve,
}
solver.Verify(103)
}