-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/believer/aoc-2024/utils" | ||
"github.com/believer/aoc-2024/utils/files" | ||
) | ||
|
||
func main() { | ||
fmt.Println("Part 1: ", part1("input.txt")) | ||
fmt.Println("Part 2: ", part2("input.txt")) | ||
} | ||
|
||
func part1(name string) (total int) { | ||
buyers := files.ReadLines(name) | ||
|
||
for _, buyer := range buyers { | ||
secret := utils.MustIntFromString(buyer) | ||
|
||
for range 2000 { | ||
secret = generateSecret(secret) | ||
} | ||
|
||
total += secret | ||
} | ||
|
||
return total | ||
} | ||
|
||
type Pattern [4]int | ||
|
||
func part2(name string) (maxBananas int) { | ||
buyers := files.ReadLines(name) | ||
scores := map[Pattern]int{} | ||
|
||
for _, buyer := range buyers { | ||
secret := utils.MustIntFromString(buyer) | ||
patterns := [][2]int{} | ||
seen := map[Pattern]bool{} | ||
last := secret % 10 | ||
|
||
// Generate all secrets and their deltas | ||
for range 2000 { | ||
secret = generateSecret(secret) | ||
temp := secret % 10 | ||
patterns = append(patterns, [2]int{temp - last, temp}) | ||
last = temp | ||
} | ||
|
||
// Go through the patterns four by four and them to the total score | ||
for i := range len(patterns) - 4 { | ||
key := Pattern{ | ||
patterns[i][0], | ||
patterns[i+1][0], | ||
patterns[i+2][0], | ||
patterns[i+3][0], | ||
} | ||
|
||
if _, ok := seen[key]; !ok { | ||
seen[key] = true | ||
scores[key] += patterns[i+3][1] | ||
} | ||
} | ||
} | ||
|
||
for _, b := range scores { | ||
maxBananas = max(maxBananas, b) | ||
} | ||
|
||
return maxBananas | ||
} | ||
|
||
func generateSecret(secret int) int { | ||
prune := 16777216 | ||
|
||
// Step 1 | ||
result := secret * 64 | ||
secret ^= result | ||
secret %= prune | ||
|
||
// Step 2 | ||
result = secret / 32 | ||
secret ^= result | ||
secret %= prune | ||
|
||
// Step 3 | ||
result = secret * 2048 | ||
secret ^= result | ||
secret %= prune | ||
|
||
return secret | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPart1(t *testing.T) { | ||
t.Run("Part 1", func(t *testing.T) { | ||
expected := 37327623 | ||
actual := part1("test-input.txt") | ||
assert.Equal(t, expected, actual) | ||
}) | ||
} | ||
|
||
func TestPart2(t *testing.T) { | ||
t.Run("Part 2", func(t *testing.T) { | ||
expected := 23 | ||
actual := part2("test-input-part2.txt") | ||
assert.Equal(t, expected, actual) | ||
}) | ||
} | ||
|
||
func BenchmarkPart1(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
part1("input.txt") | ||
} | ||
} | ||
|
||
func BenchmarkPart2(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
part2("input.txt") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
1 | ||
2 | ||
3 | ||
2024 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
1 | ||
10 | ||
100 | ||
2024 |