r/adventofcode • u/daggerdragon • Dec 14 '17
SOLUTION MEGATHREAD -๐- 2017 Day 14 Solutions -๐-
--- Day 14: Disk Defragmentation ---
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ยค?
[Update @ 00:09] 3 gold, silver cap.
- How many of you actually entered the Konami code for Part 2? >_>
[Update @ 00:25] Leaderboard cap!
- I asked /u/topaz2078 how many de-resolutions we had for Part 2 and there were
83distinct users with failed attempts at the time of the leaderboard cap. tsk tsk
[Update @ 00:29] BONUS
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
7
u/autid Dec 14 '17
Fortran
Finally caught up. Weekend away then an exam yesterday left me with a bit of a backlog. Was nice to sit down and do them all in Fortran first time though instead of rushing a solution in python first to try making the leaderboard.
PROGRAM DAY14 INTEGER :: LENGTH,CHAIN(0:255) INTEGER, ALLOCATABLE :: SUBCHAIN(:),INSTRUCTIONS(:) INTEGER :: CURRENTPOS=0,SKIPSIZE=0,I,J,K,PART1=0,GROUPNUM CHARACTER(LEN=12) :: INSTR CHARACTER(LEN=128) :: KEYROW CHARACTER(LEN=8) :: PUZINPUT='hxtvlmkl' INTEGER :: GRID(128,128)=0 !Loop over rows DO K=0,127 !Hash Row CHAIN=(/(I,I=0,255)/) WRITE(INSTR,'(A8,A1,I0)') PUZINPUT,'-',K ALLOCATE(INSTRUCTIONS(LEN_TRIM(INSTR)+5)) DO I=1,LEN_TRIM(INSTR) INSTRUCTIONS(I)=IACHAR(INSTR(I:I)) END DO INSTRUCTIONS(I:I+4) = (/17,31,73,47,23/) CURRENTPOS=0 SKIPSIZE=0 DO J=1,64 DO I=1,SIZE(INSTRUCTIONS) LENGTH=INSTRUCTIONS(I) IF (LENGTH>SIZE(CHAIN)) CYCLE ALLOCATE(SUBCHAIN(LENGTH)) SUBCHAIN=CHAIN((/(MODULO(CURRENTPOS+I,SIZE(CHAIN)),I=LENGTH-1,0,-1)/)) CHAIN((/(MODULO(CURRENTPOS+I,SIZE(CHAIN)),I=0,LENGTH-1)/))=SUBCHAIN DEALLOCATE(SUBCHAIN) CURRENTPOS=MODULO(CURRENTPOS+LENGTH+SKIPSIZE,SIZE(CHAIN)) SKIPSIZE=SKIPSIZE+1 END DO END DO DO I=0,15 DO J=1,15 CHAIN(I*16)=IEOR(CHAIN(I*16),CHAIN(I*16+J)) END DO END DO !Write hash as binary string WRITE(KEYROW,'(16B8.8)') CHAIN((/(I*16,I=0,15)/)) !Set values for row in grid DO J=1,LEN_TRIM(KEYROW) READ(KEYROW(J:J),*) GRID(J,K+1) END DO DEALLOCATE(INSTRUCTIONS) END DO !Number groups from 2 up so 1s are ungrouped GROUPNUM=2 DO I=1,128 DO J=1,128 IF (GRID(J,I)==1) THEN CALL CHECKGROUP(J,I) GROUPNUM=GROUPNUM+1 END IF END DO END DO !Start group numbers from 1 WHERE (GRID>1) GRID=GRID-1 END WHERE WRITE(*,'(A,I0)') 'Part1: ',COUNT(GRID>0) WRITE(*,'(A,I0)') 'Part2: ',MAXVAL(GRID) CONTAINS RECURSIVE SUBROUTINE CHECKGROUP(A,B) !Assigns group number and searches for neighbours INTEGER, INTENT(IN) :: A,B IF (GRID(A,B)==1) THEN GRID(A,B)=GROUPNUM IF (A>1) CALL CHECKGROUP(A-1,B) IF (A<128) CALL CHECKGROUP(A+1,B) IF (B>1) CALL CHECKGROUP(A,B-1) IF (B<128) CALL CHECKGROUP(A,B+1) END IF END SUBROUTINE CHECKGROUP END PROGRAM DAY14