Skip to content

Commit 7b45430

Browse files
committed
Solution 2024, day 7
1 parent 948b99f commit 7b45430

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

2024/7/Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
input:
2+
http "https://adventofcode.com/2024/day/7/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

2024/7/common.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"strconv"
8+
"strings"
9+
)
10+
11+
func main() {
12+
sum := 0
13+
for scanner := bufio.NewScanner(os.Stdin); scanner.Scan(); {
14+
ff := strings.Split(scanner.Text(), ":")
15+
left, _ := strconv.Atoi(ff[0])
16+
right := []int{}
17+
for _, v := range strings.Fields(ff[1]) {
18+
n, _ := strconv.Atoi(v)
19+
right = append(right, n)
20+
}
21+
22+
if valid(left, right[0], right[1:]) {
23+
sum += left
24+
}
25+
}
26+
fmt.Println(sum)
27+
}

2024/7/main1.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
func valid(goal, temp int, others []int) bool {
4+
if len(others) == 0 {
5+
return temp == goal
6+
}
7+
8+
if valid(goal, temp+others[0], others[1:]) {
9+
return true
10+
}
11+
12+
return valid(goal, temp*others[0], others[1:])
13+
}

2024/7/main2.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import "strconv"
4+
5+
func valid(goal, temp int, others []int) bool {
6+
if len(others) == 0 {
7+
return temp == goal
8+
}
9+
10+
if valid(goal, temp+others[0], others[1:]) {
11+
return true
12+
}
13+
if valid(goal, temp*others[0], others[1:]) {
14+
return true
15+
}
16+
17+
temp, _ = strconv.Atoi(strconv.Itoa(temp) + strconv.Itoa(others[0]))
18+
return valid(goal, temp, others[1:])
19+
}

0 commit comments

Comments
 (0)