Day 8 part 1

main
kirbylife 2024-12-17 01:36:29 -06:00
parent 7a99f86e01
commit bc367374a0
1 changed files with 89 additions and 0 deletions

89
day-8/main.go 100644
View File

@ -0,0 +1,89 @@
package main
import (
"fmt"
"os"
"strings"
)
type coord struct {
x int
y int
}
func check(e error) {
if e != nil {
panic(e)
}
}
func generateMap(lines [][]byte) map[byte][]coord {
output := make(map[byte][]coord)
for y, line := range lines {
for x, char := range line {
if char == 46 {
continue
}
new_coord := coord{x: x, y: y}
output[char] = append(output[char], new_coord)
}
}
return output
}
func parseInput() [][]byte {
raw_data, err := os.ReadFile("input.txt")
check(err)
data := string(raw_data[:])
lines := strings.Split(data, "\n")
output := make([][]byte, len(lines)-1)
for n, line := range lines {
if len(line) > 0 {
output[n] = []byte(line)
}
}
return output
}
func part1(input [][]byte, charmap map[byte][]coord) {
counter := 0
for _, coords := range charmap {
for n1, coord1 := range coords {
for n2, coord2 := range coords {
if n1 == n2 {
continue
}
newY := coord1.y + (coord1.y - coord2.y)
newX := coord1.x + (coord1.x - coord2.x)
if newY < 0 || newY >= len(input) {
continue
}
if newX < 0 || newX >= len(input[0]) {
continue
}
if input[newY][newX] != '#' {
input[newY][newX] = '#'
counter += 1
}
}
}
}
// for _, line := range input {
// for _, char := range line {
// fmt.Print(string(char), " ")
// }
// fmt.Println()
// }
fmt.Println(counter)
}
func main() {
input := parseInput()
things := generateMap(input)
fmt.Println("Part 1:")
part1(input, things)
}