This commit is contained in:
zv0n 2023-12-10 16:37:39 +01:00
parent 53c44dc5d7
commit ce22253dd8
4 changed files with 185 additions and 0 deletions

71
2023/04/cards.go Normal file
View File

@ -0,0 +1,71 @@
package main
import (
"slices"
"strconv"
"strings"
)
type Card struct {
id uint64
winning []uint64
guesses []uint64
correctGuesses int
}
func (c *Card) parseCard(input string) error {
initialSplit := strings.Split(input, ":")
idString := initialSplit[0][5:]
numberSplit := strings.Split(initialSplit[1], "|")
winningNumbers := strings.Split(numberSplit[0], " ")
guesses := strings.Split(numberSplit[1], " ")
var err error
c.id, err = strconv.ParseUint(strings.TrimSpace(idString), 10, 64)
if err != nil {
return err
}
for _, number := range winningNumbers {
number = strings.TrimSpace(number)
if len(number) == 0 {
continue
}
num, err := strconv.ParseUint(number, 10, 64)
if err != nil {
return err
}
c.winning = append(c.winning, num)
}
for _, number := range guesses {
number = strings.TrimSpace(number)
if len(number) == 0 {
continue
}
num, err := strconv.ParseUint(number, 10, 64)
if err != nil {
return err
}
c.guesses = append(c.guesses, num)
}
c.correctGuesses = 0
for _, guess := range c.guesses {
if slices.Contains(c.winning, guess) {
c.correctGuesses++
}
}
return nil
}
func (c *Card) getPoints() uint64 {
if c.correctGuesses == 0 {
return 0
}
var points uint64 = 1
for i := c.correctGuesses - 1; i > 0; i-- {
points *= 2
}
return points
}

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

@ -0,0 +1,11 @@
module zv0n/advent_of_code/2023/04
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/04/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=

84
2023/04/main.go Normal file
View File

@ -0,0 +1,84 @@
package main
import (
"bufio"
"container/list"
"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 getTotalAmountOfCards(cards []Card) uint64 {
var result uint64 = 0
cardStack := list.New()
cardStack.Init()
for _, card := range cards {
cardStack.PushBack(card.id)
}
for cardStack.Len() > 0 {
current := cardStack.Front()
id := current.Value.(uint64)
index := id - 1
if cards[index].correctGuesses > 0 {
for i := id; i < id+uint64(cards[index].correctGuesses); i++ {
cardStack.PushBack(cards[i].id)
}
}
result++
cardStack.Remove(current)
}
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)
}
var cards []Card
var points []uint64
for _, line := range lines {
cards = append(cards, Card{})
err := cards[len(cards)-1].parseCard(line)
if err != nil {
redPrint.Fprintf(os.Stderr, "Could not parse line '%s'\n", line)
}
points = append(points, cards[len(cards)-1].getPoints())
}
sumPart1 := funk.Reduce(points, func(acc, elem uint64) uint64 { return acc + elem }, uint64(0))
fmt.Print("Part 1: ")
yellowPrint.Print(sumPart1)
fmt.Println()
part2 := getTotalAmountOfCards(cards)
fmt.Print("Part 1: ")
yellowPrint.Print(part2)
fmt.Println()
}