Skip to content

Commit

Permalink
2024 day 8 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
timredband committed Dec 23, 2024
1 parent 2010419 commit 21d8fd4
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 0 deletions.
10 changes: 10 additions & 0 deletions 2024/go/inputs/day8/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
..........
..........
..........
..........
..........
..........
..........
..........
........a.
........a.
12 changes: 12 additions & 0 deletions 2024/go/inputs/day8/example2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............
50 changes: 50 additions & 0 deletions 2024/go/inputs/day8/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
..................................w...............
..................................................
.....................................C............
......................................3...........
.............b.......u.........w...........3......
...........................u......................
...8........K...........u.......y.........I.......
..................................P5........B.....
...U................2.P...............B...C.......
............i......x.P........w......s.C......IB5.
..............t....................y....x.........
.......8............K....1.......w............u...
.................i.............r.........s........
..2.............t..T.K.......r......8..........I..
8..............t..T.....r...................5.....
...2........................1....M.t...N....x.....
...V....U.......................N.S..........I....
.........W...i............O....v............S.....
................L...1...s.fT.....x............3...
....6.......C...N.........Tf.3....................
...6.......F..........V..........k...N......H.....
...................U..f........0......H.y.........
.......................O...P......0...............
.......L..U....m.......R............s.............
6...i.................O....0.2.........H.....B....
........................R......H.........S........
......F.....c..........m..............d...........
................F.L.....m..................7......
.............J.........................S..........
.b....j.k..............V0.........................
.................L.....K..........................
.......F.......J..............r.....M.............
......................m1....a.R...7...............
.......4......Y..6.D..............................
k............9.......D................M...........
.....Xb.................V...h.....................
A.........9.Xl..........D......R..................
4.............c..d........D.............7.........
.A.4.............c.............M.7.v..............
..........n.9........................h............
...j....bd.........f.....p..W.....................
.............k.........p..........................
.......W4.......p......X.....5..J.....v...........
........W.d..c......A........n..v.....o...........
..........l.....n..........o......Y...h...........
.....A............................................
..j..........n....................................
a9.lX..................Y...........o..............
.......a.................Y..........o.............
...a....l.......................p.................
18 changes: 18 additions & 0 deletions 2024/go/pkg/day8/day8.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package day8

import (
"errors"
"fmt"
"os"
)

func Execute(part string, file *os.File) (int, error) {
switch part {
case "1":
return Part1(file), nil
case "2":
return Part2(file), nil
default:
return 0, errors.New(fmt.Sprintf("unknown part: %s", part))
}
}
74 changes: 74 additions & 0 deletions 2024/go/pkg/day8/part1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package day8

import (
"os"
"strings"

"github.com/timredband/advent-of-code/pkg/utils"
)

type Coordinate struct {
x int
y int
}

func Part1(file *os.File) int {
inputs := utils.ReadFile(file)
board := make([][]string, len(inputs))

for i, v := range inputs {
line := strings.Split(v, "")
board[i] = line
}

coordinatesByAntenna := make(map[string][]Coordinate)

for x, row := range board {
for y, val := range row {
if val == "." {
continue
}

if _, ok := coordinatesByAntenna[val]; !ok {
coordinatesByAntenna[val] = make([]Coordinate, 0)
}

coordinatesByAntenna[val] = append(coordinatesByAntenna[val], Coordinate{x, y})
}

}

antinodeCoordinates := make(map[Coordinate]struct{})

for _, coordinates := range coordinatesByAntenna {
i, j := 0, 1

for i < len(coordinates) {
for j < len(coordinates) {
xDiff := coordinates[i].x - coordinates[j].x
yDiff := coordinates[i].y - coordinates[j].y

firstAntiNode := Coordinate{x: coordinates[i].x + xDiff, y: coordinates[i].y + yDiff}
secondAntiNode := Coordinate{x: coordinates[j].x - xDiff, y: coordinates[j].y - yDiff}

if firstAntiNode.x >= 0 && firstAntiNode.x < len(board) && firstAntiNode.y >= 0 && firstAntiNode.y < len(board) {
antinodeCoordinates[firstAntiNode] = struct{}{}
}

if secondAntiNode.x >= 0 && secondAntiNode.x < len(board) && secondAntiNode.y >= 0 && secondAntiNode.y < len(board) {
antinodeCoordinates[secondAntiNode] = struct{}{}
}

j += 1
}

coordinates = coordinates[1:]

i, j = 0, 1
}
}

result := len(antinodeCoordinates)

return result
}
9 changes: 9 additions & 0 deletions 2024/go/pkg/day8/part2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package day8

import (
"os"
)

func Part2(file *os.File) int {
return 0
}
3 changes: 3 additions & 0 deletions 2024/go/pkg/days/days.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/timredband/advent-of-code/pkg/day5"
"github.com/timredband/advent-of-code/pkg/day6"
"github.com/timredband/advent-of-code/pkg/day7"
"github.com/timredband/advent-of-code/pkg/day8"
)

func Execute(day string, part string, file *os.File) (int, error) {
Expand All @@ -30,6 +31,8 @@ func Execute(day string, part string, file *os.File) (int, error) {
return day6.Execute(part, file)
case "7":
return day7.Execute(part, file)
case "8":
return day8.Execute(part, file)
default:
return 0, errors.New(fmt.Sprintf("unknown day: %s", day))
}
Expand Down

0 comments on commit 21d8fd4

Please sign in to comment.