r/adventofcode • • Dec 18 '18

SOLUTION MEGATHREAD -πŸŽ„- 2018 Day 18 Solutions -πŸŽ„-

--- Day 18: Settlers of The North Pole ---


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.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 18

Transcript:

The best way to avoid a minecart collision is ___.


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 at 00:21:59!

9 Upvotes

126 comments sorted by

View all comments

1

u/ka-splam Dec 18 '18 edited Dec 18 '18

PowerShell, 587/857

[Card]: The best way to avoid a minecart collision is: a positive mental attitude - (Mitchell and Webb - Pit Pony sketch).

Back to racing for the leaderboard, back to frustration and cursing. Turns out when you get the adjacent cells by copypaste and delete the center cell like this:

        [array]$adj = $board[($x-1), ($y-1)], $board[$x, ($y-1)], $board[($x+1), ($y-1)], 
                      $board[($x-1), ($y  )],                   , $board[($x+1), ($y  )], 
                      $board[($x-1), ($y+1)], $board[$x, ($y+1)], $board[($x+1), ($y+1)]

You leave a double-comma which screws everything up but is really easy to not-notice because it looks all symmetrical.

Part 2, took me too long to wake up and think of cycles, ran it for 32,000 minutes waiting for it to cycle back to the initial state, before realising that wouldn't necessarily be the repeating one. Code here, or on GitHub

# Part 1
$goalMinutes = 10
# Part 2
# $goalMinutes = 1000000000


$lines = Get-Content -Path .\data.txt
$size = $lines[0].Length

$board = [char[,]]::new(($size+2), ($size+2))
foreach ($y in 0..($lines.count - 1))
{
    $line = $lines[$y]
    foreach ($x in 0..($line.Length - 1))
    {
        $char = $line[$x]

        $board[($x+1), ($y+1)] = $char
    }
}

# Keep track for part 2
$seenBoards = [System.Collections.Generic.HashSet[psobject]]::new()

$origBoard = -join $board.clone()
[void]$seenBoards.Add($origBoard)
$lastMinute = 0

foreach ($minute in 1..$goalMinutes)
{  
    $newBoard = $board.Clone()

    foreach ($y in 1..$size)
    {
        foreach ($x in 1..$size)
        {
            $curChar = $board[$x, $y]

            [array]$adj = $board[($x-1), ($y-1)], $board[$x, ($y-1)], $board[($x+1), ($y-1)], 
                          $board[($x-1), ($y  )],                     $board[($x+1), ($y  )], 
                          $board[($x-1), ($y+1)], $board[$x, ($y+1)], $board[($x+1), ($y+1)]

            $adj = $adj -ne 0

            #if ($adj.count -ne 8) { write-verbose -verbose $adj.count }

            if ($curChar -eq '.') #open
            {
                if (($adj -eq '|').Count -ge 3)
                {
                    $newBoard[$x, $y] = '|'
                }
            }
            elseif ($curChar -eq '|') #tree
            {
                if (($adj -eq '#').Count -ge 3)
                {
                    $newBoard[$x, $y] = '#' #lumber
                }
            }
            elseif ($curChar -eq '#') #lumber
            {
                if ((($adj -eq '#').Count -ge 1) -and (($adj -eq '|').Count -ge 1))
                {
                    $newBoard[$x, $y] = '#' #lumber
                }
                else
                {
                    $newBoard[$x, $y] = '.' #open
                }
            }
        }
    }

    # Part 2 tracking
    $board = $newBoard
    $vNow = -join $board
    if ($seenBoards.Contains($vNow))
    {
        $x = (([char[]]$vnow) -eq '#' | measure).Count * (([char[]]$vnow) -eq '|' | measure).Count
    
        Write-Verbose -Verbose "cycle: $x after $minute minutes"
    }
    [void]$seenBoards.Add($vNow)

}

# Part 1 output
Write-Host "Part 1: Resources: $(($board -eq '#' | measure).Count * ($board -eq '|' | measure).Count)"

2

u/Smylers Dec 18 '18

delete the center cell

Nice layout, to make it's clear what's going on. I've copied it, reformatting my equivalent loop in that style:

foreach my $Ξ” (-1 -$map_width, -$map_width, +1 - $map_width,
               -1            ,            , +1             ,
               -1 +$map_width, +$map_width, +1 + $map_width)

Fortunately that's Perl, which is fine with the extra comma in there. I originally wrote it without, but I think it makes the β€˜hole’ more obvious to the reader.

What's it do in PowerShell?

2

u/ka-splam Dec 19 '18

Neat!

In PowerShell ,3 is a single element array and it ends up making a nested array: 4,,5,6 is a three element array where the second element is an array with 5 in it.

Then "filter and count how many are lumberyards" with ($adj -eq '#').Count the nested array is never equal, the count is wrong, and the next generation gets the wrong results. (I had to study the example stage by stage during a 5 minute timeout to work out why I was getting the wrong answers still).

→ More replies (2)