mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-05-19 03:23:27 +02:00
2015-day20: slow-ish solution for infinite elves delivering to infinite houses
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"github.com/alexchao26/advent-of-code-go/cast"
|
||||
|
||||
"github.com/alexchao26/advent-of-code-go/util"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var part int
|
||||
flag.IntVar(&part, "part", 1, "part 1 or 2")
|
||||
flag.Parse()
|
||||
fmt.Println("Running part", part)
|
||||
|
||||
ans := infiniteElvesAndHouses(util.ReadFile("./input.txt"), part)
|
||||
fmt.Println("Output:", ans)
|
||||
}
|
||||
|
||||
func infiniteElvesAndHouses(input string, part int) int {
|
||||
targetNum := cast.ToInt(input)
|
||||
|
||||
for house := 1; house < math.MaxInt32; house++ {
|
||||
var gifts int
|
||||
for _, factor := range getFactors(house) {
|
||||
if part == 1 {
|
||||
gifts += factor * 10
|
||||
} else if part == 2 && house/factor <= 50 {
|
||||
// for part 2, ensure that this is the 50th or less house that
|
||||
// the elf has visited before adding gifts
|
||||
gifts += factor * 11
|
||||
}
|
||||
}
|
||||
if gifts >= targetNum {
|
||||
return house
|
||||
}
|
||||
}
|
||||
|
||||
panic("expect return from loop")
|
||||
}
|
||||
|
||||
func getFactors(num int) []int {
|
||||
var factors []int
|
||||
sqrt := int(math.Sqrt(float64(num)))
|
||||
for i := 1; i <= sqrt; i++ {
|
||||
if num%i == 0 {
|
||||
factors = append(factors, i, num/i)
|
||||
}
|
||||
}
|
||||
return factors
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/alexchao26/advent-of-code-go/util"
|
||||
)
|
||||
|
||||
func Test_infiniteElvesAndHouses(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
part int
|
||||
want int
|
||||
}{
|
||||
{"part1 actual", util.ReadFile("input.txt"), 1, 776160},
|
||||
{"part2 actual", util.ReadFile("input.txt"), 2, 786240},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := infiniteElvesAndHouses(tt.input, tt.part); got != tt.want {
|
||||
t.Errorf("infiniteElvesAndHouses() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user