-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
56 lines (44 loc) · 1023 Bytes
/
main.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
// -*- compile-command: "go run main.go ../example1.txt ../input.txt"; -*-
package main
import (
"flag"
"fmt"
"log"
"strings"
. "github.com/gmlewis/advent-of-code-2021/enum"
"github.com/gmlewis/advent-of-code-2021/must"
)
var logf = log.Printf
var printf = fmt.Printf
func main() {
flag.Parse()
Each(flag.Args(), process)
}
func process(filename string) {
logf("Processing %v ...", filename)
sacks := must.ReadFileLines(filename)
dups := Map(sacks, findDups)
priorities := Map(dups, itemPriority)
sum := Sum(priorities)
printf("Solution: %v\n", sum)
}
func findDups(sack string) string {
n := len(sack) / 2
in1, in2 := map[rune]int{}, map[rune]int{}
for i, r := range sack {
if i < n {
in1[r]++
continue
}
in2[r]++
if _, ok := in1[r]; ok {
return string(r)
}
}
log.Fatalf("Could not find dup in %q", sack)
return ""
}
const priorities = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func itemPriority(item string) int {
return strings.Index(priorities, item)
}