view the Advent of Code 2021 Day 1 challenge
# Setup - load any packages
import numpy as np
data_raw = np.genfromtxt('day1_data.txt',delimiter=',')
data_raw
array([ 140., 154., 165., ..., 3591., 3595., 3596.])
How many measurements are larger than the previous measurement?
# create a numpy array
data = data_raw
# create additional numpy array
# duplicate the first element at the start of the array to offset the elements
data2=np.insert(data, 0, data[0], axis=0)
# duplicate the last element at the end of the original data array so it is the same length as data2
data=np.append(data, data[-1])
print(data)
print(data2)
[ 140. 154. 165. ... 3595. 3596. 3596.] [ 140. 140. 154. ... 3591. 3595. 3596.]
# Compare each of the elements in data array to see if it is greater than the element in data2 array
compare=data>data2
# view the results
print(compare)
# Get a count
sum(compare)
[False True True ... True True False]
1298
def CompareSonarList(list1, list2):
return (sum(list1)<sum(list2))
results = []
for i in range(len(data_raw)-1):
#print (data_raw[i:i+3], data_raw[i+1:i+4])
results.append(CompareSonarList(data_raw[i:i+3], data_raw[i+1:i+4]))
print(sum(results))
1248