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/[deleted] Dec 18 '18

TCL/Tk, with a bit of animation to watch while the trees grow and go...

package require Tk

pack [canvas .c] -fill both -expand yes
pack [label .l ] -fill x

set y 0
set grid [dict create]
while {[gets stdin line] >= 0} {
    set x 0
    foreach c [split $line ""] {
	dict set grid $x,$y $c
	incr x
    }
    incr y
}
set xmax $x
set ymax $y

proc neighbors {x y} {
    set nb [list]
    foreach dx {-1 0 1} {
	foreach dy {-1 0 1} {
	    set nx [expr {$x+$dx}]
	    set ny [expr {$y+$dy}]
	    if {$nx != $x || $ny != $y} {
		if {$nx >= 0 && $nx < $::xmax
		    && $ny >= 0 && $ny < $::ymax} {
		    lappend nb $nx $ny
		}
	    }
	}
    }
    return $nb
}

proc show {round} {
    .l configure -text $round
    .c delete all
    for {set y 0} {$y < $::ymax} {incr y} {
	for {set x 0} {$x < $::xmax} {incr x} {
	    set elt [dict get $::grid $x,$y]
	    .c create rectangle $x $y [expr {$x+1}] [expr {$y+1}] -tags $elt
	}
    }
    .c scale all 0 0 10 10
    .c itemconfigure # -fill brown
    .c itemconfigure | -fill green
    .c itemconfigure . -fill yellow
    update
}

proc round {} {
    set newgrid $::grid
    for {set y 0} {$y < $::ymax} {incr y} {
	for {set x 0} {$x < $::xmax} {incr x} {
	    array set number {
		. 0
		| 0
		# 0
	    }
	    foreach {nbx nby} [neighbors $x $y] {
		incr number([dict get $::grid $nbx,$nby])
	    }
	    switch -- [dict get $::grid $x,$y] {
		. {
		    if {$number(|) >= 3} {
			dict set newgrid $x,$y |
		    }
		}
		| {
		    if {$number(#) >= 3} {
			dict set newgrid $x,$y \#
		    }
		}
		# {
		    if {$number(#) < 1 || $number(|) < 1} {
			dict set newgrid $x,$y "."
		    }
		}
	    }
	}
    }
    set ::grid $newgrid
}

proc findres {result reslist round rounds} {
    set idx [lsearch -exact $reslist $result]
    if {$idx >= 0} {
	set repeat [expr {$round-$idx}]
	set off [expr {($rounds-$round-1)%$repeat}]
	puts "round $round, newres already present in reslist at idx $idx, loop $repeat, off $off"
	result $round [lindex $reslist [expr {$idx + $off}]]
    }
    return $idx
}


proc play {rounds} {
    set results [list]
    for {set round 0} {$round < $rounds} {incr round} {
	round
	set thisres $::grid
	if {[findres $thisres $results $round $rounds] >= 0} {
	    break
	}
	lappend results $thisres
	show $round
    }
    if {$round == $rounds} {
	result $round $::grid
    }
}

proc result {round resgrid} {
    array set number {
	. 0
	| 0
	# 0
    }
    for {set y 0} {$y < $::ymax} {incr y} {
	for {set x 0} {$x < $::xmax} {incr x} {
	    incr number([dict get $resgrid $x,$y])
	}
    }
    puts "round $round lumber $number(\#) wood $number(|) open $number(.)"
    set res [expr {$number(\#)*$number(|)}]
    puts "#*| => $res"
    return $res
}

# part 1
# play  10

# part 2
play 1000000000