Skip to content

Commit

Permalink
Day 20
Browse files Browse the repository at this point in the history
  • Loading branch information
Magiczne committed Nov 9, 2024
1 parent afdf040 commit 995f330
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions d20/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
33100000
50 changes: 50 additions & 0 deletions d20/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"aoc2015/util"
"math"
)

// Brute force is my best friend.
func part1(target int) int {
for houseNumber := range math.MaxInt32 {
giftCounter := 0

for _, factor := range util.FactorsOf(houseNumber) {
giftCounter += factor * 10
}

if giftCounter >= target {
return houseNumber
}
}

return -1
}

func part2(target int) int {
for houseNumber := range math.MaxInt32 {
giftCounter := 0

for _, factor := range util.FactorsOf(houseNumber) {
if houseNumber/factor <= 50 {
giftCounter += factor * 11
}
}

if giftCounter >= target {
return houseNumber
}
}

return -1
}

func main() {
reader := func(name string) int {
return util.StringToInt(util.ReadFile(name))
}

util.TestRuns("20", reader, part1, part2)
util.SolutionRuns("20", reader, part1, part2)
}
34 changes: 34 additions & 0 deletions util/math.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,43 @@
package util

import (
"math"

mapset "github.com/deckarep/golang-set/v2"
"golang.org/x/exp/constraints"
)

func FactorsOf(value int) []int {
factors := mapset.NewSet[int]()

for i := 1; i <= int(math.Sqrt(float64(value))); i++ {
if value%i == 0 {
factors.Add(i)
factors.Add(value / i)
}
}

return factors.ToSlice()
}

func GreatestCommonDivisor(a, b int) int {
for b != 0 {
a, b = b, a%b
}

return a
}

func LeastCommonMultiple(numbers ...int) int {
lcm := numbers[0]

for i := 1; i < len(numbers); i++ {
lcm = ((numbers[i] * lcm) / GreatestCommonDivisor(numbers[i], lcm))
}

return lcm
}

func Sum[T constraints.Integer | constraints.Float](numbers []T) T {
sum := T(0)

Expand Down

0 comments on commit 995f330

Please sign in to comment.