My yearly advent-of-code solutions
1program day_02
2 implicit none
3 logical :: is_decrement, fail
4 integer :: io, i,x, spaces, res
5 integer, dimension(:), allocatable :: list
6 character(len=100) :: line
7
8 open(newunit=io, file='./day_02_input.txt', status='old', action='read')
9
10 res = 0
11 do i = 1, 1000
12 read(io, '(a)') line ! reading the line
13 spaces = 1 ! reset space count
14 do x = 1, len(trim(line))
15 ! counting spaces to see how big of an array we need to allocate
16 if(iachar(line(x:x))== 32 ) then
17 spaces = spaces+1
18 end if
19 end do
20
21 allocate(list(spaces))
22 read(line, *) list !reading the ints into an array
23 fail = .false.
24
25 do x = 1, size(list) -1
26 if (list(x) == list(x+1)) then
27 fail = .true.
28 exit
29 end if
30 if(x == 1 .and. list(x) > list(x+1)) then
31 is_decrement = .true.
32 else if (x==1 .and. list(x) < list(x+1)) then
33 is_decrement = .false.
34 end if
35
36 if(is_decrement) then
37 if (list(x) < list(x+1) .or. abs(list(x) - list(x+1)) > 3) then
38 fail = .true.
39 exit
40 end if
41 else
42 if (list(x) > list(x+1) .or. abs(list(x) - list(x+1)) > 3) then
43 fail = .true.
44 exit
45 end if
46 end if
47 end do
48 if (fail .eqv. .false.) then
49 res = res + 1
50 endif
51 deallocate(list)
52 end do
53 print*, res
54end program day_02