Skip to content

Commit

Permalink
2023-04
Browse files Browse the repository at this point in the history
  • Loading branch information
dbut2 committed Dec 4, 2023
1 parent 61fea65 commit a952190
Show file tree
Hide file tree
Showing 6 changed files with 468 additions and 0 deletions.
72 changes: 72 additions & 0 deletions 2023/04/01.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main

import (
"embed"
_ "embed"
"fmt"
"strings"

"github.com/dbut2/advent-of-code/pkg/lists"
"github.com/dbut2/advent-of-code/pkg/math"
"github.com/dbut2/advent-of-code/pkg/sti"
"github.com/dbut2/advent-of-code/pkg/test"
"github.com/dbut2/advent-of-code/pkg/utils"
)

//go:embed input.txt
var input string

//go:embed test*.txt
var tests embed.FS

func main() {
t := test.Register(tests, solve)
t.Expect(1, 13)
fmt.Println(solve(input))
}

func solve(input string) int {
s := utils.ParseInput(input)

//Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53

total := 0

for _, line := range s {
game := strings.Split(line, ": ")

games := strings.Split(game[1], "|")

left := strings.Split(games[0], " ")
right := strings.Split(games[1], " ")

leftNumbers := []int{}
for _, n := range left {
if n == "" {
continue
}
n = strings.TrimSpace(n)
leftNumbers = append(leftNumbers, sti.Sti(n))
}

rightNumbers := []int{}
for _, n := range right {
if n == "" {
continue
}
n = strings.TrimSpace(n)
rightNumbers = append(rightNumbers, sti.Sti(n))
}

matches := len(lists.Intersection(leftNumbers, rightNumbers))

if matches == 0 {
continue
}

total += math.Pow(2, matches-1)
}

return total

}
77 changes: 77 additions & 0 deletions 2023/04/02.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package main

import (
"embed"
_ "embed"
"fmt"
"strings"

"github.com/dbut2/advent-of-code/pkg/lists"
"github.com/dbut2/advent-of-code/pkg/sti"
"github.com/dbut2/advent-of-code/pkg/test"
"github.com/dbut2/advent-of-code/pkg/utils"
)

//go:embed input.txt
var input string

//go:embed test*.txt
var tests embed.FS

func main() {
t := test.Register(tests, solve)
t.Expect(2, 30)
fmt.Println(solve(input))
}

func solve(input string) int {
s := utils.ParseInput(input)

//Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53

total := 0
multis := map[int]int{}

for i := range s {
multis[i] = 1
}

for i, line := range s {
game := strings.Split(line, ": ")

games := strings.Split(game[1], "|")

left := strings.Split(games[0], " ")
right := strings.Split(games[1], " ")

leftNumbers := []int{}
for _, n := range left {
if n == "" {
continue
}
n = strings.TrimSpace(n)
leftNumbers = append(leftNumbers, sti.Sti(n))
}

rightNumbers := []int{}
for _, n := range right {
if n == "" {
continue
}
n = strings.TrimSpace(n)
rightNumbers = append(rightNumbers, sti.Sti(n))
}

matches := lists.Intersection(leftNumbers, rightNumbers)

multiplier := multis[i]

total += multiplier

for j, _ := range matches {
multis[i+j+1] += multiplier
}
}

return total
}
Loading

0 comments on commit a952190

Please sign in to comment.