feat(2025): optimize day1 part2

This commit is contained in:
tiennm99 committed 2025-12-01 22:49:13 +07:00
1 parent 7f49236d21
commit 2b4ddffbcf
1 file changed
+10 -6
+10 -6
View File
@@ -3,6 +3,7 @@ package main
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"math"
"os" "os"
"strconv" "strconv"
) )
@@ -22,13 +23,16 @@ func main() {
multiplier = -1 multiplier = -1
} }
number, _ := strconv.Atoi(line[1:]) number, _ := strconv.Atoi(line[1:])
for range number { prevCurrent := current
current += multiplier current += number * multiplier
current = ((current % 100) + 100) % 100 counter += int(math.Floor(math.Abs(float64(current) / 100)))
if current == 0 { if prevCurrent*current < 0 {
counter++ counter++
}
} }
if current == 0 {
counter++
}
current = ((current % 100) + 100) % 100
} }
fmt.Printf("%d\n", counter) fmt.Printf("%d\n", counter)
} }