-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc_day01_p1.f90
81 lines (64 loc) · 2.51 KB
/
aoc_day01_p1.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
! Advent of Code 2015 : https://adventofcode.com/2015
! Puzzle for Day 01 part 01 :
! Build and run with Fortran Package Manager (fpm):
! fpm run aoc_day01_p1
program aoc_day01_p1
use, intrinsic :: iso_fortran_env, stderr => error_unit
implicit none
! Declare and set constants the AOC day and part
integer, parameter :: day = 01, part = 1
! Declare file related variables
! - Prod uses: day01-input.txt
! - Test uses: day01-TEST-input.txt
character(len = *), parameter :: filename = "data/day01-input.txt"
! Variables to track and manage the program
integer :: floor_number = 0, up = 0, down = 0
! Output startup message
call display_welcome(day, part)
! Check input puzzle data file and get its line count
call get_floor(filename,floor_number,up,down)
print *, ""
write (*, '(A,I0)') "Floor changes analysed: ", (up + down)
write (*, '(A,I0)') "Floors up are: ",up
write (*, '(A,I0)') "Floors down are: ",down
print *, ""
write (*, '(A,I0)') "PUZZLE ANSWER: Final floor is: ",floor_number
print *, ""
contains
! Display the AoC puzzle day and part being resolved
subroutine display_welcome(day, part)
implicit none
integer, intent(in) :: day, part
print *, ""
write (*, '(A,I2.2,A,I2.2)') "AoC 2015 - solution for Day ", day, " part ", part
print *, ""
end subroutine display_welcome
! count the floor changes held in the single line of '(()()()" in the input file
! where '(' is up one floor and ')' is down one floor
subroutine get_floor(filename,floor_number,up,down)
implicit none
character(len = *), intent(in) :: filename
integer, intent(inout) :: floor_number, up, down
integer :: fileunit, io
logical :: exists
character(len=1) :: next_floor
inquire(file = filename, exist = exists)
if (.not. exists) then
! below should be printed to stderr!!
write (stderr, '(A,A)') "ERROR: unable to locate file name: ", filename
return
end if
open(newunit = fileunit, file = filename, action = 'read', position = 'rewind')
do
read(fileunit,'(A)', iostat = io, advance='no') next_floor
if (io /= 0) exit
if (next_floor .eq. "(") then
up = up + 1
else
down = down + 1
end if
end do
floor_number = up - down
close(fileunit)
end subroutine get_floor
end program aoc_day01_p1