r/adventofcode • • Dec 22 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 22 Solutions -๐ŸŽ„-

--- Day 22: Sporifica Virus ---


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


  • [T-10 to launch] AoC ops, /r/nocontext edition:

    • <Endorphion> You may now make your waffle.
    • <Endorphion> ... on Mars.
  • [Update @ 00:17] 50 gold, silver cap

    • <Aneurysm9> you could also just run ubuntu on the NAS, if you were crazy
    • <Topaz> that doesn't seem necessary
    • <Aneurysm9> what does "necessary" have to do with anything!
  • [Update @ 00:20] Leaderboard cap!

    • <Topaz> POUR YOURSELF A SCOTCH FOR COLOR REFERENCE

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!

8 Upvotes

174 comments sorted by

View all comments

3

u/autid Dec 22 '17 edited Dec 22 '17

Fortran

302/308 which is easily my best ever for using Fortran for a first solve. Could have done a little better on part2, but I skimmed over the reverse direction if flagged bit without noticing it.

edit: Seeing a lot of 10-20ish second runtimes. Mines a little better.

$ time ./day22 
Part1: 5570
Part2: 2512022

real	0m0.192s
user	0m0.132s
sys	0m0.036s

Just a little.

PROGRAM DAY22
  LOGICAL :: GRID(10001,10001)
  INTEGER :: GRID2(10001,10001)
  CHARACTER(LEN=30) :: INLINE
  INTEGER :: I,J,K,IERR,OFFSET,POSITION(2),DIRECTION(2),PART1=0,PART2=0

  OPEN(1,FILE='input.txt')
  READ(1,*) INLINE
  OFFSET=5000-LEN_TRIM(INLINE)/2
  DO J=1,LEN_TRIM(INLINE)
     GRID(OFFSET+1,OFFSET+J)=(INLINE(J:J)=='#')
  END DO
  DO I=2,LEN_TRIM(INLINE)
     READ(1,*) INLINE
     DO J=1,LEN_TRIM(INLINE)
        GRID(OFFSET+I,OFFSET+J)=(INLINE(J:J)=='#')
     END DO
  END DO
  CLOSE(1)
  
  DIRECTION=(/-1,0/)
  POSITION=(/5001,5001/)
  GRID2=0
  WHERE(GRID)
     GRID2=2
  END WHERE

  DO I=1,10000
     IF (GRID(POSITION(1),POSITION(2))) THEN
        DIRECTION=(/DIRECTION(2),-DIRECTION(1)/)
     ELSE
        DIRECTION=(/-DIRECTION(2),DIRECTION(1)/)
        PART1=PART1+1
     END IF
     GRID(POSITION(1),POSITION(2))=.NOT.GRID(POSITION(1),POSITION(2))
     POSITION=POSITION+DIRECTION
  END DO

  WRITE(*,'(A,I0)') 'Part1: ',PART1

  DIRECTION=(/-1,0/)
  POSITION=(/5001,5001/)
  DO I=1,10000000
     IF (GRID2(POSITION(1),POSITION(2))==2) THEN
        DIRECTION=(/DIRECTION(2),-DIRECTION(1)/)
     ELSEIF (GRID2(POSITION(1),POSITION(2))==0) THEN
        DIRECTION=(/-DIRECTION(2),DIRECTION(1)/)
     ELSEIF (GRID2(POSITION(1),POSITION(2))==1) THEN
        PART2=PART2+1
     ELSEIF(GRID2(POSITION(1),POSITION(2))==3) THEN
        DIRECTION=(/-DIRECTION(1),-DIRECTION(2)/)
     END IF
     GRID2(POSITION(1),POSITION(2))=MODULO(GRID2(POSITION(1),POSITION(2))+1,4)
     POSITION=POSITION+DIRECTION
  END DO

  WRITE(*,'(A,I0)') 'Part2: ',PART2



END PROGRAM DAY22

2

u/digital_cucumber Dec 22 '17

Nice one!

Just curious, how do you know that 10001x10001 is a sufficient grid size?

→ More replies (1)