Advent of Code #1

import string
print("reading file..")
#open file to be read
source_file = open("sonar.txt", "r")
contents = source_file.read()
sonar_list = contents.split("\n")
source_file.close()
#examine contents
print(sonar_list)
#total length of list
length_of_sonar_list = len(sonar_list)
#some variables
list_pointer = 0
increased = 1
the_end = length_of_sonar_list -1
print("Number of elements in the list: ", length_of_sonar_list)
#main loop
while (list_pointer < the_end):
 element1 = sonar_list[list_pointer]
 element2 = sonar_list[list_pointer+1]
 if (element2 > element1) :
  increased=increased+1
  print(element1, element2,"***increased")
 else:
  print(element1, element2,"---decreased")
 list_pointer=list_pointer+1
print()
print("Total increases = ",increased)

 

output:



Copyright 2018. All rights reserved.

Posted December 2, 2021 by sal in category "code