File tree Expand file tree Collapse file tree 4 files changed +78
-0
lines changed
Expand file tree Collapse file tree 4 files changed +78
-0
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments