File tree 1 file changed +11
-9
lines changed
1 file changed +11
-9
lines changed Original file line number Diff line number Diff line change @@ -23,10 +23,16 @@ func day7(file string) (part1, part2 int) {
23
23
l , r , _ := strings .Cut (t , ": " )
24
24
testv , _ := strconv .Atoi (l )
25
25
rs := strings .Split (r , " " )
26
- n := make ([]int , 0 , len (rs ))
27
- for _ , v := range rs {
28
- nums , _ := strconv .Atoi (v )
29
- n = append (n , nums )
26
+ n := make ([]int , len (rs ))
27
+ mult10 := make ([]int , len (rs )) // Faster if we precalc 10^x
28
+ for i , v := range rs {
29
+ num , _ := strconv .Atoi (v )
30
+ n [i ] = num
31
+ m := 1
32
+ for range len (v ) {
33
+ m *= 10
34
+ }
35
+ mult10 [i ] = m
30
36
}
31
37
ops := make ([]int , len (n )- 1 )
32
38
prevvalues := make ([]int , len (n ))
@@ -42,11 +48,7 @@ func day7(file string) (part1, part2 int) {
42
48
} else if ops [i - 1 ] == 1 {
43
49
start *= n [i ]
44
50
} else {
45
- // ispart2 = true
46
- for range len (rs [i ]) {
47
- start *= 10
48
- }
49
- start += n [i ]
51
+ start = start * mult10 [i ] + n [i ]
50
52
}
51
53
prevvalues [i ] = start
52
54
if start > testv {
You can’t perform that action at this time.
0 commit comments