advent-of-code-2023/day3/main.go

229 lines
5.6 KiB
Go
Raw Normal View History

2023-12-03 15:49:48 +00:00
package main
import (
2023-12-03 16:38:08 +00:00
"errors"
2023-12-03 15:49:48 +00:00
"fmt"
"io"
"os"
"strconv"
"strings"
"unicode"
)
type Coordinate struct {
row int
col int
}
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: %s inputfile\n", os.Args[0])
os.Exit(1)
}
filename := os.Args[1]
inputFile, err := os.Open(filename)
if err != nil {
panic(fmt.Sprintf("failed to open input file: %s", err))
}
inputBytes, err := io.ReadAll(inputFile)
if err != nil {
panic(fmt.Sprintf("failed to read input file: %s", err))
}
input := string(inputBytes)
inputLines := strings.Split(strings.TrimSpace(input), "\n")
fmt.Printf("Part 1: %d\n", part1(inputLines))
2023-12-03 16:38:08 +00:00
fmt.Printf("Part 2: %d\n", part2(inputLines))
2023-12-03 15:49:48 +00:00
}
func part1(inputLines []string) int {
2023-12-03 16:38:08 +00:00
symbolPositions := findParts(inputLines, isSymbol)
2023-12-03 15:49:48 +00:00
partNumberCandidates := []Coordinate{}
for _, symbolPos := range symbolPositions {
candidates := findNumbersAdjacentTo(inputLines, symbolPos)
partNumberCandidates = append(partNumberCandidates, candidates...)
}
total := 0
2023-12-03 16:38:08 +00:00
// keep track of digits we've already scanned; it's possible one number
// is adjacent to two parts, and we don't wanna double count
2023-12-03 15:49:48 +00:00
scanned := map[Coordinate]struct{}{}
for _, candidate := range partNumberCandidates {
if _, ok := scanned[candidate]; ok {
continue
}
2023-12-03 16:38:08 +00:00
scannedNumber, scannedDigits, err := scanPartNumber(inputLines, candidate)
if err != nil {
panic(fmt.Sprintf("candidate was invalid: %s", err))
}
2023-12-03 15:49:48 +00:00
2023-12-03 16:38:08 +00:00
for _, scannedDigit := range scannedDigits {
scanned[scannedDigit] = struct{}{}
2023-12-03 15:49:48 +00:00
}
2023-12-03 16:38:08 +00:00
total += scannedNumber
}
2023-12-03 15:49:48 +00:00
2023-12-03 16:38:08 +00:00
return total
}
2023-12-03 15:49:48 +00:00
2023-12-03 16:38:08 +00:00
func part2(inputLines []string) int {
gearPositions := findParts(inputLines, isGear)
totalRatio := 0
for _, gearPos := range gearPositions {
partNumbers, err := scanForGearPartNumbers(inputLines, gearPos)
2023-12-03 15:49:48 +00:00
if err != nil {
2023-12-03 16:38:08 +00:00
panic(fmt.Sprintf("gear scan failed: %s", err))
2023-12-03 15:49:48 +00:00
}
2023-12-03 16:38:08 +00:00
if len(partNumbers) == 2 {
totalRatio += partNumbers[0] * partNumbers[1]
}
2023-12-03 15:49:48 +00:00
}
2023-12-03 16:38:08 +00:00
return totalRatio
2023-12-03 15:49:48 +00:00
}
2023-12-03 16:38:08 +00:00
// findParts will find all parts in the input, using the given isPart function to tell
// if a character is a valid part.
func findParts(inputLines []string, isPart func(rune) bool) []Coordinate {
2023-12-03 15:49:48 +00:00
coords := []Coordinate{}
for row, line := range inputLines {
for col, char := range line {
2023-12-03 16:38:08 +00:00
if isPart(char) {
2023-12-03 15:49:48 +00:00
coord := Coordinate{
row: row,
col: col,
}
coords = append(coords, coord)
}
}
}
return coords
}
2023-12-03 16:38:08 +00:00
// findNumbersAdjacentTo will find all the digit characters adjacent to a given coordinate
2023-12-03 15:49:48 +00:00
func findNumbersAdjacentTo(inputLines []string, coordinate Coordinate) []Coordinate {
coords := []Coordinate{}
for dRow := -1; dRow <= 1; dRow++ {
for dCol := -1; dCol <= 1; dCol++ {
if dRow == 0 && dCol == 0 {
continue
}
row := coordinate.row + dRow
col := coordinate.col + dCol
if unicode.IsDigit(rune(inputLines[row][col])) {
coord := Coordinate{
row: row,
col: col,
}
coords = append(coords, coord)
}
}
}
return coords
}
2023-12-03 16:38:08 +00:00
// scanPartNumber takes an initial digit character and attempts to complete it by scanning left and right from that
// position. Returns the found part number and the positions scanned.
func scanPartNumber(inputLines []string, knownDigitPosition Coordinate) (int, []Coordinate, error) {
if !unicode.IsDigit(rune(inputLines[knownDigitPosition.row][knownDigitPosition.col])) {
return 0, nil, errors.New("position was not a numeric char")
}
line := inputLines[knownDigitPosition.row]
scanned := []Coordinate{}
backwardsBuf := strings.Builder{}
// Scan backward
for i := knownDigitPosition.col - 1; i >= 0; i-- {
char := line[i]
if !unicode.IsDigit(rune(char)) {
break
}
scanned = append(scanned, Coordinate{row: knownDigitPosition.row, col: i})
backwardsBuf.WriteByte(char)
}
forwardsBuf := strings.Builder{}
// Scan forward
for i := knownDigitPosition.col; i < len(line); i++ {
char := line[i]
if !unicode.IsDigit(rune(char)) {
break
}
scanned = append(scanned, Coordinate{row: knownDigitPosition.row, col: i})
forwardsBuf.WriteByte(char)
}
fullNumber := reverseString(backwardsBuf.String()) + forwardsBuf.String()
scannedNumber, err := strconv.Atoi(fullNumber)
if err != nil {
panic(fmt.Sprintf("%s was not a number", fullNumber))
}
return scannedNumber, scanned, nil
}
// scanForGearPartNumbers will find all part numbers adjacent to the given gear character position
func scanForGearPartNumbers(inputLines []string, gearPosition Coordinate) ([]int, error) {
if !isGear(rune(inputLines[gearPosition.row][gearPosition.col])) {
return nil, errors.New("position is not gear")
}
candidates := findNumbersAdjacentTo(inputLines, gearPosition)
// Keep track of the digits scanned, as it is possible for a single part number to be adjacent
// to the gear in multiple places
scanned := map[Coordinate]struct{}{}
adjacentPartNumbers := []int{}
for _, candidate := range candidates {
if _, ok := scanned[candidate]; ok {
continue
}
scannedNumber, scannedDigits, err := scanPartNumber(inputLines, candidate)
if err != nil {
// Strictly a programmer error, as this would imply that findNumbersAdjacentTo
// didn't find us a number
panic(fmt.Sprintf("candidate was invalid: %s", err))
}
for _, scannedDigit := range scannedDigits {
scanned[scannedDigit] = struct{}{}
}
adjacentPartNumbers = append(adjacentPartNumbers, scannedNumber)
}
return adjacentPartNumbers, nil
}
2023-12-03 15:49:48 +00:00
func isSymbol(r rune) bool {
return r != '.' && !unicode.IsDigit(r)
}
2023-12-03 16:38:08 +00:00
func isGear(r rune) bool {
return r == '*'
}
2023-12-03 15:49:48 +00:00
func reverseString(s string) string {
buffer := strings.Builder{}
for i := len(s) - 1; i >= 0; i-- {
buffer.WriteByte(s[i])
}
return buffer.String()
}