Advent of Code #2

#open file to be read
source_file = open("sonar2.txt", "r")
contents = source_file.read()
sonar_list = contents.split("\n")
source_file.close()
length_of_sonar_list = len(sonar_list)
list_pointer = 0
forward = 0
depth = 0
the_end = length_of_sonar_list
while (list_pointer < the_end):
 element = sonar_list[list_pointer]
 first_char = element[0]
 last_num = int(element[-1])
 if (first_char == "f") :
  forward=forward+last_num
 elif (first_char =="d") :
  depth=depth+last_num
 else :
  depth=depth-last_num
 list_pointer=list_pointer+1
print("forward =", forward)
print("depth =", depth)
print(forward*depth)

 



Copyright 2018. All rights reserved.

Posted December 3, 2021 by sal in category "code