diff --git a/d20/input.txt b/d20/input.txt new file mode 100644 index 0000000..8ba64a8 --- /dev/null +++ b/d20/input.txt @@ -0,0 +1 @@ +33100000 \ No newline at end of file diff --git a/d20/main.go b/d20/main.go new file mode 100644 index 0000000..0b056c9 --- /dev/null +++ b/d20/main.go @@ -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) +} diff --git a/util/math.go b/util/math.go index aa820a1..018e082 100644 --- a/util/math.go +++ b/util/math.go @@ -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)