From 2b4ddffbcfcac22f62b8fd0925d42f7893fb2753 Mon Sep 17 00:00:00 2001 From: tiennm99 Date: Mon, 1 Dec 2025 22:49:13 +0700 Subject: [PATCH] feat(2025): optimize day1 part2 --- 2025/day/1/part2/part2.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/2025/day/1/part2/part2.go b/2025/day/1/part2/part2.go index acb68fb..2341328 100644 --- a/2025/day/1/part2/part2.go +++ b/2025/day/1/part2/part2.go @@ -3,6 +3,7 @@ package main import ( "bufio" "fmt" + "math" "os" "strconv" ) @@ -22,13 +23,16 @@ func main() { multiplier = -1 } number, _ := strconv.Atoi(line[1:]) - for range number { - current += multiplier - current = ((current % 100) + 100) % 100 - if current == 0 { - counter++ - } + prevCurrent := current + current += number * multiplier + counter += int(math.Floor(math.Abs(float64(current) / 100))) + if prevCurrent*current < 0 { + counter++ } + if current == 0 { + counter++ + } + current = ((current % 100) + 100) % 100 } fmt.Printf("%d\n", counter) }