-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (38 loc) · 1.2 KB
/
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
// -*- compile-command: "go run main.go ../example1.txt ../input.txt"; -*-
package main
import (
"flag"
"fmt"
"log"
"regexp"
. "github.com/gmlewis/advent-of-code-2021/enum"
"github.com/gmlewis/advent-of-code-2021/maps"
"github.com/gmlewis/advent-of-code-2021/must"
)
// That's the right answer!
// You are one gold star closer to finding the sleigh keys.
// You got rank 843 on this star's leaderboard.
var logf = log.Printf
var printf = fmt.Printf
func main() {
flag.Parse()
Each(flag.Args(), process)
}
func process(filename string) {
logf("Processing %v ...", filename)
lines := must.ReadFileLines(filename)
b := Reduce(lines, map[string]int{}, parseLines)
crossings := maps.Count(b, func(k string, v int) bool { return v >= 2 })
printf("Solution: %v\n", crossings)
}
var lineRE = regexp.MustCompile(`(\d+),(\d+) -> (\d+),(\d+)`)
func parseLines(line string, b map[string]int) map[string]int {
m := lineRE.FindStringSubmatch(line)
if len(m) < 5 {
log.Fatalf("bad line: %q", line)
}
x1, y1, x2, y2 := must.Atoi(m[1]), must.Atoi(m[2]), must.Atoi(m[3]), must.Atoi(m[4])
pts := Ranges([]int{x1, y1}, []int{x2, y2})
Each(pts, func(pt []int) { b[fmt.Sprintf("%v,%v", pt[1], pt[0])]++ })
return b
}