-
Notifications
You must be signed in to change notification settings - Fork 0
/
day02.go
94 lines (83 loc) · 2.18 KB
/
day02.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package day02 solves AoC 2020 day 2.
package day02
import (
"fmt"
"strings"
"unicode/utf8"
"github.com/fis/aoc/glue"
)
func init() {
glue.RegisterSolver(2020, 2, glue.LineSolver(solve))
}
func solve(lines []string) ([]string, error) {
p1, err := countValid(lines, policy.validateSled)
if err != nil {
return nil, err
}
p2, err := countValid(lines, policy.validateToboggan)
if err != nil {
return nil, err
}
return glue.Ints(p1, p2), nil
}
func countValid(lines []string, validator func(policy, string) bool) (int, error) {
valid := 0
for _, line := range lines {
parts := strings.SplitN(line, ": ", 2)
if len(parts) < 2 {
return 0, fmt.Errorf("invalid password record: %q", line)
}
pol, err := parsePolicy(parts[0])
if err != nil {
return 0, err
}
if validator(pol, parts[1]) {
valid++
}
}
return valid, nil
}
type policy struct {
Min, Max int
C rune
}
func parsePolicy(p string) (policy, error) {
var (
min, max int
c rune
)
if n, err := fmt.Sscanf(p, "%d-%d %c", &min, &max, &c); err != nil || n != 3 {
return policy{}, fmt.Errorf("invalid policy string: %q", p)
}
return policy{Min: int(min), Max: int(max), C: c}, nil
}
func (p policy) validateSled(pass string) bool {
freq := 0
for _, c := range pass {
if c == p.C {
freq++
}
}
return freq >= p.Min && freq <= p.Max
}
func (p policy) validateToboggan(pass string) bool {
c1, _ := utf8.DecodeRuneInString(pass[p.Min-1:])
c2, _ := utf8.DecodeRuneInString(pass[p.Max-1:])
return (c1 == p.C) != (c2 == p.C)
}
func (p policy) String() string {
return fmt.Sprintf("(%d-%d %c)", p.Min, p.Max, p.C)
}