2020-day15 so close :( 600/209. difficult to just understand the prompt. brute forced a dictionary for pt 2

2017-day10 weird fake hashing, xor and hexadecimals
This commit is contained in:
alexchao26
2020-12-14 23:58:27 -05:00
parent 0bc9afb7e0
commit 1e2a250acd
4 changed files with 211 additions and 46 deletions
+37 -22
View File
@@ -15,32 +15,47 @@ func main() {
flag.Parse()
fmt.Println("Running part", part)
var ans int
if part == 1 {
ans := part1(util.ReadFile("./input.txt"))
util.CopyToClipboard(fmt.Sprintf("%v", ans))
fmt.Println("Output:", ans)
ans = rambunctiousRecitation(util.ReadFile("./input.txt"), 2020)
} else {
ans := part2(util.ReadFile("./input.txt"))
util.CopyToClipboard(fmt.Sprintf("%v", ans))
fmt.Println("Output:", ans)
// brute force, takes ~5seconds to run
ans = rambunctiousRecitation(util.ReadFile("./input.txt"), 30000000)
}
fmt.Println("Output:", ans)
}
func part1(input string) int {
parsed := parseInput(input)
_ = parsed
return 0
}
func part2(input string) int {
return 0
}
func parseInput(input string) (ans []int) {
lines := strings.Split(input, "\n")
for _, l := range lines {
ans = append(ans, mathutil.StrToInt(l))
func rambunctiousRecitation(input string, turnToReturn int) int {
var startingNums []int
for _, num := range strings.Split(input, ",") {
startingNums = append(startingNums, mathutil.StrToInt(num))
}
return ans
said := map[int][]int{}
var numSaidLast int
// populate the starting map with the numbers from the input
for i, num := range startingNums {
said[num] = append(said[num], i+1)
numSaidLast = num
}
// then picking up from the next turn (len of input), continue until the
// desired turn is reached
for i := len(startingNums); i <= turnToReturn; i++ {
indexSlice := said[numSaidLast]
// if the length of the slice of incides is 1, that means it was only
// called once. Say zero, add to the zero slice of indices
if len(indexSlice) == 1 {
numSaidLast = 0
said[0] = append(said[0], i)
} else {
// otherwise determine the diff between the last 2 times it was said
length := len(indexSlice)
numSaidLast = indexSlice[length-1] - indexSlice[length-2]
// add this index i to the indices slice for the number that is said
said[numSaidLast] = append(said[numSaidLast], i)
}
}
return numSaidLast
}