From 475f075633a2b578a919999b476c2aa137ebed41 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Fri, 14 Dec 2018 11:51:26 +0000 Subject: [PATCH] Day 13 --- advent-of-code.cabal | 22 +-- data/advent13-small.txt | 7 + data/advent13-small2.txt | 8 + data/advent13-very-small.txt | 7 + data/advent13.txt | 150 +++++++++++++++ problems/day13.html | 304 +++++++++++++++++++++++++++++++ src/advent12/advent12-comonad.hs | 1 + src/advent12/advent12.hs | 1 - src/advent13/advent13.hs | 169 ++++++++++++----- 9 files changed, 611 insertions(+), 58 deletions(-) create mode 100644 data/advent13-small.txt create mode 100644 data/advent13-small2.txt create mode 100644 data/advent13-very-small.txt create mode 100644 data/advent13.txt create mode 100644 problems/day13.html diff --git a/advent-of-code.cabal b/advent-of-code.cabal index 9c96015..d45d830 100644 --- a/advent-of-code.cabal +++ b/advent-of-code.cabal @@ -146,13 +146,6 @@ executable advent11 build-depends: base >= 4.7 && < 5 , containers -executable advent11m - hs-source-dirs: src/advent11 - main-is: advent11-map.hs - default-language: Haskell2010 - build-depends: base >= 4.7 && < 5 - , containers - executable advent11naive hs-source-dirs: src/advent11 main-is: advent11-naive.hs @@ -169,9 +162,16 @@ executable advent12 , megaparsec , containers -executable life - hs-source-dirs: src/advent12 - main-is: life.hs +-- executable life +-- hs-source-dirs: src/advent12 +-- main-is: life.hs +-- default-language: Haskell2010 +-- build-depends: base >= 4.7 && < 5 +-- , comonad + +executable advent13 + hs-source-dirs: src/advent13 + main-is: advent13.hs default-language: Haskell2010 build-depends: base >= 4.7 && < 5 - , comonad + , containers \ No newline at end of file diff --git a/data/advent13-small.txt b/data/advent13-small.txt new file mode 100644 index 0000000..44ae839 --- /dev/null +++ b/data/advent13-small.txt @@ -0,0 +1,7 @@ +/->-\ +| | /----\ +| /-+--+-\ | +| | | | v | +\-+-/ \-+--/ + \------/ + \ No newline at end of file diff --git a/data/advent13-small2.txt b/data/advent13-small2.txt new file mode 100644 index 0000000..c13a054 --- /dev/null +++ b/data/advent13-small2.txt @@ -0,0 +1,8 @@ +/>-<\ +| | +| /<+-\ +| | | v +\>+/ + \ No newline at end of file diff --git a/data/advent13-very-small.txt b/data/advent13-very-small.txt new file mode 100644 index 0000000..b2742f6 --- /dev/null +++ b/data/advent13-very-small.txt @@ -0,0 +1,7 @@ +| +v +| +| +| +^ +| diff --git a/data/advent13.txt b/data/advent13.txt new file mode 100644 index 0000000..ef14585 --- /dev/null +++ b/data/advent13.txt @@ -0,0 +1,150 @@ + /----------------------------------------------------------------------------------------------------------\ + | /---------------------\ /-----------------\ | + /-------------------------\ | /--------+-----------\ | | /----------\ | | + | /----------+----+----------------------------------------+--------+-----------+-------\ | | | | | | + | /------------+\ | | /-------------------------+--------+-\ | | | | | | | | + | | || | | /----+-------------------------+--------+-+---------+-------+-+-\ | | | | | + | | /--++--------\| /+---------+----+-------------------------+--------+\| | /--+-+-+---------+-+----------+-\ | | + | | | || /-++---++---------+----+-------------------------+--------+++---------+----+--+-+-+---------+-+----\ | | | | + | | | ||/-----+-++---++---------+----+--\ /------------------+--------+++---------+----+--+-+-+---------+-+----+-----+-+--+\ | +/---------+-+---------+--+++-----+-++---++---------+----+--+---+-\ | ||| | | | | | | | | | | || | +| | | | ||| | || || | | | | | /---------\ | ||| | | | | | | | | | | || | +| | | | ||| /+-++---++---------+----+--+---+-+--+---------+---+--------+++---------+----+--+-+\| | | | | | || | +| | | | ||| || || || | /+--+---+-+--+---------+---+--------+++------\ | | | ||| | \----+-----/ | || | +| /--+-+---------+--+++----++-++---++---------+---++--+---+-+--+-------\ | | ||| | | | | ||| | | | || | +| /---+--+-+---------+--+++----++-++---++->\ | || | | | | | | | ||| | | | | ||| | | | || | +| | | | | | ||| || || || | /---+---++--+---+-+--+-------+-+---+--------+++------+--+----+--+-+++---------+----\ | | || | +| | | | | | ||| || || || | v |/--++--+-\ | | | | | | ||| /---+--+----+--+-+++--------\| | | | || | +| | | | | |/-+++----++-++---++--+--+---++--++--+-+-+-+--+-------+\| | ||| | | | /--+--+-+++--------++----+-+-------+--++--\| +| | | | | /---++-+++----++-++---++--+--+---++--++--+-+-+-+--+--\ ||| | ||| | | | | | | ||| || | | | || || +| | | | | | || ||| || || || | | || || | | | | | | ||| | ||| | /-+--+-+--+--+-+++--------++----+-+---\ | || || +| | | | | | || ||| || || || | | || ||/-+-+-+-+--+--+----+++---+--------+++--+-+-+--+-+--+--+-+++--------++-\ | | | | || || +| | | | | /--+---++-+++----++-++---++--+--+---++--+++-+-+-+-+\ | | /--+++---+--------+++--+-+-+>-+-+--+--+-+++--------++-+--+-+---+---+--++\ || +| | | | | | /+---++-+++----++\|| \+--+--+---++--+++-+-+-+-++-+--+-+--+++---+--------+/| | | | | | |/-+-+++--------++-+--+-+---+-\ | ||| || +| | | | | | || || ||| ||||| | | | || ||| | | | || | | | ||| | /------+-+--+-+-+--+-+--++-+-+++--------++-+\ | | | | | ||| || +| | | | | | || \+-+++----+++/| \--+--+---++--+++-+-+-+-++-+--+-+--+++---+-+------+-+--+-+-+--+-+--++-+-+++--------++-++-+-+-<-+-+-+--+++-+/ +| \---+--+-+--+-++----+-+++----+++-+-------/ | /++--+++-+-+-+-++-+--+\| ||| | | \-+--+-+-+--+-+--++-+-/|| || || | | | | | ||| | +| | | | | || | ||| ||| | | ||| ||| | | | || | ||| ||| | | | /+-+-+--+-+--++-+--++--------++-++-+-+--\| | | ||| | +| | | | | || | ||| ||| | | ||| ||| | | | || | ||| ||| | | | || | | | | || | || || || | | || | | ||| | +| | | | | || | ||| ||| | | ||| ||| | | | || | ||| ||| | | /-----+-++-+-+--+-+--++-+--++------\ || || | | || | | ||| | +| | | | |/++----+-+++----+++-+----------+--+++--+++-+-+-+-++-+--+++--+++---+-+--+-----+-++-+-+--+-+--++-+-\|| | || || | | || | | ||| | +| |/-+-+--++++----+-+++----+++-+----------+--+++--+++-+-+-+\|| | ||| ||| | | | | ||/+-+--+-+--++-+-+++-----\| || ||/+-+--++-+-+--+++\| +|/-----++-+-+--++++----+-+++----+++-+----------+--+++\ ||| | | |||| | ||| ||| | | | /---+-++++-+--+-+--++-+-+++-----++-++-++++-+--++-+-+--+++++\ +|| || | | ||||/---+-+++----+++-+----------+\ |||| ||| | | |||| | ||| ||| \-+--+-+---+-++++-+--/ | || | ||| || || |||| | || | | |||||| +|| || | | ||||| | ||| |||/+----------++-++++-+++-+-+-++++-+--+++--+++-----+--+-+---+-++++-+----+--++-+-+++-----++-++-++++-+--++-+\| |||||| +|| || | | ||||| | ||| ||||| || ^||| ||| | | |||| | ||| ||| | | | | |||| | | || | ||| || || |||| | || ||| |||||| +|| || | | ||||| | ||| /+++++----------++-++++-+++-+-+-++++-+--+++--+++-----+--+-+---+-++++-+----+--++-+-+++-----++-++-++++\| || ||| |||||| +|| || | |/-+++++---+-+++-\ |||||| || |||| ||| | | |||| | ||| ||| | | | | |||| | | || | ||| || || |||||| || ||| |||||| +|| || | || ||||| | ||| | |||||| ||/++++-+++-+-+-++++-+-\||| ||| | | | /+-++++-+----+--++-+-+++-----++\|| |||||| || ||| |||||| +|| || | || ||||| | ||| | |||||| /-----+++++++-+++-+-+-++++-+-++++--+++-----+--+-+--++-++++-+----+--++-+-+++-----+++++-++++++--++-+++\ |||||| +|| || | || ||||| | ||| | |||||| | ||||||| ||| | | |||| | |||| ||| | | | || |||| | | || | ||| ||||| |||||| || |||| |||||v +|| || | || ||||| | ||| | |||||| /+-----+++++++-+++-+-+-++++-+-++++--+++-----+--+-+--++-++++-+----+--++\| ||| ||||| |||||| || |||| |||||| +|| || | || ||||| | ||| | |||||| || ||||||| ||| | | |||| | |||| ||| | | \--++-++++-+----+--++++-+++-----+++++-++++++--++-++++-+++++/ +|| || | || ||||| | ||| | |||||| || ||||||| |||/+-+-++++-+-++++--+++----\| | || |||| | | |||| ||| ||||| |||||| || |||| ||||| +|| || | || ||||| | ||| | |||||| || ||||||| \++++-+-++++-+-++++--+++----++--+----++-++++-/ | |||| ||| ||||| |||||| || |||| ||||| +|| || \-++-+++++---+-+++-+-+++++/ || /+++++++--++++-+-++++-+-++++--+++----++--+----++-++++------+--++++-+++-----+++++-++++++--++\|||| ||||| +|| || || ||||| | |||/+-+++++----++----++++++++--++++-+-++++-+-++++--+++--\ || |/---++-++++------+--++++-+++-----+++++-++++++-\||||||| ||||| +|| || || ||||| | ||\++-+++++----++----++++++++--+++/ | |||| |/++++--+++--+-++\ || /-++-++++------+--++++-+++----\||||| |||||| |||||||| ||||| +|| || || ||||| | || || ||||| || |||||||| ||| | |||| |||||| ||| | ||| || | || |||| | |||| ||| |||||\-++++++-++++++++-/|||| +|| /--++---++-+++++---+-++-++-+++++--\ || |||||||| ||| /+-++++-++++++--+++--+-+++-++-+-++-++++------+--++++-+++----+++++\ |||||| |||||||| |||| +|| | || ||/+++++---+-++-++-+++++--+-++----++++++++--+++-++-++++-++++++--+++--+-+++-++-+-++\|||| | |||| ||| |||||| |||||| |||||||| |||| +|| | || |||||||| | || || ||||| | || |||||||| ||| || |||| |||||| ||| | ||| || | |||||\+------+--++++-+++----+/|||| |||||| |||||||| |||| +|| | || |||||||| |/++-++-+++++--+-++----++++++++--+++-++-++++-++++++--+++--+\||| || | ||||| | | |||| ||| | |||| |||||| |||||||| |||| +|| | || |||||||| |||| || ||||| | || /++++++++--+++-++-++++-++++++--+++--+++++-++-+-+++++-+--\ | |||| ||| | |||| |||||| |||||||| |||| +|| | || |||||||| ||^| || ||||| | || /-+++++++++--+++-++-++++-++++++--+++--+++++-++-+-+++++-+--+---+--++++-+++---\| |||| |||||| |||||||| |||| +|| | || |||||||| |||| || ||||| | || | ||||||||| ||| || |||| |||||| ||| ||||| || | ||||| | | | |||| ||| || |||| |||||| |||||||| |||| +|| | /++---++++++++---++++-++-+++++--+-++-+-+++++++++--+++-++-++++-++++++--+++-\||||| || | ||||| | | | |||| ||| || |||| |||||| |||||||| |||| +|| | ||| |\++++++---++++-+/ ||||| | || | ||||||||| ||| || |||| |||||| ||| |||||| ||/+-+++++-+--+---+--++++-+++---++\|||| |||||| |||||||| |||| +|| /+-+++---+-++++++---++++-+--+++++--+-++-+-+++++++++--+++-++-++++-++++++--+++-++++++-++++-+++++-+--+---+\ |||| ||| ||||||| |||||| |||||||| |||| +|| || |||/--+-++++++---++++-+--+++++--+-++-+-+++++++++--+++-++-++++-++++++--+++-++++++-++++\||||| | | || |||| ||| ||||||| |||||| |||||||| |||| +|| || |||| | |||||| |||| | ||||| | || | ||||||||| ||| || |||| |||||| ||| |||||| |||||||||| | | \+-++++-+++---+++++++-++++++-++++++++--+++/ +|| || |||| | |||||| |||| | ||||| | || | ||||||||| ||| || |||| |||||| ||| |||||| |||||||||| | | | |||| ||| ||||||| |||||| |||||||| ||| +|| || |||| | |||||| |||| | ||||| | || | ||||||||| \++-++-++++-++++++--+++-++++++-++++++/||| | | | |||| ||| |||||||/++++++-++++++++--+++-\ +|| || |||| | |||||| |||| | ||||| | |\-+-+++++++++---++-++-++++-++++++--+++-++++++-++++++-+++-+--+----+-++++-+++->-++++++++++++++-+++++++/ ||| | +|| || |||| | |||||\---++++-+--+++++--+-+--+-+++/||||| || ||/++++-++++++-\||| |||||| |||||| ||| | | | |||| ||| |||||||||||||| ||||||| ||| | +|| || |||| | |||||/---++++-+--+++++--+-+-\| ||\-+++++---++-+++++++-++++++-++++-++++++-++++++-+++-+--+----+-++++-+++---+++++++++++/|| ||||||| ||| | +|| || |\++--+-++++++---++++-+--+++++--+-+-++-++--+++++---++-+++++++<++++++-+/|| |||||| \+++++-+++-+--+----+-++++-+++---+++/||||||\-++-+++++++---++/ | +|| || | || | |||||| |||| | ||||| | | || || ||||| || ||||||| |\++++-+-++-+++++/ ||||| ||| | | | |||| ||| ||| |||||| || ||||||| || | +|| || | || | |||||| /++++-+--+++++--+-+-++-++\ ||||| || ||||||| |/++++-+-++-+++++---+++++-+++-+--+----+-++++-+++---+++-++++++\ || ||||||| || | +|| ||/+-++--+-++++++--+++++-+--+++++--+-+-++-+++-+++++---++-+++++++-++++++\| || ||||| ||||| ||| | | | |||| ||| ||| ||||||| || ||||||| || | +|| |||| || | |||||| ||||| | ||||| | | || ||| ||||| || ||||||| |||||\++-++-+++++---+++++-+++-+--+----+-++++-+++---+++-+++++++-++-+++++++---+/ | +|| |||| || | |||||| ||||| | ||||| |/+-++-+++-+++++---++\||||||| ||||| || || ||||| ||||| ||| | | | |||| ||| ||| ||||||| || ||||||| | | +|| |||| || | |||||| /+++++-+--+++++--+++-++-+++-+++++---++++++++++-+++++-++-++-+++++---+++++-+++-+--+\ | |||| ||| ||| ||||||| || ||||||| | | +|| |||| || ^ |||||| |||||| | ||||| ||| || ||| ||||| /-++++++++++-+++++-++-++-+++++---+++++-+++-+--++\/-+-++++-+++---+++-+++++++-++-+++++++--\| | +|| |||| || | \+++++-++++++-+--+++++--+++-++-+++-+++++-+-++++++++++-+++++-++-++-+++++---+++++-/||/+--++++-+-++++-+++-\ ||| ||||||| || ||||||| || | +|| |||| || | \++++-++++++-+--+++++--+++-++-+++-+++++-+-+++++++++/ ||||| || || ||||| ||||| |||| |||| | |||| ||| | ||| ||||||| || ||||||| || | +|| |||| || | |||| |||||| | ||||| ||| |\-+++-+++++-+-+++++++++--+++++-++-++-+++++---+++++--++++--++++-+-++++-+++-+-/|| ||||||| || ||||||| || | +|| |||| || | |||| |||||| | ||||| |||/+--+++-+++++-+-+++++++++--+++++-++-++-+++++---+++++--++++--++++-+-++++-+++-+--++-+++++++-++-+++++++\ || | +|| |||| || | |||| |||||| | ||||| ||||| ||| ||||| | ||||||||| ||||| || || ||||| ||||| |||| |||| | |||| ||| | || ||||||| || |||||||| || | +|| |||| || /+---++++-++++++-+--+++++--+++++\ ||| ||||| | ||||||||| ||||| || || ||||| ||||| |||| |||| | |||| ||| | || ||||||| || |||||||| || | +|| |||| || || |||| |||||| | |\+++--++++++-+++-+++++-+-+++++++++--+++++-++-++-+++++---+++++--++++--++++-+-++++-+/| | || ||||||| || |||||||| || | +|| |||| \+-++---++++-++++++-+--+-+++--++++++-+++-+++++-+-+++++++/| ||||| || || ||||| ||||| |||| |||| | |||| | | | || ||||||| || |||||||| || | +|| |||| | || |||| |||||| | \-+++--++++++-+++-+++++-+-+++++++-+--+++++-++-++-+++++---+++++--++++--++++-+-++++-+-+-+--++-+++++++-/| |||||||| || | +|| |||| | || |||| |||||| | ||| |||||| ||| ||||| | ||||||| | ||||| || || ||||\---+++++--++++--++++-+-++++-+-+-+--++-+++++/| | |||||||| || | +|| |||| /+-++---++++-++++++-+----+++--++++++\||| ||||| | ||||||| | ||||| || || |||| ||||| |||| |||| | \+++-+-+-+--++-+++++-+--+-++++++/| || | +|| |||| || || |||| |||||| | ||| |||||||||| ||||| | ||||||| | ||||| || || |||| ||||| |||| |||| | ||| | | | || ||||| | | |||||| | || | +|| |||| || || |||| |||||| | ||| |||||||||| ||||| | ||||||| | ||||| || || |||| ||||| |||| |||| | ||| | | | || ||||| | | |||||| | || | +|| |||| || || |||| |||||| | ||| |||||||||| ||||| | ||||||| | ||||| || || |||| ||||\--++++--++++-+--+++-+-+-+--++-/|||| | | |||||| | || | +|| |||| || || |||| |||||| | ||| |||||||||| ||||| | ||||||| | ||||| || || |||| |||| |||| |||| | ||| | | | || |||| | | |||||| | || | +|| |||| || || |||| |||||| | ||| |||||||||| ||||| | ||||||| | ||||| || || |||| |||| |\++--++++-+--+++-+-+-+--++--/||| | | |||||| | || | +|| |||| || || |||| |||||| | ||| |||||||||| |\+++-+-+++++++-+--++++/ || || |||| /-++++---+-++--++++-+--+++-+-+-+--++---+++-+--+-++++++-+-++--\| +|| |||| || || |||| |||||| | ||| |||||||||| | ||| | ||||||| | |||| || || |||| | |||| | || |||| | ||| | | | || ||| | | |||||| | || || +|| |||| || || |||| |||||| | ||| |||||||||| | ||| | ||||||| | /++++--++-++-++++--+-++++---+-++--++++-+--+++-+-+-+--++---+++-+--+-++++++-+\|| || +|| ||||/++-++---++++-++++++-+---\\++--++++++++++-+-+++-+-+++++++-+-+++++--++-++-++++--+-++++---+-++--++++-+--+++-+-+-+--++---+++-+--/ |||||| |||| || +|| ||||||| || |||| |||||| | | || |||||||||| | ||| | ||||||\-+-+++++--++-++-++++--+-++++---+-++--++++-+--+++-+-+-+--++---+++-+----++++++-+++/ || +|| ||||||| || |||| \+++++-+---+-++--++++++++++-+-+++-+-++++++--+-+++++--++-++-++++--+-++++---+-++--+/|| | ||| | | | || ||| | |||||| ||| || +|| ||||||| || |||| ||||| | | || |||||||||| | ||| | |||||| | ||||| || || |||| | |||| | |^ | || | ||| | | | || |\+-+----++++++-+++---+/ +|| |||\+++-++---++++--+++++-+---+-++--++++++++++-+-+++-+-++++++--+-+++++--++-++-/||| | |||| | || | || | ||| | | | || | | | |||||| ||| | +|| ||| ||| || |||| ||||| | | || |\++++++++-+-+++-+-++/||| | ||||| || || ||| | |||| | || | || | ||| | | | || | v | |||||| ||| | +|| ||| ||| || |||| |||\+-+---+-++--+-++++++++-+-+++-+-++-+++--+-+++++--++-++--+++--+-++++---+-++--+-++-+--++/ | | | || | | | |||||| ||| | +|| ||| ||| || |||| ||| | | | || | |||||||| | ||| | || ||| | |\+++--++-+/ ||| | |||| | || |/++-+--++--+-+-+--++---+-+-+-\ |||||| ||| | +|| ||| ||| || |||| ||| | | | || | |||||||| | ||| | || ||\--+-+-+++--+/ | ||| | |||| | || |||| | || | | |/-++---+-+-+-+--++++++-+++-\ | +|| ||| |||/++---++++--+++-+\| | || | |||||||| | ||| | \+-++---+-+-+++--+--+---+++--+-++++---+-++--++++-+--++--+-+-++-++---+-//+-+--++++++-+++-+\| +|| ||| |||||| |||| ||| ||| | || | |||||||| | ||| | | || | | ||| | | ||| | |||| | || |||| | || | | || || | || | |||||| ||| ||| +\+-+++-++++++---++++--+++-+++---+-++--+-++++++++-+-+++-+--+-++---/ | ||| | | ||| | |||| | || |||| | ||/-+-+-++-++---+\ || | |||||| ||| ||| + | |\+-++++++---++++--+++-+++---+-++--/ ||||||\+-+-+++-+--+-++-----+-+++--+--+---+++--+-++++---+-++--++++-+--+++-+-+-++-++---++-++-+--+++/|| ||| ||| + | | | |||||\---++++--+++-/|| | || |||||| | | ||| | | || | ||| | | ||| | |||| /-+-++--++++-+--+++-+-+-++-++---++-++-+--+++-++\||| ||| + | | | ||||| /++++--+++--++---+-++----++++++-+-+\\++-+--+-++-----+-+++--+--+---+++--+-++++-+-+-++--++++-+--+++-+-/ || || || || | ||| |||||| ||| + | | | ||||| |||\+--+++--++---+-++----++++++-+-++-++-+--+-++-----+-++/ | | |||/-+-++++-+-+-++--++++\| ||| | || || || || | ||| |||||| ||| + | | | ||||| ^|| | ||\--++---+-++----++++++-+-++-++-+--+-++-----+-++---+--+---+/|| | |||| | \-++--++++++--+++-+---++-++---++-++-+--+/| |||||| ||| + | | | ||||\---+++-+--++---++---+-++----+++/|| | || || | |/++-----+-++---+--+---+-++-+\|||| | || |||||| ||| | || || || || | | | |||||| ||| + | | | |||| ||| | || || | || \++-++-+-++-++-+--++++-----+-++---+--+---+-++-++++++-+---++--++++++--+/| | || || || \+-+--+-+-++++++-+/| + | | | |||| ||| | || || | || || || | || || | \+++-----+-++---+--+---+-/| |||||| | || |||||| | | | || || || | | | | |||||| | | + | | | |||| ||| | |\---++---+-++-----++-++-+-++-++-+---+++-----+-++---+--/ | | |||||| | || |||||| | | | || || || | | | | |||||| | | + | | | |\++----+++-+--+----++---+-++-----++-/| | || \+>+---++/ \-++---+------+--+-++++++-+---++--++++++--+-+-+---++-++---++--+-+--+-+-++++/| | | + | | | | || ||\-+--+----++---+-/| || | | || | | || || | | | |||||| | || |||||| | | | || || || | | | | |||| | | | + | | | | || /-++--+--+----++---+--+-----++--+-+-++--+\| || || | | | |||||| | || |||||| | | | || || /++--+-+--+-+-++++-+-+\| + | | | | || | || |/-+----++---+--+-----++--+-+-++--+++---++--------++---+------+--+-++++++-+--\|| |||||| | | | || || ||| | | | | |||| | ||| + | | | | || | || || | || | | || | ^ || ||| || || | | | |||||| | ||| |||||| | | | || || ||| | | | | |||| | ||| + | | | | || | || || | || | | || | | || ||| || || | | | |||||| | ||\--++++++--+-+-+---++-++--+++--+-+--+-/ |||| | ||| + | | | | || | \+--++-+----++---+--+-----++--+-+-+/ ||| || /------++---+------+--+-++++++-+--++<--++++++--+-+-+---++-++--+++--+-+--+---++++-+-+++-\ + | | | | || | | || | || | | || | | | ||| || | || | | | |||\++-+--++---++++++--+-+-+---++-+/ ||| | | | |||| | ||| | + \-+-+-+-++--+--+--++-+----++---+--+-----++--+-+-+---/|| || | || | | | ||| || | || |||||| | | | |\-+---+++--+-+--+---++++-+-/|| | + | | | || | | || | || | | || | | \----++---++-+------+/ | | | ||| || | || |\++++--+-+-+---+--+---+++--+-/ | |||| | || | + | | | || | | || | || | | || | | || || | | | | | ||| || | || | |||| \-+-+---+--+---+++--+----+---/||| | || | + | \-+-++--+--+--++-+----++---+--+-----++--+-+------++---++-+------+----/ | | ||| || | |\---+-++++----+-+---/ | ||| | | ||| | || | + | | || | | || | || | | || | | || || | | | | ||| || | | /+-++++----+-+------+---+++--+----+---\||| | || | + | | || | | || | || | | || | | || || | | | | ||| || | | || |\++----+-+------+---+++--+----+---++++-/ || | + | | || | | || | || | \-----++--+-+------++---++-+------+-----------+--+-+++-++-+--+---++-+-++----+-+------+---+++--+----+---+/|| || | + | | || | | || | || | || | | |\---++-+------+-----------+--+-+++-++-+--+---++-/ || | | | ||| | | | || || | + | | || | | || | || | || \-+------+----++-+------+-----------+--+-+++-++-+--+---+/ || \-+------+---++/ | | | || || | + | | || | | || | || | || | | |\-+------+-----------+--+-+++-++-+--+---+----++------+------+---+/ | | | || || | + | | || | | || | || | || | | | | | | \-+++-++-+--+---+----/| | | | | | | || || | + | | || | | || \----++---+--------++----/ | | | | | /+++-++-+--+---+-----+------+------+---+\ | | | || || | + | | || | | \+------++---+--------+/ /-----+----+--+------+-----------+--\|\++-++-+--+---+-----+------+------+---++---+----+---+-++----+/ | + | | |\--+--+---+------/| | \------+-----+----+--+------+-----------+--++-++-++-+--+---+-----+------+------+---++---+----+---+-+/ | | + | | | | \---+-------+---+--->-----------+-----+----+--+------+-----------+--++-++-++-+--+---+-----+------/ | \+---+----+---+-+-----/ | + | | | | | | | | | | | | | || || || | | | | | | | | | | | + | | | | \-------+---+---------------+-----+----+--+------+-----------+--++-++-++-+--/ | | | | | | | | | + | | | \--------------+---+---------------+-----/ | | | | || || || \------+-----+-------------+----+---+----+---+-/ | + | | | | | | \--+------+-----------+--++-/| || | | | | | | | | + | | | | | | | | | || | \+--------+-----+-------------/ | | | | | + | | | | | | | | | || | | | | | | | | | + \---+-+------------------+---+---------------+-------------+------+-----------+--++--+--+--------+-----/ | | | | | + | \------------------+---+---------------+-------------+------+-----------+--++--+--/ | | | | | | + | | | \-------------+------+-----------+--/| \-----------+------------------------+---+----/ | | + | | | | \-----------+---+--------------+------------------------+---/ | | + | \---+-----------------------------+------------------/ \--------------+------------------------/ | | + \------------------------/ | | | | + | \-------------------------------------/ | + \--------------------------------------------------------------------------------------/ diff --git a/problems/day13.html b/problems/day13.html new file mode 100644 index 0000000..b77f442 --- /dev/null +++ b/problems/day13.html @@ -0,0 +1,304 @@ + + + + +Day 13 - Advent of Code 2018 + + + + + + + +

Advent of Code

Neil Smith (AoC++) 26*

       y(2018)

+ + + +
+

--- Day 13: Mine Cart Madness ---

A crop of this size requires significant logistics to transport produce, soil, fertilizer, and so on. The Elves are very busy pushing things around in carts on some kind of rudimentary system of tracks they've come up with.

+

Seeing as how cart-and-track systems don't appear in recorded history for another 1000 years, the Elves seem to be making this up as they go along. They haven't even figured out how to avoid collisions yet.

+

You map out the tracks (your puzzle input) and see where you can help.

+

Tracks consist of straight paths (| and -), curves (/ and \), and intersections (+). Curves connect exactly two perpendicular pieces of track; for example, this is a closed loop:

+
/----\
+|    |
+|    |
+\----/
+
+

Intersections occur when two perpendicular paths cross. At an intersection, a cart is capable of turning left, turning right, or continuing straight. Here are two loops connected by two intersections:

+
/-----\
+|     |
+|  /--+--\
+|  |  |  |
+\--+--/  |
+   |     |
+   \-----/
+
+

Several carts are also on the tracks. Carts always face either up (^), down (v), left (<), or right (>). (On your initial map, the track under each cart is a straight path matching the direction the cart is facing.)

+

Each time a cart has the option to turn (by arriving at any intersection), it turns left the first time, goes straight the second time, turns right the third time, and then repeats those directions starting again with left the fourth time, straight the fifth time, and so on. This process is independent of the particular intersection at which the cart has arrived - that is, the cart has no per-intersection memory.

+

Carts all move at the same speed; they take turns moving a single step at a time. They do this based on their current location: carts on the top row move first (acting from left to right), then carts on the second row move (again from left to right), then carts on the third row, and so on. Once each cart has moved one step, the process repeats; each of these loops is called a tick.

+

For example, suppose there are two carts on a straight track:

+
|  |  |  |  |
+v  |  |  |  |
+|  v  v  |  |
+|  |  |  v  X
+|  |  ^  ^  |
+^  ^  |  |  |
+|  |  |  |  |
+
+

First, the top cart moves. It is facing down (v), so it moves down one square. Second, the bottom cart moves. It is facing up (^), so it moves up one square. Because all carts have moved, the first tick ends. Then, the process repeats, starting with the first cart. The first cart moves down, then the second cart moves up - right into the first cart, colliding with it! (The location of the crash is marked with an X.) This ends the second and last tick.

+

Here is a longer example:

+
/->-\        
+|   |  /----\
+| /-+--+-\  |
+| | |  | v  |
+\-+-/  \-+--/
+  \------/   
+
+/-->\        
+|   |  /----\
+| /-+--+-\  |
+| | |  | |  |
+\-+-/  \->--/
+  \------/   
+
+/---v        
+|   |  /----\
+| /-+--+-\  |
+| | |  | |  |
+\-+-/  \-+>-/
+  \------/   
+
+/---\        
+|   v  /----\
+| /-+--+-\  |
+| | |  | |  |
+\-+-/  \-+->/
+  \------/   
+
+/---\        
+|   |  /----\
+| /->--+-\  |
+| | |  | |  |
+\-+-/  \-+--^
+  \------/   
+
+/---\        
+|   |  /----\
+| /-+>-+-\  |
+| | |  | |  ^
+\-+-/  \-+--/
+  \------/   
+
+/---\        
+|   |  /----\
+| /-+->+-\  ^
+| | |  | |  |
+\-+-/  \-+--/
+  \------/   
+
+/---\        
+|   |  /----<
+| /-+-->-\  |
+| | |  | |  |
+\-+-/  \-+--/
+  \------/   
+
+/---\        
+|   |  /---<\
+| /-+--+>\  |
+| | |  | |  |
+\-+-/  \-+--/
+  \------/   
+
+/---\        
+|   |  /--<-\
+| /-+--+-v  |
+| | |  | |  |
+\-+-/  \-+--/
+  \------/   
+
+/---\        
+|   |  /-<--\
+| /-+--+-\  |
+| | |  | v  |
+\-+-/  \-+--/
+  \------/   
+
+/---\        
+|   |  /<---\
+| /-+--+-\  |
+| | |  | |  |
+\-+-/  \-<--/
+  \------/   
+
+/---\        
+|   |  v----\
+| /-+--+-\  |
+| | |  | |  |
+\-+-/  \<+--/
+  \------/   
+
+/---\        
+|   |  /----\
+| /-+--v-\  |
+| | |  | |  |
+\-+-/  ^-+--/
+  \------/   
+
+/---\        
+|   |  /----\
+| /-+--+-\  |
+| | |  X |  |
+\-+-/  \-+--/
+  \------/   
+
+

After following their respective paths for a while, the carts eventually crash. To help prevent crashes, you'd like to know the location of the first crash. Locations are given in X,Y coordinates, where the furthest left column is X=0 and the furthest top row is Y=0:

+
           111
+ 0123456789012
+0/---\        
+1|   |  /----\
+2| /-+--+-\  |
+3| | |  X |  |
+4\-+-/  \-+--/
+5  \------/   
+
+

In this example, the location of the first crash is 7,3.

+
+

Your puzzle answer was 41,17.

--- Part Two ---

There isn't much you can do to prevent crashes in this ridiculous system. However, by predicting the crashes, the Elves know where to be in advance and instantly remove the two crashing carts the moment any crash occurs.

+

They can proceed like this for a while, but eventually, they're going to run out of carts. It could be useful to figure out where the last cart that hasn't crashed will end up.

+

For example:

+
/>-<\  
+|   |  
+| /<+-\
+| | | v
+\>+</ |
+  |   ^
+  \<->/
+
+/---\  
+|   |  
+| v-+-\
+| | | |
+\-+-/ |
+  |   |
+  ^---^
+
+/---\  
+|   |  
+| /-+-\
+| v | |
+\-+-/ |
+  ^   ^
+  \---/
+
+/---\  
+|   |  
+| /-+-\
+| | | |
+\-+-/ ^
+  |   |
+  \---/
+
+

After four very expensive crashes, a tick ends with only one cart remaining; its final location is 6,4.

+

What is the location of the last cart at the end of the first tick where it is the only cart left?

+
+

Your puzzle answer was 134,117.

Both parts of this puzzle are complete! They provide two gold stars: **

+

At this point, you should return to your advent calendar and try another puzzle.

+

If you still want to see it, you can get your puzzle input.

+

You can also this puzzle.

+
+ + + + + + \ No newline at end of file diff --git a/src/advent12/advent12-comonad.hs b/src/advent12/advent12-comonad.hs index c566d75..77f4cc0 100644 --- a/src/advent12/advent12-comonad.hs +++ b/src/advent12/advent12-comonad.hs @@ -1,6 +1,7 @@ {-# LANGUAGE OverloadedStrings, DeriveFunctor #-} -- Heavily based on https://gist.github.com/gatlin/21d669321c617836a317693aef63a3c3 +-- See also https://www.schoolofhaskell.com/user/edwardk/cellular-automata/part-3 -- import Data.List diff --git a/src/advent12/advent12.hs b/src/advent12/advent12.hs index cbf75db..67bfef1 100644 --- a/src/advent12/advent12.hs +++ b/src/advent12/advent12.hs @@ -29,7 +29,6 @@ part1 rules row = sum $ (iterate (\r -> applyRules rules r) row)!!20 part2 :: [Rule] -> Pots -> Integer --- part2 rules pots = (length differentQuads, steadyDiff, sum la, sum lb, sum lc, sum ld)-- (fromIntegral (sum la)) + steadyDiff * remainingGenerations part2 rules pots = (fromIntegral (sum lc)) + steadyDiff * remainingGenerations where rows = (iterate (\r -> applyRules rules r) pots) rowQuads = zip4 rows (drop 1 rows) (drop 2 rows) (drop 3 rows) diff --git a/src/advent13/advent13.hs b/src/advent13/advent13.hs index 354d24a..c1bf4d8 100644 --- a/src/advent13/advent13.hs +++ b/src/advent13/advent13.hs @@ -1,63 +1,140 @@ {-# LANGUAGE OverloadedStrings #-} +import Prelude hiding (Left, Right) import Data.List -import qualified Data.Set as S +import Data.Tuple (swap) +import qualified Data.Map.Strict as M +import Data.Map.Strict ((!)) -data Cell = Empty | Horizontal | Vertical | Clockwise | Anticlockwise | Junction deriving (Show, Eq) +import Debug.Trace + +type Coord = (Int, Int) -- x, y +data Cell = Horizontal | Vertical | TopLeft | TopRight | Junction deriving (Show, Eq) data Direction = Up | Right | Down | Left deriving (Show, Eq, Ord, Enum, Bounded) +data Decision = Anticlockwise | Straight | Clockwise deriving (Show, Eq, Ord, Enum, Bounded) +data Cart = Cart Direction Decision deriving (Eq, Show) +type Layout = M.Map Coord Cell +type Carts = M.Map Coord Cart + main :: IO () main = do - text <- TIO.readFile "data/advent12.txt" - let (initial, rules) = successfulParse text - let row = makeWorld 0 initial - print $ part1 rules row - print $ part2 rules row - - + text <- readFile "data/advent13.txt" + let (layout, carts) = parse text + -- print carts + -- print layout + -- print $ propogateUntilCollision (orderedCarts carts) layout carts + putStrLn $ showCoord $ part1 carts layout + putStrLn $ showCoord $ part2 carts layout + + +part1 :: Carts -> Layout -> Coord +part1 carts layout = collisionSite + where (collisionSite, _, _, _) = propogateUntilCollision (orderedCarts carts) layout carts + +part2 :: Carts -> Layout -> Coord +part2 carts layout = propogateUntilOne (orderedCarts carts) layout carts + +showCoord :: Coord -> String +showCoord (x, y) = show x ++ "," ++ show y + + +-- Parsing +parse :: String -> (Layout, Carts) +parse text = foldl' parseRow (M.empty, M.empty) $ zip [0..] (lines text) + +parseRow (layout, carts) (y, row) = foldl' parseCellWithY (layout, carts) $ zip [0..] row + where parseCellWithY = parseCell y + +parseCell y (layout, carts) (x, cell) = + let here = (x, y) + in case cell of + '-' -> (M.insert here Horizontal layout, carts) + '|' -> (M.insert here Vertical layout, carts) + '\\' -> (M.insert here TopLeft layout, carts) + '/' -> (M.insert here TopRight layout, carts) + '+' -> (M.insert here Junction layout, carts) + '^' -> (M.insert here Vertical layout, M.insert here (Cart Up Anticlockwise) carts) + 'v' -> (M.insert here Vertical layout, M.insert here (Cart Down Anticlockwise) carts) + '<' -> (M.insert here Horizontal layout, M.insert here (Cart Left Anticlockwise) carts) + '>' -> (M.insert here Horizontal layout, M.insert here (Cart Right Anticlockwise) carts) + _ -> (layout, carts) + + +-- Moving + +-- first argument is the carts left to move this tick. +propogateUntilCollision :: [Coord] -> Layout -> Carts -> (Coord, Coord, [Coord], Carts) +-- propogateUntilCollision cs _ carts | trace ("pUC " ++ show cs ++ " " ++ show carts) False = undefined +-- finished this tick +propogateUntilCollision [] layout carts = + if M.size carts <= 1 + then (survivingCoord, survivingCoord, [], carts) -- empty coords list asserts finished a tick with only one cart remaining. + else propogateUntilCollision (orderedCarts carts) layout carts -- start the next tick + where survivingCoord = head $ M.keys carts +-- not finished this tick, so move this cart then move the rest +propogateUntilCollision (c:cs) layout carts = + if c' `M.member` carts + then (c', c, cs, carts) + else propogateUntilCollision cs layout carts' + where cart = carts!c + (c', cart') = moveOnce c cart layout + carts' = M.insert c' cart' $ M.delete c carts + +orderedCarts :: Carts -> [Coord] +orderedCarts carts = sortOn swap $ M.keys carts + +-- move a cart, without getting as far as collision detection +moveOnce :: Coord -> Cart -> Layout -> (Coord, Cart) +moveOnce coord (Cart direction decision) layout = (coord', (Cart direction'' decision')) + where coord' = takeStep direction coord + direction' = (curve direction (layout!coord')) + (direction'', decision') = junctionTurn (layout!coord') direction' decision + +-- keep moving carts until only one left +-- move carts until there's a collision; remove those carts then carry on +propogateUntilOne :: [Coord] -> Layout -> Carts -> Coord +propogateUntilOne coords layout carts = + if null coords' -- only when finished a tick and only one cart remaining. + then c1 + else propogateUntilOne coords'' layout carts'' + where (c1, c2, coords', carts') = propogateUntilCollision coords layout carts + carts'' = M.delete c1 $ M.delete c2 carts' + coords'' = filter (/= c1) $ filter (/= c2) coords' -- Move in the current direction -takeStep :: Direction -> Cell -> Int -> Int -> (Direction, Int, Int) -takeStep Up x y = (x, y-1) -takeStep Down x y = (x, y+1) -takeStep Left x y = ( x-1, y) -takeStep Right x y = (x+1, y) +takeStep :: Direction -> Coord -> Coord +takeStep Up (x, y) = (x, y-1) +takeStep Down (x, y) = (x, y+1) +takeStep Left (x, y) = ( x-1, y) +takeStep Right (x, y) = (x+1, y) + +curve :: Direction -> Cell -> Direction +curve Up TopLeft = Left +curve Down TopLeft = Right +curve Left TopLeft = Up +curve Right TopLeft = Down +curve Up TopRight = Right +curve Down TopRight = Left +curve Left TopRight = Down +curve Right TopRight = Up +curve d _ = d + +junctionTurn :: Cell -> Direction -> Decision -> (Direction, Decision) +junctionTurn Junction direction Anticlockwise = (predW direction, Straight) +junctionTurn Junction direction Straight = (direction, Clockwise) +junctionTurn Junction direction Clockwise = (succW direction, Anticlockwise) +junctionTurn _ direction decision = (direction, decision) -- | a `succ` that wraps -turnCW :: (Bounded a, Enum a, Eq a) => a -> a -turnCW dir | dir == maxBound = minBound - | otherwise = succ dir +succW :: (Bounded a, Enum a, Eq a) => a -> a +succW dir | dir == maxBound = minBound + | otherwise = succ dir -- | a `pred` that wraps -turnACW :: (Bounded a, Enum a, Eq a) => a -> a -turnACW dir | dir == minBound = maxBound - | otherwise = pred dir - - - --- Parse the input file - -type Parser = Parsec Void Text - -sc :: Parser () -sc = L.space (skipSome spaceChar) CA.empty CA.empty - -symb = L.symbol sc -potP = (char '.' *> pure False) <|> (char '#' *> pure True) - -initialPrefix = symb "initial state:" -ruleSepP = symb "=>" - -fileP = (,) <$> initialP <*> many ruleP -initialP = initialPrefix *> many potP <* sc -ruleP = Rule <$> ruleLHS <* ruleSepP <*> ruleRHS -ruleLHS = count 5 potP <* sc -ruleRHS = potP <* sc +predW :: (Bounded a, Enum a, Eq a) => a -> a +predW dir | dir == minBound = maxBound + | otherwise = pred dir -successfulParse :: Text -> ([Bool], [Rule]) -successfulParse input = - case parse fileP "input" input of - Left _error -> ([], []) -- TIO.putStr $ T.pack $ parseErrorPretty err - Right world -> world -- 2.34.1