qosaside.blogg.se

Kotlin find in list
Kotlin find in list









We’ll later use this function to check whether a given number is valid by passing this number and the list of the previous 25 numbers as arguments.įor convenience, we can define the function as an extension on a List of Long numbers. Let’s solve the task in Kotlin! For a start, let’s implement a function that checks whether a given list contains a pair of numbers that sum up to a given number.

  • 50 would also not be valid although 25 appears in the previous 25 numbers, the two numbers in the pair must be different.
  • 100 would not be valid no two of the previous 25 numbers sum to 100.
  • 49 would be a valid next number, as it is the sum of 24 and 25.
  • 26 would be a valid next number, as it could be 1 plus 25 (or many other pairs, like 2 and 24).
  • If the first 25 numbers are 1 through 25 in a random order, the next number must be the sum of two of those numbers to be valid:

    kotlin find in list

    Two numbers that sum to a valid number must be different from each other. We’ll call the number valid if it can be presented as a sum of two numbers from the previous sublist, and invalid otherwise. We need to attack a weakness in data encrypted with the eXchange-Masking Addition System (XMAS)! The data is a list of numbers, and we need to find the first number in the list (starting from the 26th) that is not the sum of any 2 of the 25 numbers before it.

    kotlin find in list

    While solving it, we’ll look into different ways to manipulate lists in Kotlin.

    kotlin find in list

    This post continues our “Idiomatic Kotlin” series and provides the solution for the Advent of Code Day 9* challenge.











    Kotlin find in list