Compare commits

..

No commits in common. "e7796247ee62b90d1f8795a97ab945df3dc715d2" and "53c44dc5d700dfd724c371f1143d5ef41609c80b" have entirely different histories.

8 changed files with 0 additions and 463 deletions

View File

@ -1,71 +0,0 @@
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
}

View File

@ -1,11 +0,0 @@
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
)

View File

@ -1,19 +0,0 @@
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=

View File

@ -1,84 +0,0 @@
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()
}

View File

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

View File

@ -1,19 +0,0 @@
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=

View File

@ -1,218 +0,0 @@
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strconv"
"strings"
"unicode"
"github.com/fatih/color"
)
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 parseInput(lines []string) (Input, []uint64, error) {
var input Input
var seeds []uint64
var seedMap, soilMap, fertilizerMap, waterMap, lightMap, temperatureMap, humidityMap bool
seedMap = false
soilMap = false
fertilizerMap = false
waterMap = false
lightMap = false
temperatureMap = false
humidityMap = false
for idx, line := range lines {
if idx == 0 {
numbers := strings.Split(strings.TrimSpace(strings.Split(line, ":")[1]), " ")
for _, num := range numbers {
seed, err := strconv.ParseUint(num, 10, 64)
if err != nil {
return input, seeds, err
}
seeds = append(seeds, seed)
}
} else {
if len(line) == 0 {
seedMap = false
soilMap = false
fertilizerMap = false
waterMap = false
lightMap = false
temperatureMap = false
humidityMap = false
} else if unicode.IsDigit(rune(line[0])) {
numbers := strings.Split(line, " ")
var mapper Mapper
source, err := strconv.ParseUint(numbers[1], 10, 64)
if err != nil {
return input, seeds, err
}
target, err := strconv.ParseUint(numbers[0], 10, 64)
if err != nil {
return input, seeds, err
}
count, err := strconv.ParseUint(numbers[2], 10, 64)
if err != nil {
return input, seeds, err
}
mapper.source = source
mapper.target = target
mapper.count = count
if seedMap {
input.seedToSoil = append(input.seedToSoil, mapper)
} else if soilMap {
input.soilToFertilizer = append(input.soilToFertilizer, mapper)
} else if fertilizerMap {
input.fertilizerToWater = append(input.fertilizerToWater, mapper)
} else if waterMap {
input.waterToLight = append(input.waterToLight, mapper)
} else if lightMap {
input.lightToTemperature = append(input.lightToTemperature, mapper)
} else if temperatureMap {
input.temperatureToHumidity = append(input.temperatureToHumidity, mapper)
} else if humidityMap {
input.humidityToLocation = append(input.humidityToLocation, mapper)
}
} else {
if line[:4] == "seed" {
seedMap = true
} else if line[:4] == "soil" {
soilMap = true
} else if line[:4] == "fert" {
fertilizerMap = true
} else if line[:4] == "wate" {
waterMap = true
} else if line[:4] == "ligh" {
lightMap = true
} else if line[:4] == "temp" {
temperatureMap = true
} else if line[:4] == "humi" {
humidityMap = true
}
}
}
}
sort.Sort(MapperSort(input.seedToSoil))
sort.Sort(MapperSort(input.soilToFertilizer))
sort.Sort(MapperSort(input.fertilizerToWater))
sort.Sort(MapperSort(input.waterToLight))
sort.Sort(MapperSort(input.lightToTemperature))
sort.Sort(MapperSort(input.temperatureToHumidity))
sort.Sort(MapperSort(input.humidityToLocation))
return input, seeds, nil
}
func calculateIntervals(intervals [][2]uint64, mapper []Mapper) [][2]uint64 {
var newIntervals [][2]uint64
for _, interval := range intervals {
finishedInterval := false
for _, mapper := range mapper {
if finishedInterval {
break
}
if interval[0] >= mapper.source && interval[0] < mapper.source+mapper.count {
parseInterval := interval
if interval[1] >= mapper.source+mapper.count {
parseInterval[1] = mapper.source + mapper.count - 1
interval[0] = mapper.source + mapper.count
} else {
finishedInterval = true
}
newIntervals = append(newIntervals, [2]uint64{parseInterval[0] - mapper.source + mapper.target, parseInterval[1] - mapper.source + mapper.target})
} else if interval[0] < mapper.source {
if interval[1] >= mapper.source {
newIntervals = append(newIntervals, [2]uint64{interval[0], mapper.source - 1})
interval = [2]uint64{mapper.source, interval[1]}
} else {
newIntervals = append(newIntervals, interval)
finishedInterval = true
}
}
}
if !finishedInterval {
newIntervals = append(newIntervals, interval)
}
}
sort.Sort(IntervalSort(newIntervals))
var returnIntervals [][2]uint64
for _, interval := range newIntervals {
if len(returnIntervals) == 0 {
returnIntervals = append(returnIntervals, interval)
}
if returnIntervals[len(returnIntervals)-1][1] == interval[0] {
returnIntervals[len(returnIntervals)-1][1] = interval[1]
} else {
returnIntervals = append(returnIntervals, interval)
}
}
return returnIntervals
}
func parseAlmanacTest(input Input) [][2]uint64 {
intervals := input.seedIntervals
intervals = calculateIntervals(intervals, input.seedToSoil)
intervals = calculateIntervals(intervals, input.soilToFertilizer)
intervals = calculateIntervals(intervals, input.fertilizerToWater)
intervals = calculateIntervals(intervals, input.waterToLight)
intervals = calculateIntervals(intervals, input.lightToTemperature)
intervals = calculateIntervals(intervals, input.temperatureToHumidity)
intervals = calculateIntervals(intervals, input.humidityToLocation)
return intervals
}
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)
}
input, seeds, err := parseInput(lines)
if err != nil {
redPrint.Fprintln(os.Stderr, err)
}
for _, seed := range seeds {
input.seedIntervals = append(input.seedIntervals, [2]uint64{seed, seed})
}
sort.Sort(IntervalSort(input.seedIntervals))
locationsNew := parseAlmanacTest(input)
fmt.Print("Part 1: ")
yellowPrint.Print(locationsNew[0][0])
fmt.Println()
input.seedIntervals = [][2]uint64{}
for i := 0; i < len(seeds); i += 2 {
input.seedIntervals = append(input.seedIntervals, [2]uint64{seeds[i], seeds[i] + seeds[i+1] - 1})
}
sort.Sort(IntervalSort(input.seedIntervals))
locationsNew2 := parseAlmanacTest(input)
fmt.Print("Part 2: ")
yellowPrint.Print(locationsNew2[0][0])
fmt.Println()
}

View File

@ -1,30 +0,0 @@
package main
type IntervalSort [][2]uint64
func (is IntervalSort) Len() int { return len(is) }
func (is IntervalSort) Swap(i, j int) { is[i], is[j] = is[j], is[i] }
func (is IntervalSort) Less(i, j int) bool { return is[i][0] < is[j][0] }
type MapperSort []Mapper
func (ms MapperSort) Len() int { return len(ms) }
func (ms MapperSort) Swap(i, j int) { ms[i], ms[j] = ms[j], ms[i] }
func (ms MapperSort) Less(i, j int) bool { return ms[i].source < ms[j].source }
type Input struct {
seedIntervals [][2]uint64
seedToSoil []Mapper
soilToFertilizer []Mapper
fertilizerToWater []Mapper
waterToLight []Mapper
lightToTemperature []Mapper
temperatureToHumidity []Mapper
humidityToLocation []Mapper
}
type Mapper struct {
source uint64
target uint64
count uint64
}