-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
91 lines (69 loc) · 1.57 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
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
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
var input []string
for s := bufio.NewScanner(os.Stdin); s.Scan(); {
input = append(input, s.Text())
}
fmt.Printf("Part 1: %d\n", part1(input))
fmt.Printf("Part 2: %d\n", part2(input))
}
func part1(input []string) int {
antennas := parse(input)
antinodes := make(map[Coord]struct{})
for _, locations := range antennas {
for _, loc := range locations {
for _, loc2 := range locations {
if loc == loc2 {
continue
}
dx := loc.x - loc2.x
dy := loc.y - loc2.y
antinode := Coord{loc.x + dx, loc.y + dy}
if antinode.x >= 0 && antinode.x < len(input[0]) &&
antinode.y >= 0 && antinode.y < len(input) {
antinodes[antinode] = struct{}{}
}
}
}
}
return len(antinodes)
}
func part2(input []string) int {
antennas := parse(input)
antinodes := make(map[Coord]struct{})
for _, locations := range antennas {
for _, loc := range locations {
for _, loc2 := range locations {
if loc == loc2 {
continue
}
dx := loc.x - loc2.x
dy := loc.y - loc2.y
for x, y := loc.x, loc.y; 0 <= x && x < len(input[0]) && 0 <= y && y < len(input); x, y = x+dx, y+dy {
antinodes[Coord{x, y}] = struct{}{}
}
}
}
}
return len(antinodes)
}
type Coord struct {
x, y int
}
func parse(input []string) (antennas map[byte][]Coord) {
antennas = make(map[byte][]Coord)
for y := range input {
for x := range input[y] {
if input[y][x] == '.' {
continue
}
antennas[input[y][x]] = append(antennas[input[y][x]], Coord{x, y})
}
}
return antennas
}