crlogic

Day4

Dec 4th, 2022
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. # Part 1
  2. # In how many assignment pairs does one range fully contain the other?
  3. [string[]]$assignments = $puzzleInput -split '\n' | Select -SkipLast 1
  4. $fullyContain = 0
  5. $answers = foreach ($pair in $assignments) {
  6. [int]$p1l, [int]$p1h, [int]$p2l, [int]$p2h = $pair -split '-|,'
  7. if (($p1l -le $p2l -and $p1h -ge $p2h) -or ($p2l -le $p1l -and $p2h -ge $p1h)) {
  8. $fullyContain++
  9. }
  10. }
  11. $fullyContain
  12.  
  13. # Part 2
  14. # In how many assignment pairs do the ranges overlap?
  15. $overlap = 0
  16. $answers = foreach ($pair in $assignments) {
  17. [int]$p1l, [int]$p1h, [int]$p2l, [int]$p2h = $pair -split '-|,'
  18. $elf1 = $p1l..$p1h
  19. $elf2 = $p2l..$p2h
  20. if (Compare $elf1 $elf2 -ExcludeDifferent -IncludeEqual) {
  21. $overlap++
  22. }
  23. }
  24. $overlap
  25.  
Advertisement
Add Comment
Please, Sign In to add comment