2023/02
This commit is contained in:
parent
1b00f4f637
commit
99b6bf7a46
101
2023/02/cubes.go
Normal file
101
2023/02/cubes.go
Normal file
@ -0,0 +1,101 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Cube int
|
||||
|
||||
const (
|
||||
RED Cube = iota
|
||||
BLUE
|
||||
GREEN
|
||||
)
|
||||
|
||||
type CubeCount struct {
|
||||
color Cube
|
||||
count uint64
|
||||
}
|
||||
|
||||
type Game struct {
|
||||
id uint64
|
||||
sets [][]CubeCount
|
||||
maxRed uint64
|
||||
maxBlue uint64
|
||||
maxGreen uint64
|
||||
}
|
||||
|
||||
func parseColor(color string) Cube {
|
||||
if color == "red" {
|
||||
return RED
|
||||
}
|
||||
if color == "green" {
|
||||
return GREEN
|
||||
}
|
||||
return BLUE
|
||||
}
|
||||
|
||||
func (g *Game) parseGame(input string) error {
|
||||
initialSplit := strings.Split(input, ":")
|
||||
setSplit := strings.Split(initialSplit[1], ";")
|
||||
idString := initialSplit[0][5:]
|
||||
|
||||
var err error
|
||||
g.id, err = strconv.ParseUint(idString, 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, set := range setSplit {
|
||||
cubes := strings.Split(set, ",")
|
||||
var parsedSet []CubeCount
|
||||
|
||||
for _, cube := range cubes {
|
||||
var count uint64
|
||||
var color string
|
||||
fmt.Sscanf(strings.TrimSpace(cube), "%d %s", &count, &color)
|
||||
parsedSet = append(parsedSet, CubeCount{parseColor(color), count})
|
||||
}
|
||||
g.sets = append(g.sets, parsedSet)
|
||||
}
|
||||
g.maxBlue = 0
|
||||
g.maxGreen = 0
|
||||
g.maxRed = 0
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Game) resolveGame() {
|
||||
for _, set := range g.sets {
|
||||
for _, cubeCount := range set {
|
||||
if cubeCount.color == RED && cubeCount.count > g.maxRed {
|
||||
g.maxRed = cubeCount.count
|
||||
} else if cubeCount.color == GREEN && cubeCount.count > g.maxGreen {
|
||||
g.maxGreen = cubeCount.count
|
||||
} else if cubeCount.color == BLUE && cubeCount.count > g.maxBlue {
|
||||
g.maxBlue = cubeCount.count
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Game) canBePlayed(cubeSet [3]CubeCount) bool {
|
||||
for _, cubeCount := range cubeSet {
|
||||
if cubeCount.color == RED && cubeCount.count < g.maxRed {
|
||||
return false
|
||||
}
|
||||
if cubeCount.color == GREEN && cubeCount.count < g.maxGreen {
|
||||
return false
|
||||
}
|
||||
if cubeCount.color == BLUE && cubeCount.count < g.maxBlue {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (g *Game) getGamePower() uint64 {
|
||||
return g.maxBlue * g.maxGreen * g.maxRed
|
||||
}
|
11
2023/02/go.mod
Normal file
11
2023/02/go.mod
Normal file
@ -0,0 +1,11 @@
|
||||
module zv0n/advent_of_code/2023/02
|
||||
|
||||
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/02/go.sum
Normal file
19
2023/02/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=
|
70
2023/02/main.go
Normal file
70
2023/02/main.go
Normal file
@ -0,0 +1,70 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/thoas/go-funk"
|
||||
)
|
||||
|
||||
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 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)
|
||||
}
|
||||
var games []Game
|
||||
for _, line := range lines {
|
||||
games = append(games, Game{})
|
||||
games[len(games)-1].parseGame(line)
|
||||
games[len(games)-1].resolveGame()
|
||||
}
|
||||
|
||||
var part1Requirements = [3]CubeCount{{RED, 12}, {GREEN, 13}, {BLUE, 14}}
|
||||
var validIDsPart1 []uint64
|
||||
for _, game := range games {
|
||||
if game.canBePlayed(part1Requirements) {
|
||||
validIDsPart1 = append(validIDsPart1, game.id)
|
||||
}
|
||||
}
|
||||
|
||||
sumPart1 := funk.Reduce(validIDsPart1, func(acc, elem uint64) uint64 { return acc + elem }, uint64(0))
|
||||
fmt.Print("Part 1: ")
|
||||
yellowPrint.Print(sumPart1)
|
||||
fmt.Println()
|
||||
|
||||
var powersPart2 []uint64
|
||||
for _, game := range games {
|
||||
powersPart2 = append(powersPart2, game.getGamePower())
|
||||
}
|
||||
|
||||
sumPart2 := funk.Reduce(powersPart2, func(acc, elem uint64) uint64 { return acc + elem }, uint64(0))
|
||||
fmt.Print("Part 2: ")
|
||||
yellowPrint.Print(sumPart2)
|
||||
fmt.Println()
|
||||
}
|
Loading…
Reference in New Issue
Block a user