Skip to content

Commit

Permalink
day 08 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
aleche28 committed Dec 8, 2024
1 parent c52ad9b commit 4aede2e
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 0 deletions.
112 changes: 112 additions & 0 deletions solutions/alessio/day08/day08.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package main

import (
"fmt"
"os"
"strings"
)

func check(e error) {
if e != nil {
panic(e)
}
}

type Cell struct {
r, c int
}

func checkInBounds(cell Cell, rows int, cols int) bool {
return cell.r >= 0 && cell.r < rows && cell.c >= 0 && cell.c < cols
}

func part1(lines []string) {
rows, cols := len(lines), len(lines[0])
antennas := make(map[byte][]Cell)
for r := range rows {
for c := range cols {
if lines[r][c] != '.' {
antennas[lines[r][c]] = append(antennas[lines[r][c]], Cell{r, c})
}
}
}

antinodes := make(map[Cell]bool)
for _, places := range antennas {
for i := range len(places) {
for j := i + 1; j < len(places); j++ {
p1, p2 := places[i], places[j]
dr, dc := p2.r-p1.r, p2.c-p1.c
antinode1 := Cell{p2.r + dr, p2.c + dc}
if checkInBounds(antinode1, rows, cols) {
antinodes[antinode1] = true
}
antinode2 := Cell{p1.r - dr, p1.c - dc}
if checkInBounds(antinode2, rows, cols) {
antinodes[antinode2] = true
}
}
}
}

fmt.Println(len(antinodes))
}

func part2(lines []string) {
rows, cols := len(lines), len(lines[0])
antennas := make(map[byte][]Cell)
for r := range rows {
for c := range cols {
if lines[r][c] != '.' {
antennas[lines[r][c]] = append(antennas[lines[r][c]], Cell{r, c})
}
}
}

antinodes := make(map[Cell]bool)
for _, places := range antennas {
for i := range len(places) {
for j := i + 1; j < len(places); j++ {
p1, p2 := places[i], places[j]
// mark also antennas as antinodes
antinodes[p1] = true
antinodes[p2] = true

dr, dc := p2.r-p1.r, p2.c-p1.c
cnt := 1
for {
antinode := Cell{p2.r + cnt*dr, p2.c + cnt*dc}
if checkInBounds(antinode, rows, cols) {
antinodes[antinode] = true
cnt++
} else {
break
}
}

cnt = 1
for {
antinode := Cell{p1.r - cnt*dr, p1.c - cnt*dc}
if checkInBounds(antinode, rows, cols) {
antinodes[antinode] = true
cnt++
} else {
break
}
}
}
}
}

fmt.Println(len(antinodes))
}

func main() {
data, err := os.ReadFile("./input08.txt")
check(err)

lines := strings.Split(strings.Trim(string(data), "\n"), "\n")

part1(lines)
part2(lines)
}
3 changes: 3 additions & 0 deletions solutions/alessio/day08/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module aoc8

go 1.23.3
50 changes: 50 additions & 0 deletions solutions/alessio/day08/input08.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
.AU..Z.....8.......................t..C.6.........
..................................................
.....K.U....................v.....................
...Z..A.............................v8.....t......
p..................a8...........b...t.............
..pU.....A..4.....................................
..........................E.......................
...........K..V..............v8.....Cb............
....V................b...p........................
....7.............................................
....4.....A..........V......K..E.....6............
.4.........................Vb...........0..C......
..................................k........N......
K....7...........9...........6.....kE.............
......7......1...................k.......C........
...p....................9....0.....N6.............
..........Z........e..1...........................
.............................E................N...
...4...............................v0..........z..
........U.....Z......1................z..a........
.....5.......7......................N.............
....................n.............................
.......................0.9...c..........z.d.T.....
...................n.W......a...t......D....d.....
..........I.....e......................o9.........
....5..2................e...........D.............
...........................n......D...............
......25I...1..................c......W.......o...
................n..............D..................
...........I........i..e..........................
......5......2.....P..............a...............
...........................z..................T...
..........j.....................Wd...........O..o.
................................................c.
.................I................B...............
...........u.............................T.d......
.............................................J....
.....3.i....u......................o..............
3...i.............................................
..................................................
...........j...............W....O............w....
...P........................J.....................
.....u............................................
.............................w....................
......u.................2...w...J.................
.....j.....B3......................O..............
P....B..............................c.............
................B.............w...................
.....i.............3..............................
..P.j....................J..........O.............

0 comments on commit 4aede2e

Please sign in to comment.