! Copyright (C) 2017 Jon Harper. ! See http://factorcode.org/license.txt for BSD license. USING: assocs combinators kernel math math.order math.statistics sequences splitting ; IN: aoc.2017.11 : biggest-absolute ( a b -- a/b ) 2dup [ abs ] bi@ [ min ] keep = [ drop ] [ nip ] if ; : (hexdist) ( a b c -- n ) biggest-absolute [ abs ] bi@ + ; : hexdist ( a b c -- n ) { { [ 3dup [ sgn ] tri@ over [ = ] 2bi@ and ] [ (hexdist) ] } { [ 2over [ sgn ] bi@ = ] [ rot (hexdist) ] } [ -rot (hexdist) ] } cond ; : p1 ( str -- dist ) "," split histogram [ [ "n" "s" ] dip [ at 0 or ] curry bi@ - ] [ [ "nw" "se" ] dip [ at 0 or ] curry bi@ - ] [ [ "ne" "sw" ] dip [ at 0 or ] curry bi@ - ] tri hexdist ; : inccount ( a b c str -- a' b' c' ) { { "n" [ [ 1 + ] 2dip ] } { "s" [ [ 1 - ] 2dip ] } { "nw" [ [ 1 + ] dip ] } { "se" [ [ 1 - ] dip ] } { "ne" [ 1 + ] } { "sw" [ 1 - ] } } case ; : p2 ( str -- dist ) "," split [ 0 0 0 ] dip [ inccount 3dup hexdist ] map supremum [ 3drop ] dip ;