Skip to content

Commit b9839c8

Browse files
committed
day8: solved puzzle 1
1 parent 28c36a6 commit b9839c8

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

day8/example.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
............
2+
........0...
3+
.....0......
4+
.......0....
5+
....0.......
6+
......A.....
7+
............
8+
............
9+
........A...
10+
.........A..
11+
............
12+
............

day8/main.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"regexp"
8+
9+
"github.com/fxnn/adventofcode2024/util"
10+
)
11+
12+
const ANTENNA_PATTERN = "[a-zA-Z0-9]"
13+
14+
func discoverAntennaPoints(lines [][]byte) map[byte][]util.Point {
15+
var antennaPattern = regexp.MustCompile(ANTENNA_PATTERN)
16+
var antennaPoints = make(map[byte][]util.Point)
17+
for y, line := range lines {
18+
for x, b := range line {
19+
if antennaPattern.Match([]byte{b}) {
20+
var list []util.Point
21+
if l, ok := antennaPoints[b]; ok {
22+
list = l
23+
} else {
24+
list = make([]util.Point, 0)
25+
}
26+
antennaPoints[b] = append(list, util.Point{Y: y, X: x})
27+
}
28+
}
29+
}
30+
return antennaPoints
31+
}
32+
33+
func discoverAntinodes(antennaPoints map[byte][]util.Point, mapHeight int, mapWidth int) map[util.Point]util.Void {
34+
var antinodes = make(map[util.Point]util.Void)
35+
for _, points := range antennaPoints {
36+
for i, point := range points {
37+
for j, otherPoint := range points {
38+
if i == j {
39+
continue
40+
}
41+
var delta = point.Subtract(otherPoint)
42+
var antinode1 = point.Add(delta)
43+
if antinode1.IsInBounds(mapHeight, mapWidth) {
44+
antinodes[antinode1] = util.Void{}
45+
}
46+
var antinode2 = otherPoint.Subtract(delta)
47+
if antinode2.IsInBounds(mapHeight, mapWidth) {
48+
antinodes[antinode2] = util.Void{}
49+
}
50+
}
51+
}
52+
}
53+
return antinodes
54+
}
55+
56+
func main() {
57+
scanner := bufio.NewScanner(os.Stdin)
58+
scanner.Split(bufio.ScanLines)
59+
60+
var lines [][]byte
61+
for scanner.Scan() {
62+
lines = append(lines, scanner.Bytes())
63+
}
64+
65+
var mapHeight = len(lines)
66+
var mapWidth = len(lines[0])
67+
var antennaPoints = discoverAntennaPoints(lines)
68+
var antinodes = discoverAntinodes(antennaPoints, mapHeight, mapWidth)
69+
70+
fmt.Printf("unique antinodes: %d\n", len(antinodes))
71+
}

util/point.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package util
2+
3+
type Point struct {
4+
Y int
5+
X int
6+
}
7+
8+
func (p *Point) Add(o Point) Point {
9+
return Point{Y: p.Y + o.Y, X: p.X + o.X}
10+
}
11+
12+
func (p *Point) Subtract(o Point) Point {
13+
return Point{Y: p.Y - o.Y, X: p.X - o.X}
14+
}
15+
16+
func (p *Point) IsInBounds(height, width int) bool {
17+
if p.Y < 0 || p.Y >= height {
18+
return false
19+
}
20+
if p.X < 0 || p.X >= width {
21+
return false
22+
}
23+
return true
24+
}
25+
26+
func ByteAt(lines [][]byte, p Point) (byte, bool) {
27+
if p.Y < 0 || p.X < 0 {
28+
return 0, false
29+
}
30+
if p.Y >= len(lines) {
31+
return 0, false
32+
}
33+
if p.X >= len(lines[p.Y]) {
34+
return 0, false
35+
}
36+
return lines[p.Y][p.X], true
37+
}

util/util.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"strconv"
77
)
88

9+
type Void struct{}
10+
911
// Atoi converts string to integer, and exist immediately upon error
1012
func Atoi(s string) int {
1113
if i, err := strconv.Atoi(s); err != nil {

0 commit comments

Comments
 (0)