Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Python
- lines = [list(map(int, line.split())) for line in open('input.txt')]
- def safe(l):
- 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:]))
- def safe2(l):
- return 1 if safe(l) or any(safe(l[:i] + l[i+1:]) for i in range(len(l))) else 0
- print(sum(safe(l) for l in lines))
- print(sum(safe2(l) for l in lines))
- #Julia
- lines = [parse.(Int, split(line)) for line in readlines("input.txt")]
- 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]))
- safe2(lista) = safe(lista) || any(safe(vcat(lista[1:i-1], lista[i+1:end])) for i in 1:length(lista)) ? 1 : 0
- println(sum(safe(line) for line in lines))
- println(sum(safe2(line) for line in lines))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement