2023/03
This commit is contained in:
parent
99b6bf7a46
commit
53c44dc5d7
11
2023/03/go.mod
Normal file
11
2023/03/go.mod
Normal file
@ -0,0 +1,11 @@
|
||||
module zv0n/advent_of_code/2023/03
|
||||
|
||||
go 1.21.4
|
||||
|
||||
require (
|
||||
github.com/fatih/color v1.16.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/thoas/go-funk v0.9.3 // indirect
|
||||
golang.org/x/sys v0.14.0 // indirect
|
||||
)
|
19
2023/03/go.sum
Normal file
19
2023/03/go.sum
Normal file
@ -0,0 +1,19 @@
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
|
||||
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/thoas/go-funk v0.9.3 h1:7+nAEx3kn5ZJcnDm2Bh23N2yOtweO14bi//dvRtgLpw=
|
||||
github.com/thoas/go-funk v0.9.3/go.mod h1:+IWnUfUmFO1+WVYQWQtIJHeRRdaIyyYglZN7xzUPe4Q=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
|
||||
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
159
2023/03/main.go
Normal file
159
2023/03/main.go
Normal file
@ -0,0 +1,159 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"unicode"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/thoas/go-funk"
|
||||
)
|
||||
|
||||
type EnginePart struct {
|
||||
number uint64
|
||||
line uint64
|
||||
startIndex uint64
|
||||
endIndex uint64
|
||||
}
|
||||
|
||||
func readFileLines(filename string) ([]string, error) {
|
||||
var lines []string
|
||||
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return lines, err
|
||||
}
|
||||
fileScanner := bufio.NewScanner(file)
|
||||
fileScanner.Split(bufio.ScanLines)
|
||||
|
||||
for fileScanner.Scan() {
|
||||
lines = append(lines, fileScanner.Text())
|
||||
}
|
||||
|
||||
return lines, nil
|
||||
}
|
||||
|
||||
func getEngineParts(lines *[]string) []EnginePart {
|
||||
var result []EnginePart
|
||||
var tmpPart EnginePart
|
||||
for lineIdx := 0; lineIdx < len(*lines); lineIdx++ {
|
||||
line := &(*lines)[lineIdx]
|
||||
tmpPart.number = 0
|
||||
tmpPart.line = uint64(lineIdx)
|
||||
numStarted := false
|
||||
for charIdx, char := range *line {
|
||||
if unicode.IsDigit(char) {
|
||||
if !numStarted {
|
||||
tmpPart.startIndex = uint64(charIdx)
|
||||
numStarted = true
|
||||
}
|
||||
tmpPart.number *= 10
|
||||
tmpPart.number += uint64(char) - uint64('0')
|
||||
} else if numStarted {
|
||||
numStarted = false
|
||||
tmpPart.endIndex = uint64(charIdx) - 1
|
||||
result = append(result, tmpPart)
|
||||
tmpPart.number = 0
|
||||
}
|
||||
}
|
||||
if numStarted {
|
||||
tmpPart.endIndex = uint64(len(*line)) - 1
|
||||
result = append(result, tmpPart)
|
||||
tmpPart.number = 0
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func hasSymbol(line *string, startIndex uint64, endIndex uint64) bool {
|
||||
for i := startIndex; i <= endIndex; i++ {
|
||||
if (*line)[i] != '.' && !unicode.IsDigit(rune((*line)[i])) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func getValidEngineParts(parts *[]EnginePart, lines *[]string) []uint64 {
|
||||
var validParts []uint64
|
||||
for i := 0; i < len(*parts); i++ {
|
||||
part := &(*parts)[i]
|
||||
startIndex := part.startIndex
|
||||
endIndex := part.endIndex
|
||||
if startIndex > 0 {
|
||||
startIndex--
|
||||
}
|
||||
if endIndex < uint64(len((*lines)[0]))-1 {
|
||||
endIndex++
|
||||
}
|
||||
for i := part.line - 1; i != part.line+2; i++ {
|
||||
if i >= uint64(len(*lines)) {
|
||||
continue
|
||||
}
|
||||
if hasSymbol(&(*lines)[i], startIndex, endIndex) {
|
||||
validParts = append(validParts, part.number)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return validParts
|
||||
}
|
||||
|
||||
func hasTwoAdjecent(parts *[]EnginePart, lineIdx uint64, charIdx uint64) (bool, []EnginePart) {
|
||||
var result []EnginePart
|
||||
for i := 0; i < len(*parts); i++ {
|
||||
part := &(*parts)[i]
|
||||
if part.line >= lineIdx-1 && part.line <= lineIdx+1 {
|
||||
if part.startIndex <= charIdx+1 && part.endIndex >= charIdx-1 {
|
||||
result = append(result, *part)
|
||||
}
|
||||
}
|
||||
}
|
||||
return len(result) == 2, result
|
||||
}
|
||||
|
||||
func getGears(parts *[]EnginePart, lines *[]string) []uint64 {
|
||||
var result []uint64
|
||||
for lineIdx := 0; lineIdx < len(*lines); lineIdx++ {
|
||||
line := &((*lines)[lineIdx])
|
||||
for charIdx, char := range *line {
|
||||
if char == '*' {
|
||||
valid, engineParts := hasTwoAdjecent(parts, uint64(lineIdx), uint64(charIdx))
|
||||
if valid {
|
||||
result = append(result, engineParts[0].number*engineParts[1].number)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func main() {
|
||||
redPrint := color.New(color.FgRed)
|
||||
yellowPrint := color.New(color.FgYellow)
|
||||
if len(os.Args) < 2 {
|
||||
redPrint.Fprintln(os.Stderr, "You must provide the input file")
|
||||
os.Exit(1)
|
||||
}
|
||||
lines, err := readFileLines(os.Args[1])
|
||||
if err != nil {
|
||||
redPrint.Fprintln(os.Stderr, "Could not read input file")
|
||||
os.Exit(1)
|
||||
}
|
||||
engineParts := getEngineParts(&lines)
|
||||
|
||||
validParts := getValidEngineParts(&engineParts, &lines)
|
||||
validPartSum := funk.Reduce(validParts, func(acc, elem uint64) uint64 { return acc + elem }, uint64(0))
|
||||
|
||||
fmt.Print("Part 1: ")
|
||||
yellowPrint.Print(validPartSum)
|
||||
fmt.Println()
|
||||
|
||||
gearRatios := getGears(&engineParts, &lines)
|
||||
gearRatiosSum := funk.Reduce(gearRatios, func(acc, elem uint64) uint64 { return acc + elem }, uint64(0))
|
||||
|
||||
fmt.Print("Part 2: ")
|
||||
yellowPrint.Print(gearRatiosSum)
|
||||
fmt.Println()
|
||||
}
|
Loading…
Reference in New Issue
Block a user