Skip to content

Commit

Permalink
Add solutions for 2023 Day 3
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmckendry committed Dec 3, 2023
1 parent 1b87e37 commit 426046a
Show file tree
Hide file tree
Showing 4 changed files with 302 additions and 0 deletions.
96 changes: 96 additions & 0 deletions 2023/03p1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package main

import (
"fmt"
"strconv"

"aoc2023/utils"
)

type enginePart struct {
number int
isPart bool
partIdentifier rune
identifierPosition coordinate
}

type coordinate struct {
x int
y int
}

func D03P1() {
lines := utils.ReadLines("inputs/03.txt")
grid := make([][]rune, len(lines))

engineParts := []enginePart{}

for i, line := range lines {
grid[i] = []rune(line)
}

currentPart := enginePart{}
partNumberString := ""
for i, line := range grid {
for j, char := range line {
if char >= '0' && char <= '9' {
partNumberString += string(char)

if !currentPart.isPart {
checkIfPartIsPart(coordinate{j, i}, grid, &currentPart)
}

} else if partNumberString != "" {
currentPart.number, _ = strconv.Atoi(partNumberString)

engineParts = append(engineParts, currentPart)
partNumberString = ""
currentPart = enginePart{}
}
}
}

sumOfParts := 0
for _, part := range engineParts {
if part.isPart {
sumOfParts += part.number
}
}

fmt.Printf("Sum of parts: %d\n", sumOfParts)
}

func checkIfPartIsPart(position coordinate, grid [][]rune, currentPart *enginePart) {
neighbouringGridPoisitions := []coordinate{
{position.x + 1, position.y + 1},
{position.x + 1, position.y - 1},
{position.x + 1, position.y},
{position.x - 1, position.y + 1},
{position.x - 1, position.y - 1},
{position.x - 1, position.y},
{position.x, position.y + 1},
{position.x, position.y - 1},
}
for _, neighbour := range neighbouringGridPoisitions {
//Check if coordinate is in grid
if neighbour.x < 0 || neighbour.y < 0 || neighbour.x >= len(grid) ||
neighbour.y >= len(grid[0]) {
continue
}

// Check if the neighbour is a number
if grid[neighbour.y][neighbour.x] >= '0' && grid[neighbour.y][neighbour.x] <= '9' {
continue
}

// Check if the neighbour is a full stop
if grid[neighbour.y][neighbour.x] == '.' {
continue
}

currentPart.partIdentifier = grid[neighbour.y][neighbour.x]
currentPart.identifierPosition = neighbour
currentPart.isPart = true
return
}
}
64 changes: 64 additions & 0 deletions 2023/03p2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"fmt"
"strconv"

"aoc2023/utils"
)

func D03P2() {
lines := utils.ReadLines("inputs/03.txt")
grid := make([][]rune, len(lines))

engineParts := []enginePart{}

for i, line := range lines {
grid[i] = []rune(line)
}

currentPart := enginePart{}
partNumberString := ""
for i, line := range grid {
for j, char := range line {
if char >= '0' && char <= '9' {
partNumberString += string(char)

if !currentPart.isPart {
checkIfPartIsPart(coordinate{j, i}, grid, &currentPart)
}

} else if partNumberString != "" {
currentPart.number, _ = strconv.Atoi(partNumberString)

engineParts = append(engineParts, currentPart)
partNumberString = ""
currentPart = enginePart{}
}
}
}

// Using a map prevents duplicate gear ratios
gearRatios := map[coordinate]int{}

for _, part := range engineParts {
if part.partIdentifier == '*' {
// Check if the other half of the gear ratio is in the list
for _, otherPart := range engineParts {
if otherPart.partIdentifier == '*' &&
otherPart.identifierPosition == part.identifierPosition && otherPart.number != part.number {

// Add the gear ratio to the map
gearRatios[part.identifierPosition] = part.number * otherPart.number
}
}
}
}

totalSumOfGearRatios := 0
for _, gearRatio := range gearRatios {
totalSumOfGearRatios += gearRatio
}

fmt.Printf("Sum of gear ratios: %d\n", totalSumOfGearRatios)
}
Loading

0 comments on commit 426046a

Please sign in to comment.