diff --git a/2021/2/.gitignore b/2021/2/.gitignore new file mode 100644 index 0000000..4b4b198 --- /dev/null +++ b/2021/2/.gitignore @@ -0,0 +1,3 @@ +depth +depth2 +input diff --git a/2021/2/Makefile b/2021/2/Makefile new file mode 100644 index 0000000..3882cc2 --- /dev/null +++ b/2021/2/Makefile @@ -0,0 +1,11 @@ +.PHONY := all build run + +all: build run + +build: + gfortran -o depth depth.f90 + gfortran -o depth2 depth2.f90 + +run: + ./depth + ./depth2 diff --git a/2021/2/depth.f90 b/2021/2/depth.f90 new file mode 100644 index 0000000..8c0096a --- /dev/null +++ b/2021/2/depth.f90 @@ -0,0 +1,40 @@ +program depth + implicit none + + integer :: ios, space_index, second_int, nforward = 0, ndepth = 0 + integer, parameter :: read_unit = 99 + character(len=30) :: line, first_part, second_part + + open(unit=read_unit, file='input', iostat=ios) + if ( ios /= 0 ) stop "Error opening file" + + do + read(read_unit, "(A)", iostat=ios) line + if (ios /= 0) exit + + space_index = scan(line, ' ') + first_part = line(1:space_index) + second_part = line(space_index+1:) + + read(second_part, *, iostat=ios) second_int + + select case (first_part) + + case ("forward") + nforward = nforward + second_int + + case ("up") + ndepth = ndepth - second_int + + case ("down") + ndepth = ndepth + second_int + + end select + + end do + + print*, "Total forward:", nforward + print*, "Total depth:", ndepth + print*, "Product:", nforward * ndepth + +end program depth diff --git a/2021/2/depth2.f90 b/2021/2/depth2.f90 new file mode 100644 index 0000000..0dbb9d1 --- /dev/null +++ b/2021/2/depth2.f90 @@ -0,0 +1,41 @@ +program depth2 + implicit none + + integer :: ios, space_index, second_int, nforward = 0, ndepth = 0, naim = 0 + integer, parameter :: read_unit = 99 + character(len=30) :: line, first_part, second_part + + open(unit=read_unit, file='input', iostat=ios) + if ( ios /= 0 ) stop "Error opening file" + + do + read(read_unit, "(A)", iostat=ios) line + if (ios /= 0) exit + + space_index = scan(line, ' ') + first_part = line(1:space_index) + second_part = line(space_index+1:) + + read(second_part, *, iostat=ios) second_int + + select case (first_part) + + case ("forward") + nforward = nforward + second_int + ndepth = ndepth + (naim * second_int) + + case ("up") + naim = naim - second_int + + case ("down") + naim = naim + second_int + + end select + + end do + + print*, "Total forward:", nforward + print*, "Total depth:", ndepth + print*, "Product:", nforward * ndepth + +end program depth2