1
Advent of Code 2024[github]
1
2
Advent of Code is an Advent calendar of small programming puzzles
3
for a variety of skill levels that can be solved in any
4
programming language you like. People use them as interview prep,
5
company training, university coursework, practice problems, a
6
speed contest, or to challenge each other.
7
8
You don't need a computer science background to participate -
9
just a little programming knowledge and some problem solving
10
skills will get you pretty far. Nor do you need a fancy computer;
11
every problem has a solution that completes in at most 15 seconds
12
on ten-year-old hardware.
13
14
15
17
18
func Part2(input []byte) {
19
line := strings.Trim(string(input), "\n")
20
stones := parse(line)
21
blinks := 75
22
23
cache := map[Pair]uint64{}
24
count := big.NewInt(0)
25
for _, stone := range stones {
26
count.Add(count, new(big.Int).SetUint64(walk(uint64(stone), uint64(blinks), cache)))
27
}
28
29
fmt.Println("Result is", count)
30
}
31
32
type Pair struct {
33
f uint64
34
s uint64
35
}
36
37
func walk(it, deepth uint64, cache map[Pair]uint64) uint64 {
38
if deepth == 0 {
39
return 1
40
}
41
42
cached, exists := cache[Pair{it, deepth}]
43
if exists {
44
return cached
45
}
46
47
func Part2(input []byte) {
line := strings.Trim(string(input), "\n")
stones := parse(line)
blinks := 75
cache := map[Pair]uint64{}
count := big.NewInt(0)
for _, stone := range stones {
count.Add(count, new(big.Int).SetUint64(walk(uint64(stone), uint64(blinks), cache)))
}
fmt.Println("Result is", count)
}
type Pair struct {
f uint64
s uint64
}
func walk(it, deepth uint64, cache map[Pair]uint64) uint64 {
if deepth == 0 {
return 1
}
cached, exists := cache[Pair{it, deepth}]
if exists {
return cached
}
func getValidPages(update []int, rules map[int][]int) []int {
// we have all rules, which are 24 per page
// we also have pages in update
// we need to iterate over update pages, and for each page:
// 1. grab all possible rules
relevantRulesPerPage := map[int][]int{}
for _, page := range update {
allRulesForPage := rules[page]
// 2. filter rules which are not present in update
presentInUpdateRules := []int{}
for _, potentialRule := range allRulesForPage {
if slices.Contains(update, potentialRule) {
presentInUpdateRules = append(presentInUpdateRules, potentialRule)
}
}
// (now we have all actual rules for page in update)
// 3. save those relevant rules per page to map
relevantRulesPerPage[page] = presentInUpdateRules
}
// 4. sort pages by rules count
res := slices.Clone(update)
slices.SortFunc(res, func(p1, p2 int) int {
return len(relevantRulesPerPage[p2]) - len(relevantRulesPerPage[p1])
})
// 5. Return sorter pages slice
return res
}
func generateOperatorStrings2(operatorsCount int) []string {
if operatorsCount == 1 {
return operators
}
result := []string{}
for _, operator := range operators {
options := generateOperatorStrings2(operatorsCount - 1)
for _, option := range options {
result = append(result, operator+option)
}
}
return result
}
func calculate2(operands []int, operators string) int {
result := operands[0]
for i, operator := range operators {
switch operator {
case '+':
result += operands[i+1]
case '*':
result *= operands[i+1]
default:
squashed := fmt.Sprintf("%d%d", result, operands[i+1])
result, _ = strconv.Atoi(squashed)
}
}
return result
}
func expand(diskMap string) []int {
res := []int{}
id := 0
for i, r := range diskMap {
isFile := i%2 == 0
if isFile {
res = append(res, repeat(id, parseInt(r))...)
id++
} else {
res = append(res, repeat(-1, parseInt(r))...)
}
}
return res
}