import lib/[loader, util] proc isValid(numbers: seq[int], idx: int, size = 25): bool = # find two previous numbers that sum to the given number let slice = (idx - size) .. (idx - 1) for a in slice: for b in slice: if a == b: continue elif numbers[a] + numbers[b] == numbers[idx]: return true return false proc findRangeWithSum(numbers: seq[int]; target: int): (int, int) = for start in 0 .. (numbers.high - 1): for stop in (start + 1) .. numbers.high: var total = 0 for i in start .. stop: total += numbers[i] if total == target: return (start, stop) raise newException(ValueError, "Could not find a range of numbers adding to " & $target) proc partOne(numbers: seq[int]): int = for idx in 25 .. numbers.high: if not isValid(numbers, idx): return numbers[idx] raise newException(ValueError, "Could not find an invalid value") proc partTwo(numbers: seq[int]): int = let target = partOne(numbers) let (start, stop) = numbers.findRangeWithSum(target) return min(numbers[start..stop]) + max(numbers[start..stop]) when isMainModule: let data = loadInts(9) echo "One: ", partOne(data) echo "Two: ", partTwo(data)