I’m trying to complete Day 2 Part 2 of the Advent of code
All the info is on the web page but Part 1 describes a large number lines of data, each line has 5 – 8 numbers.
A line is considered safe of all the numbers are either all increasing or all decreasing. Any two adjacent numbers differ by at least one and at most three.
You then have to get the total number of safe lines. That wasn’t too difficult, heres my code
with open('Day2_Input', 'r') as dataInput: data = dataInput.read() safe = 0 for i in data.splitlines(): report = i.split(' ') if (all(int(report[i]) < int(report[i +1 ]) for i in range(len(report) -1 ))) or (all(int(report[i]) > int(report[i+1]) for i in range(len(report) - 1))) if all(abs(int(report[i + 1]) - int(report[i])) < 4 for i in range(len(report)-1)): safe += 1 print(safe)
My issue is with part 2. With this part it states that an unsafe line can be considered safe if removing one number would ensure it met the 2 above criteria. For example 12, 14, 16, 15, 17 would be considered unsafe but removing the 15 would mean it is safe.
The way I am approaching this is using a while loop to iterate over the list, removing a number at a time and checking again. My problem is that even though my while loop has correctly reported back that its condition is ‘False’ it continues to loop. Below is my code. I’m also using a single line as sample data to ensure it works before trying it on the 1000 lines of data the website gives you
i = '23 17 11 10 11 1' report = i.split(' ') e = 0 tempReport = list(report) while not (all(int(tempReport[i]) < int(tempReport[i+1]) for i in range(len(tempReport)-1))) or (all(int(tempReport[i]) > int(tempReport[i+1]) for i in range(len(tempReport)-1))): print(not((all(int(tempReport[i]) < int(tempReport[i + 1]) for i in range(len(tempReport) - 1))) or (all(int(tempReport[i]) > int(tempReport[i + 1]) for i in range(len(tempReport) - 1))))) tempReport = list(report) tempReport.pop(e) e+=1 print('success')
submitted by /u/darave123
[link] [comments]
r/learnpython I’m trying to complete Day 2 Part 2 of the Advent of code All the info is on the web page but Part 1 describes a large number lines of data, each line has 5 – 8 numbers. A line is considered safe of all the numbers are either all increasing or all decreasing. Any two adjacent numbers differ by at least one and at most three. You then have to get the total number of safe lines. That wasn’t too difficult, heres my code with open(‘Day2_Input’, ‘r’) as dataInput: data = dataInput.read() safe = 0 for i in data.splitlines(): report = i.split(‘ ‘) if (all(int(report[i]) < int(report[i +1 ]) for i in range(len(report) -1 ))) or (all(int(report[i]) > int(report[i+1]) for i in range(len(report) – 1))) if all(abs(int(report[i + 1]) – int(report[i])) < 4 for i in range(len(report)-1)): safe += 1 print(safe) My issue is with part 2. With this part it states that an unsafe line can be considered safe if removing one number would ensure it met the 2 above criteria. For example 12, 14, 16, 15, 17 would be considered unsafe but removing the 15 would mean it is safe. The way I am approaching this is using a while loop to iterate over the list, removing a number at a time and checking again. My problem is that even though my while loop has correctly reported back that its condition is ‘False’ it continues to loop. Below is my code. I’m also using a single line as sample data to ensure it works before trying it on the 1000 lines of data the website gives you i = ’23 17 11 10 11 1′ report = i.split(‘ ‘) e = 0 tempReport = list(report) while not (all(int(tempReport[i]) < int(tempReport[i+1]) for i in range(len(tempReport)-1))) or (all(int(tempReport[i]) > int(tempReport[i+1]) for i in range(len(tempReport)-1))): print(not((all(int(tempReport[i]) < int(tempReport[i + 1]) for i in range(len(tempReport) – 1))) or (all(int(tempReport[i]) > int(tempReport[i + 1]) for i in range(len(tempReport) – 1))))) tempReport = list(report) tempReport.pop(e) e+=1 print(‘success’) submitted by /u/darave123 [link] [comments]
I’m trying to complete Day 2 Part 2 of the Advent of code
All the info is on the web page but Part 1 describes a large number lines of data, each line has 5 – 8 numbers.
A line is considered safe of all the numbers are either all increasing or all decreasing. Any two adjacent numbers differ by at least one and at most three.
You then have to get the total number of safe lines. That wasn’t too difficult, heres my code
with open('Day2_Input', 'r') as dataInput: data = dataInput.read() safe = 0 for i in data.splitlines(): report = i.split(' ') if (all(int(report[i]) < int(report[i +1 ]) for i in range(len(report) -1 ))) or (all(int(report[i]) > int(report[i+1]) for i in range(len(report) - 1))) if all(abs(int(report[i + 1]) - int(report[i])) < 4 for i in range(len(report)-1)): safe += 1 print(safe)
My issue is with part 2. With this part it states that an unsafe line can be considered safe if removing one number would ensure it met the 2 above criteria. For example 12, 14, 16, 15, 17 would be considered unsafe but removing the 15 would mean it is safe.
The way I am approaching this is using a while loop to iterate over the list, removing a number at a time and checking again. My problem is that even though my while loop has correctly reported back that its condition is ‘False’ it continues to loop. Below is my code. I’m also using a single line as sample data to ensure it works before trying it on the 1000 lines of data the website gives you
i = '23 17 11 10 11 1' report = i.split(' ') e = 0 tempReport = list(report) while not (all(int(tempReport[i]) < int(tempReport[i+1]) for i in range(len(tempReport)-1))) or (all(int(tempReport[i]) > int(tempReport[i+1]) for i in range(len(tempReport)-1))): print(not((all(int(tempReport[i]) < int(tempReport[i + 1]) for i in range(len(tempReport) - 1))) or (all(int(tempReport[i]) > int(tempReport[i + 1]) for i in range(len(tempReport) - 1))))) tempReport = list(report) tempReport.pop(e) e+=1 print('success')
submitted by /u/darave123
[link] [comments]