-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1b87e37
commit 426046a
Showing
4 changed files
with
302 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ¤tPart) | ||
} | ||
|
||
} 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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ¤tPart) | ||
} | ||
|
||
} 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) | ||
} |
Oops, something went wrong.