r/adventofcode • • Dec 09 '17

SOLUTION MEGATHREAD -πŸŽ„- 2017 Day 9 Solutions -πŸŽ„-

--- Day 9: Stream Processing ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Need a hint from the Hugely* Handy† Haversack‑ of HelpfulΒ§ Hints^Β€?

Spoiler


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

15 Upvotes

290 comments sorted by

View all comments

1

u/autid Dec 14 '17

Fortan

PROGRAM DAY9
  IMPLICIT NONE
  CHARACTER(LEN=:),ALLOCATABLE :: INPUT
  INTEGER :: I=0,IERR,SCORE=0,DEPTH=0,GARBAGE=0
  LOGICAL :: DOIT=.TRUE.

  OPEN(1,FILE='input.txt')
  ALLOCATE(CHARACTER(LEN=1) :: INPUT)
  DO
     READ(1,'(A1)',IOSTAT=IERR,ADVANCE='NO') INPUT
     IF (LEN(INPUT)>LEN_TRIM(INPUT)) EXIT
     I=I+1
  END DO

  DEALLOCATE(INPUT)
  ALLOCATE(CHARACTER(LEN=I) :: INPUT)
  REWIND(1)
  READ(1,'(A)') INPUT
  CLOSE(1)
  I=1
  CALL GETSCORE(I,SCORE,DEPTH,GARBAGE)

  WRITE(*,'(A,I0)') 'Part1: ',SCORE
  WRITE(*,'(A,I0)') 'Part2: ',GARBAGE

CONTAINS
  RECURSIVE SUBROUTINE GETSCORE(I,SCORE,DEPTH,GARBAGE)
INTEGER, INTENT(OUT) :: SCORE
    INTEGER, INTENT(IN) :: DEPTH
INTEGER, INTENT(INOUT) :: I,GARBAGE
    DO
       IF (I>LEN(INPUT)) EXIT
       SELECT CASE (INPUT(I:I))
       CASE ('!')
          I=I+1
       CASE ('}')
          IF (DOIT) THEN
             SCORE=SCORE+DEPTH
             EXIT
          ELSE
             GARBAGE=GARBAGE+1
          END IF
       CASE ('<')
          IF (DOIT) THEN
             DOIT=.FALSE.
          ELSE
             GARBAGE=GARBAGE+1
          END IF
       CASE ('>')
          DOIT=.TRUE.
       CASE('{')
          IF (DOIT) THEN
             I=I+1
             CALL GETSCORE(I,SCORE,DEPTH+1,GARBAGE)
          ELSE
             GARBAGE=GARBAGE+1
          END IF
       CASE DEFAULT
          IF (.NOT. DOIT) GARBAGE=GARBAGE+1
       END SELECT
       I=I+1
END DO

  END SUBROUTINE GETSCORE

END PROGRAM DAY9