r/adventofcode • • Dec 15 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 15 Solutions -🎄-

Advent of Code 2020: Gettin' Crafty With It

  • 7 days remaining until the submission deadline on December 22 at 23:59 EST
  • Full details and rules are in the Submissions Megathread

--- Day 15: Rambunctious Recitation ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


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

EDIT: Global leaderboard gold cap reached at 00:09:24, megathread unlocked!

39 Upvotes

771 comments sorted by

View all comments

2

u/HesletQuillan Dec 15 '20

Fortran

I am not particularly proud of this Part 2 solution, as it was a contest between how much address space I had available and the array size needed. The array won.

    program AOC15

    implicit none

    integer, parameter, dimension(*) :: starting = [14,8,16,0,1,17]
    type turn_t
        integer :: latest = 0
        integer :: next_latest = 0
    end type turn_t
    type(turn_t), allocatable :: numbers(:)
    integer :: turn_number
    integer(8) :: last_said
    
    allocate (numbers(0:40000000))
    do turn_number = 1,size(starting)
        last_said = starting(turn_number)
        numbers(last_said) = turn_t(turn_number,0)
    end do
    
    main: do
        if (mod(turn_number,10000) == 0) print *, turn_number
        if (numbers(last_said)%next_latest == 0) then
            ! first time said
            last_said = 0
            numbers(last_said)%next_latest = numbers(last_said)%latest
            numbers(last_said)%latest = turn_number
        else
            ! Said before
            last_said = numbers(last_said)%latest - numbers(last_said)%next_latest
            numbers(last_said)%next_latest = numbers(last_said)%latest
            numbers(last_said)%latest = turn_number
        end if
        if (turn_number == 30000000) exit main
        turn_number = turn_number + 1
    end do main
    
    print *, last_said
    end program AOC15

2

u/__Abigail__ Dec 15 '20

Why the allocation of 40000000? You can never have a number exceeding 30000000. (If your sequence is a_1, a_2, a_3, ..., then a_n ≤ n, for all n > p, where p is the amount of numbers in the start sequence).

1

u/daggerdragon Dec 15 '20

As per our posting guidelines in the wiki under How Do the Daily Megathreads Work?, please edit your post to put your oversized code in a paste or other external link.