From ace71e8a98451b9fecb6e0a2d0efb6f04476f018 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Tue, 21 May 2024 03:32:18 +1000 Subject: pwc270 - improvement on ch-2 --- challenge-270/pokgopun/go/ch-2.go | 21 +++++++++------------ challenge-270/pokgopun/python/ch-2.py | 19 +++++++++---------- 2 files changed, 18 insertions(+), 22 deletions(-) (limited to 'challenge-270') diff --git a/challenge-270/pokgopun/go/ch-2.go b/challenge-270/pokgopun/go/ch-2.go index fb096121d2..07d27baca7 100644 --- a/challenge-270/pokgopun/go/ch-2.go +++ b/challenge-270/pokgopun/go/ch-2.go @@ -88,25 +88,22 @@ func distElem(ints []int, x, y int) int { mx := ints[l-1] c := 0 for { - ints = ints[:slices.Index(ints, mx)] - l = len(ints) + l = slices.Index(ints, mx) if l == 0 { break } + d := mx - ints[l-1] if l == 1 || 2*x < y { - for ints[l-1] < mx { - for i := range l { - ints[i]++ - c += x - } + for i := range l { + ints[i] += d + c += x * d } } else { - for ints[l-1] < mx { - for i := range 2 { - ints[l-1-i]++ - } - c += y + p := l / 2 + for i := range p * 2 { + ints[l-1-i] += d } + c += y * p * d } } return c diff --git a/challenge-270/pokgopun/python/ch-2.py b/challenge-270/pokgopun/python/ch-2.py index ac6e3bbcb0..ad5035105a 100644 --- a/challenge-270/pokgopun/python/ch-2.py +++ b/challenge-270/pokgopun/python/ch-2.py @@ -76,20 +76,19 @@ def distElem(ints: list, x: int, y: int): mx = ints[-1] c = 0 while True: - ints = ints[:ints.index(mx)] - l = len(ints) + l = ints.index(mx) if l == 0: break + d = mx - ints[l-1] if l == 1 or 2*x < y: - while ints[-1] < mx: - for i in range(l): - ints[i] += 1 - c += x + for i in range(l): + ints[i] += d + c += x * d else: - while ints[-1] < mx: - for i in range(-2,0): - ints[i] += 1 - c += y + p = l // 2 + for i in range(2*p): + ints[l-1-i] += d + c += y * p * d return c import unittest -- cgit