SaltyPeaches

Advent of Code: Day 3 (Part 1)

Dec 3rd, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # I modified the source beforehand to replace all spaces with commas
  2. $lines = [IO.File]::ReadAllText("D:\scripts\Day3\Sequences.txt") -split "[\r\n]" | where {$_}
  3.  
  4. # My modification left a comma at the beginning of every line
  5. # Remove preceding commas
  6. for($i=0;$i -lt $lines.length; $i++){
  7.     $lines[$i] = $lines[$i].substring(1)
  8. }
  9.  
  10. # Set my counters
  11. $possible = 0
  12. $impossible = 0
  13.  
  14. # Check each line
  15. foreach($line in $lines){
  16.     $triangle = $line -split ','
  17.     $a = [int]$triangle[0]
  18.     $b = [int]$triangle[1]
  19.     $c = [int]$triangle[2]
  20.     if(($a + $b -gt $c) -and ($a + $c -gt $b) -and ($b + $c -gt $a)){
  21.         $possible++
  22.     }
  23.     else{
  24.         $impossible++
  25.     }
  26. }
  27.  
  28. write-host "Number of possible triangles" = $possible
  29. write-host "Number of impossible triangles" = $impossible
Advertisement
Add Comment
Please, Sign In to add comment