Advent of Code 2016 Day 20: Firewall Rules

Primary tabs

This is a series of programming challenges for beginners and experienced Caché programmers.

For an introduction : go to article https://community.intersystems.com/post/advent-code-2016-day1-no-time-ta...

We are almost there : after this challenge, only 5 days left till the end of this advent of code (I cannot wait to get my life back !) .

 

The challenge of day 20 is about a list of integer ranges. Some ranges overlap,  but there are also values that are outside any range.

You have to find out what is the lowest integer that is outside any range.
 

More explanation (there is not much more to say) can be found on http://adventofcode.com/2016/day/20, with the link to your puzzle input.

The puzzle input is a list of ranges, with values in 0-4294967295, start and end value in each range is inclusive.

 

The second part of the challenge is to find all integers values that are not included in any ranges.

 

Here is the code that will do both challenges at once, explanation is inline :

 

Class AOC2016.Day20 Extends AOC2016.Utils
{

ClassMethod Part1(file As %String = "day20.txt")
{
  #dim input as %String
  #dim iInput, lowest, allowed, found as %Integer
  #Dim from, to, ip as %String
  #Dim sort as Array of %String

  Try {
    Do ..Input(file, .input)
    //Put all integer value ranges in array, sort by lowest
    For iInput=1:1:input {
      Set from=$P(input(iInput),"-",1)
      Set to=$P(input(iInput),"-",2)
      Set sort(from,to)=""
    }
    Set found=""  //lowest integer value in total array (first lowest)
    Set lowest=0 //lowest integer value in array, move forward in loop outside range
    Set from=""   //track position in sort array
    Set allowed = 0 //total amount of integer values that are outside range
    For {
      Set from=$Order(sort(from)) If from="" Quit
      If lowest<from {  //lowest so far is outside range : everything upto from not included is not in range
        If found="" Set found=lowest  //first lowest found ever
        For ip=lowest:1:from-1 set allowed=allowed+1
        set lowest=from
      }
      Set to = "" For {
        Set to=$Order(sort(from,to)) If to="" Quit
        If (lowest '< from) & (lowest '>to) {  //lowest outside range is always 1 bigger than to
          Set lowest = to+1
        }
      }
    }
    Write "lowest : ",found,!
    Write "allowed : ",allowed,!
Catch {
    Write "Error : ",$ZError,!
  }
}

}

 

Look here for all our solutions so far : https://bitbucket.org/bertsarens/advent2016 and https://github.com/DannyWijnschenk/AdventOfCode2016
 

Here is the list of all Advent of Code 2016 articles  :

1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25