This commit is contained in:
zv0n 2023-12-01 22:11:54 +01:00
parent 09860641b2
commit a3db58d124
4 changed files with 153 additions and 0 deletions

1
2023/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
input.txt

11
2023/01/go.mod Normal file
View File

@ -0,0 +1,11 @@
module zv0n/advent_of_code/2023/01
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/01/go.sum Normal file
View 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=

122
2023/01/main.go Normal file
View File

@ -0,0 +1,122 @@
package main
import (
"bufio"
"fmt"
"os"
"strings"
"unicode"
"github.com/fatih/color"
"github.com/thoas/go-funk"
)
var numberstrings = [9]string{"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}
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 getNumbersFromLines(lines []string) []int {
var result []int
for _, line := range lines {
firstnum := -1
lastnum := -1
for _, char := range line {
if unicode.IsDigit(rune(char)) {
if firstnum == -1 {
firstnum = int(char) - int('0')
} else {
lastnum = int(char) - int('0')
}
}
}
if lastnum == -1 {
lastnum = firstnum
}
result = append(result, firstnum*10+lastnum)
}
return result
}
func getNumbersFromLinesPart2(lines []string) []int {
var result []int
for _, line := range lines {
firstnum := -1
lastnum := -1
firstnumIndex := len(line)
lastnumIndex := 0
for idx, char := range line {
if unicode.IsDigit(rune(char)) {
if firstnum == -1 {
firstnum = int(char) - int('0')
firstnumIndex = idx
} else {
lastnum = int(char) - int('0')
lastnumIndex = idx
}
}
}
if lastnum == -1 {
lastnum = firstnum
lastnumIndex = firstnumIndex
}
for idx, num := range numberstrings {
stridx := strings.Index(line, num)
if stridx != -1 && stridx < firstnumIndex {
firstnum = idx + 1
firstnumIndex = stridx
}
stridx = strings.LastIndex(line, num)
if stridx > lastnumIndex {
lastnum = idx + 1
lastnumIndex = stridx
}
}
if lastnum == -1 {
lastnum = firstnum
}
result = append(result, firstnum*10+lastnum)
}
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)
}
numbers := getNumbersFromLines(lines)
sum := funk.Reduce(numbers, '+', 0)
fmt.Print("Part 1: ")
yellowPrint.Print(sum)
fmt.Println()
numbers2 := getNumbersFromLinesPart2(lines)
sum2 := funk.Reduce(numbers2, '+', 0)
fmt.Print("Part 2: ")
yellowPrint.Print(sum2)
fmt.Println()
}