Skip to content

Commit

Permalink
fix p22
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Oct 8, 2024
1 parent 0cb6674 commit 30aa9cc
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Olivia's Project Euler Solutions
| | | | |CodeQL| |br| |
| | | | |C#-lint| |
+------------+----------------------------+--------+-------------------+
| Fortran | Fortran 95+ in |gcc|, |br| | 17 | |Fri| |
| Fortran | Fortran 95+ in |gcc|, |br| | 18 | |Fri| |
| | |flang|, |nvf|, |br| |ifx| | | |
+------------+----------------------------+--------+-------------------+
| Java | Java 8+ in Corretto, |br| | 22 | |Javai| |br| |
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ Problems Solved
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`021`| | | | | |:js-d:`0021`| |:py-d:`0021`|:rs-i:`0021`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`022`|:c-d:`0022`|:cp-d:`0022`|:cs-d:`0022`|:fr-i:`0022`|:ja-d:`0022`|:js-d:`0022`| |:py-d:`0022`|:rs-d:`0022`|
|:prob:`022`|:c-d:`0022`|:cp-d:`0022`|:cs-d:`0022`|:fr-d:`0022`|:ja-d:`0022`|:js-d:`0022`| |:py-d:`0022`|:rs-d:`0022`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`023`| | | | | |:js-d:`0023`| |:py-d:`0023`|:rs-d:`0023`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+------------+
Expand Down
10 changes: 8 additions & 2 deletions fortran/src/include/utils.f90
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ module utils
logical, private :: cache_inited = .false.
type(AnswerT), private, dimension(1024) :: cached_answers
contains
integer function open_data_file(name) result(unit)
integer function open_data_file(name, direct, recl) result(unit)
character(len=DATA_MAX_NAME_SIZE), intent(in) :: name
logical, intent(in), optional :: direct
integer, intent(in), optional :: recl
integer :: ios

unit = prev_unit + 1
prev_unit = unit
open(unit=unit, file=("../_data/" // name), status='old', action='read', iostat=ios)
if (present(direct) .and. present(recl) .and. direct) then
open(unit=unit, file=("../_data/" // name), status='old', action='read', iostat=ios, access='direct', recl=recl)
else
open(unit=unit, file=("../_data/" // name), status='old', action='read', iostat=ios)
end if
if (ios /= 0) then
print *, "Error opening file: ../_data/" // name
return
Expand Down
18 changes: 11 additions & 7 deletions fortran/src/p0022.f90
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,38 @@ module Problem0022
integer(i18t) function p0022() result(answer)
character(len=DATA_MAX_NAME_SIZE), parameter :: file_name = "p0022_names.txt"
character(len=longest_name), dimension(name_count) :: names
character(len=1) current_char
character current_char
integer(i18t) score
integer ios, unit, i, j
integer ios, unit, i, j, k

i = 1
j = 1
k = 1
answer = 0
names = ''
unit = open_data_file(file_name)
unit = open_data_file(file_name, .true., 1)
do
read(unit, '(A1)', IOSTAT=ios) current_char
read(unit, rec=j, IOSTAT=ios) current_char
j = j + 1
if (ios /= 0) then
exit
end if

select case (current_char)
case (',')
i = i + 1
k = 1
case ('"')
continue
case default
names(i) = trim(names(i) // current_char)
names(i)(k:k) = current_char
k = k + 1
end select
end do
close(unit)
call p0022_sort(names)
do i = 1, name_count
score = 0
do j = 1, len(names(i))
do j = 1, len_trim(names(i))
score = score + ichar(names(i)(j:j)) - ichar('A') + 1
end do
answer = answer + score * i
Expand Down
2 changes: 0 additions & 2 deletions fortran/test.f90
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ subroutine process_problems()
print *, " Error: problem ", problem_ids(i), " failed!"
print *, " Expected Answer : ", expected%int_value
print *, " Solution returned: ", answer%int_value
if (problem_ids(i) /= 22) then
stop ERROR_ANSWER_MISMATCH
end if
end if
case (stringt)
if (expected%string_value /= answer%string_value) then
Expand Down

0 comments on commit 30aa9cc

Please sign in to comment.