Skip to content

Commit 45f1de8

Browse files
committed
Solution for 2024, day 22
1 parent d671ab6 commit 45f1de8

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

2024/22/Makefile

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
input:
2+
http "https://adventofcode.com/2024/day/22/input" "Cookie:session=${AOC_SESSION};" >input
3+
4+
main1:
5+
go build -o main1 main1.go common.go
6+
7+
main2:
8+
go build -o main2 main2.go common.go
9+
10+
.PHONY: run1 run2 clean
11+
12+
run1: main1 input
13+
./main1 <input
14+
15+
run2: main2 input
16+
./main2 <input
17+
18+
clean:
19+
rm -f main1 main2 input
20+

2024/22/common.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"os"
6+
"strconv"
7+
)
8+
9+
func parseInput() []int {
10+
var list []int
11+
scanner := bufio.NewScanner(os.Stdin)
12+
for scanner.Scan() {
13+
line := scanner.Text()
14+
num, _ := strconv.Atoi(line)
15+
list = append(list, num)
16+
}
17+
return list
18+
}
19+
20+
func compute(secret int) int {
21+
secret = (secret ^ (secret << 6)) % 16777216
22+
secret = (secret ^ (secret >> 5)) % 16777216
23+
secret = (secret ^ (secret << 11)) % 16777216
24+
return secret
25+
}

2024/22/main1.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
secrets := parseInput()
7+
8+
sum := 0
9+
for _, secret := range secrets {
10+
for i := 0; i < 2000; i++ {
11+
secret = compute(secret)
12+
}
13+
sum += secret
14+
}
15+
16+
fmt.Println(sum)
17+
}

2024/22/main2.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type Changes [4]int
6+
7+
func main() {
8+
secrets := parseInput()
9+
priceAfter := map[int]map[Changes]int{}
10+
discovered := map[Changes]map[int]struct{}{}
11+
12+
for buyerID, secret := range secrets {
13+
14+
// Initialise map
15+
if _, ok := priceAfter[buyerID]; !ok {
16+
priceAfter[buyerID] = map[Changes]int{}
17+
}
18+
19+
// ring data structure, to reduce allocations
20+
ring, i := Changes{}, 0
21+
22+
oldPrice := secret % 10
23+
for t := 1; t <= 2000; t++ {
24+
25+
// generate a new secret
26+
secret = compute(secret)
27+
28+
newPrice := secret % 10
29+
delta := newPrice - oldPrice
30+
oldPrice = newPrice
31+
32+
// add the delta to the ring
33+
ring[i], i = delta, (i+1)%4
34+
35+
// skip the first 3 elements, as there are
36+
// not enough deltas for a full change
37+
if t >= 4 {
38+
change := Changes{ring[i], ring[(i+1)%4], ring[(i+2)%4], ring[(i+3)%4]}
39+
40+
// Initialise map
41+
if _, ok := discovered[change]; !ok {
42+
discovered[change] = map[int]struct{}{}
43+
}
44+
discovered[change][buyerID] = struct{}{}
45+
46+
// insert only the first occurrence, ignore the next ones
47+
if _, ok := priceAfter[buyerID][change]; !ok {
48+
priceAfter[buyerID][change] = newPrice
49+
}
50+
}
51+
}
52+
}
53+
54+
maxBananas := 0
55+
for change, affectedIDs := range discovered {
56+
bananas := 0
57+
for buyerID := range affectedIDs {
58+
bananas += priceAfter[buyerID][change]
59+
}
60+
if bananas > maxBananas {
61+
maxBananas = bananas
62+
}
63+
}
64+
65+
fmt.Println(maxBananas)
66+
}

0 commit comments

Comments
 (0)