Advertisement
icarussiano

day 2 Python/Julia

Dec 2nd, 2024 (edited)
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. #Python
  2. lines = [list(map(int, line.split())) for line in open('input.txt')]
  3. def safe(l):
  4.     return all(((a < b and l[0] < l[1]) or (a > b and l[0] > l[1])) and abs(a-b)<=3  for a, b in zip(l, l[1:]))
  5. def safe2(l):
  6.     return 1 if safe(l) or any(safe(l[:i] + l[i+1:]) for i in range(len(l))) else 0
  7. print(sum(safe(l) for l in lines))
  8. print(sum(safe2(l) for l in lines))
  9.  
  10. #Julia
  11. lines = [parse.(Int, split(line)) for line in readlines("input.txt")]
  12. safe(lista) = all(((a < b && lista[1] < lista[2]) || (a > b && lista[1] > lista[2])) && abs(a - b) <= 3 for (a, b) in zip(lista, lista[2:end]))
  13. safe2(lista) = safe(lista) || any(safe(vcat(lista[1:i-1], lista[i+1:end])) for i in 1:length(lista)) ? 1 : 0
  14. println(sum(safe(line) for line in lines))
  15. println(sum(safe2(line) for line in lines))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement