diff --git a/2018/day-13/helpers.js b/2018/day-13/helpers.js new file mode 100644 index 0000000..67488fc --- /dev/null +++ b/2018/day-13/helpers.js @@ -0,0 +1,17 @@ +const fs = require('fs') +const path = require('path') +const filePath = path.join(__dirname, 'input.txt') + +const loadInput = (callback) => { + fs.readFile(filePath, { encoding: 'utf8' }, (err, data) => { + if (err) throw err + + if (typeof callback === 'function') { + callback(data) + } + }) +} + +module.exports = { + loadInput +} diff --git a/2018/day-13/input.txt b/2018/day-13/input.txt new file mode 100644 index 0000000..802a0fa --- /dev/null +++ b/2018/day-13/input.txt @@ -0,0 +1,150 @@ + /--------------------------------------------------------------------------------------------------\ + | /-------------------------------------------------------\ |/-----------\ + | /-----------------------------------+-----------------------------------\ | /++---------\ | + /-----+-+--------------\ | /--+---------------\ | ||| | | + | | |/-------------+--------------------+---\ | | | | ||| | | + | /-+-++-------------+--------------------+---+----<----------------------\| | | | ||| | | + | | | || | /------------------+---+---------------------------++--+---------------+---+---+++--\ | | + /-----------+---+-+-++-------->----+-+------------------+---+---------------------------++--+-\ /-----+--\| ||| | | | + | /----+---+-+-++-------------+-+------------------+---+------\ || | | | | || ||| | | | + | | | /+-+-++-------------+-+------------------+---+------+--------------------++--+-+-------+-----+--++---+++--+-\ | | + | | | || |/++------------\| | /+---+------+-----------\ || | | | | || ||| | | | | + | | | || |||| || | /++---+--\ | /--------+--------++--+-+-------+-----+--++---+++--+-+----+-+-------------\ + | |/---+--++\|||| || | /--+++---+--+---+--+--------+--------++--+-+-------+-----+--++---+++--+-+----+-+----\ | + | /----++---+--+++++++-----\ || | /-------+--+++---+--+---+--+--------+--------++--+-+-------+-----+--++---+++--+-+----+-+----+--------+--\ + | | || | ||||||| | || | | /-+--+++---+--+---+--+--------+--------++--+-+-------+-----+--++---+++--+-+----+-+----+---\ | | + | | || | ||||||| | || | | |/+--+++---+--+---+--+--------+--------++--+-+-------+-----+-\|| ||| | | | | | | | | + | | || | ||||||| | || | | ||| ||| | | | | | /-----++--+-+-------+-----+-+++---+++--+-+----+\| | | | | + | | || | ||||||| | || | | ||| ||| | | | | | | || | | | | ||| ||| |/+----+++----+---+--\ | | + | | || | ||||||| | || | | /--+++--+++---+--+---+--+--------+--+-----++--+-+-------+-----+-+++---+++--+++----+++-\ | | | | | + | | ||/--+--+++++++-----+------++-+-----+--+--+++--+++---+--+---+--+--------+--+-----++--+-+------\| | ||| ||| ||| ||| | | | | | | + | | ||| | ||||||| | || | | | ||| ||| | | | | | | |\--+-+------++-----/ ||| ||| ||| ||| | | | | | | +/-+-+----+++--+--+++++++-----+------++-+---\ | | ||| \++---+--/ | | | | | | | || ||| ||| ||| ||| | | | | | | +| | | ||| | ||||||| | || | | | | ||| || | | | | /+-----+---+-+------++-------+++--\||| ||| ||| |/-+---+--+-+-\| +| | | ||| | ||||||| | || | | | | ||| \+---+------+--+--------/ || /---+---+-+------++-------+++--++++--+++----+++-++-+-\ | | | || +| | | ||| | ||||||| | || | | | | ||| | | | | /--------++-+---+---+-+------++-------+++-\|||| ||| ||| || | | | | | || +| | | ||| | ||||||\-----+------++-+---+-+--+--+++----+---/ | | | /--++-+---+---+-+------++------\||| ||\++--+++----/|| || | | | | | || +| | | ||| \--++++++------+------+/ | | | | ||| | | | | | || | | | | || |||| || || ||| || || | | | | | || +| |/+----+++--\ |||||\------+------+--+---+-+--+--+++----+----------+--+-+-----+--++-+---+---/ | || |||| || || ||| || || | | | | | || +|/+++----+++--+--+++++-------+\ | | | |/-+--+++----+----------+--+-+-----+--++-+---+-----+------++------++++-++-++--+++-----++-++-+-+-+\ | | || +||||| ||| | ||||| || | | | || | ||| | | | | /--+--++-+---+-----+------++------++++-++-++--+++\ || || | | || | | || +||||| \++--+--+++++-------++-----+--+---+-++-+--+++----+----------/ | | | | || | | | /----++------++++-++-++--++++----++-++-+-+\|| | | || +||||| || | ||||| || |/-+---+-++-+--+++----+-------------+-+--+--+--++-+--\| | | || |||| || || |||| || || | |||| | | || +||||| || | ||||| || || | | || | ||| /--+-------------+-+--+--+--++-+--++-----+-+-\ || |||| || || |||| || || | |||| | | || +||||| || | ||||| || /---++-+---+-++-+-<+++-+--+-----<-------+-+--+--+--++-+--++-\ | | | || |||| || || |||| || || | |||| | | || +||||| |\--+--+++++-------++-+---++-+---+-++-+--+++-+--+-------------+-+--+--+--++-+--++-+---+-+-+--/| |||| || || |||| || || | |||| | | || +||||\-----+---+--+++++-------/| | || | | || | ||| | | | | | | || | || | | | | | |||| || || |||| || || | |||| | | || +|||\------+---/ /+++++--------+-+-\ || | | || | ||| | | | | | | || | || | | | | | |||| || || |||| || || | |||| | | || +||| | |||||| /------+-+-+-++-+---+-++-+--+++-+--+-------------+-+--+--+--++-+--++-+---+-+-+---+------++++\|| || |||| || || | |||| | | || +||| | /---++++++-+------+-+\| || | |/++-+--+++-+--+-------------+-+--+--+--++-+--++-+---+-+-+---+------+++++++-++--++++-\ || || | |||| | | || +||| | | |||||| | | ||| || | |||| | ||| | | | | | | /++-+--++-+---+-+-+-\ | /----+++++++-++--++++-+--++-++-+-++++-+-+\|| +||| | | |||||| | | ||| || |/--++++-+--+++-+--+-------------+-+--+--+-+++-+--++-+---+-+-+-+-+-+---\||||||| || |||| | || || | |||| | |||| +|||/------+-+---++++++-+---\ | ||| || || /++++-+--+++-+--+-------------+-+--+--+-+++-+--++-+\ | | | | | | |||||||| || |||| | || || | |||| | |||| +|||| | | |||||| | /-+--+-+++-++-++-+++++-+--+++-+--+-------------+-+--+--+-+++\| || || | | | | | | |||||||| |\--++++-+--+/ || | |||| | |||| +|||| | | |||||| | | | | ||| || || ||||| | ||| | | | | | | ||||| || || | \-+-+-+-+---++++++++-+---++++-+--+--++-+-+/|| | |||| +|||| | | /++++++-+-+-+--+-+++-++-++-+++++-+--+++-+--+--\ | | | | ||||| || || | | | | | |||||||| | |||| | | || | | || | |||| +|||| | | ||||||| | | | | ||| || || ||||| | ||| | | | /-+-+--+-\| ||||| || || | | | | | |||||||| | |||| | | || | | || | |||| +||||/-----+-+--+++++++-+-+-+--+-+++-++-++-+++++-+--+++-+--+--+----\ | | | |/++-+++++--++-++--+---+-+-+-+---++++++++-+--\|||| | /+--++-+-+\|| | |||| +||||| | | |||||\+-+-+-+--+-+++-++-++-+++++-+--+++-+--+--+----+---+-+-+--++++-+++++--++-++--+---+-+-+-+---++++++++-/ ||||| | || || | |||| | |||| +||||| /-+-+--+++++-+-+-+-+--+-+++-++-++-+++++\| ||\-+--+--+----+---+-+-+--++++-+++++--++-++--+---+-+-+-+---++++++++----+++++-+-++--++-/ |||| | |||| +||||| |/+-+--+++++-+-+-+-+--+-+++-++-++-+++++++--++--+--+--+-\ | |/+-+--++++-+++++--++-++--+---+-+-+-+---++++++++----+++++-+-++--++-\ |||| | |||| +||||| ||| | ||||| | | | | | ||| || || ||||||| /++--+--+--+-+--+-\ ||| | |||| ||||| || || |/--+-+-+-+---++++++++----+++++-+-++--++-+-++++\| |||| +||||| ||| | ||||| | | | | | ||| || || ||||||| ||| | | | | | | ||| | |||| ||||| || || || | | | | |||||||| ||\++-+-++--++-+-+++++/ |||| +||||| ||| | ||||| | | | |/-+-+++-++-++-+++++++-+++--+--+--+-+--+-+\||| | ||||/+++++--++-++--++--+-+-+-+---++++++++----++-++-+-++--++-+-+++++\ |||| +||||| ||| | ||||| | | | || | ||| || || ||||||| ||| | | | | | ||||| | |||||||||| ^| || || | | | | |||||||| || || | || || | |||||| |||| +||||| ||| | ||||| | | | || | ||| ||/++-+++++++-+++--+--+--+\| | ||||| | \+++++++++--++-++--++--+-+-+-+---++++++++----++-+/ | || || | |||||| |||| +||||| ||| | ||||| | | |/++-+-+++-+++++-+++++++-+++--+--+--+++--+-+++++-+---+++++++++--++-++--++--+-+-+-+---++++++++----++-+-\| || || | |||||| |||| +||||| ||| | ||||| | | |||| | ||| ||||| ||||||| |||/-+--+--+++--+-+++++-+---+++++++++--++-++--++--+-+-+\| |||||||| || | || || || | |||||| |||| +||||| |||/+--+++++-+-+-++++-+-+++-+++++-+++++++-++++-+--+--+++-\| ||||| | ||\++++++--++-++--++--+-+-+++---+/|||||| || | || || || | |||||| |||| +||||| ||||| ||||| | ^ |||| | ||| |||\+-+++++++-++++-+--+--+++-++-+++++-+---++-++++++--++-++--++--+-+-+++---+-++++++<---+/ | || || || | |||||| |||| +||||| ||||| ||||| | | \+++-+-+++-+++-+-+++++++-++++-+--+--+++-++-+++++-+---++-++++/| || || || | | ||| | |||||| | | || || || | |||||| |||| +||\++---+++++--+++++-+-+--+++-+-+++-+++-+-+++++++-++++-+--+--+++-++-+++++-+---++-++++-+--++-++--/| | | ||| | |||||| | | || || || | |||||| |||| +|| || ||||| ||||| | | ||| | ||| ||| | ||||||| |||| | | ||| || |||||/+---++-++++-+--++-++---+--+-+-+++---+-++++++----+--+-++-++--++-+\|||||| |||| +|| || ||||| ||||| | | ||| | ||| ||| | ||||||| |||| | | ||| || ||||||| || ||||/+--++-++---+--+-+-+++---+-++++++----+-\| || || || |||||||| |||| +|| || ||||| ||||| | | ||| | ||| ||| | ||||||| |||| | | ||| || ||||||| || \+++++--++-++---+--+-+-+++---+-++++++--->+-++-++-++--++-+++++++/ |||| +|| || ||||| ||||| | | ||| | ||| ||| | ||||||| |\++-+--+--+++-++-+++++++---++--+++++--++-++---+--+-+-+++---+-++++++----+-++-++-++--++-++++/|| |||| +\+-++---+++++--+++++-+-+--+++-+-+++-+++-+-+/||||| | || | | ||| || ||||||| || ||||| || || | | | ||| | |||||| | || || || || |||| || |||| +/+-++---+++++--+++++-+-+--+++-+-+++-+++-+-+-+++++-+-++-+--+-\||| || ||||||| || ||||| || || | | | ||| | |||||| | || || || || |||| || |||| +|| || ||||| ||||| | | ||| | ||| ||| | | ||||| |/++-+--+-++++-++-+++++++---++--+++++--++-++---+--+-+\||\---+-++++++----+-++-++-++--++-++++-++--+/|| +|| || ||||| ||||| | | \++-+-+++-+++-+-+-+++++-++++-+--+-++++-++-+++++++---++--+++++--++-++---+--+-++++----+-++++++----+-++-/| || || |||| || | || +|| || ||||| ||^\+-+-+---++-+-+++-+++-+-+-+++++-++++-+--+-++++-++-+++++++---++--+++++--+/ || | | |||| | ||||||/---+-++--+-++--++-++++-++\ | || +|| || ||||| ||| | | | || | ||| ||| | | ||||\-++++-+--+-++++-++-+++++++---++--+++++--+--++---+--+-++++----+-+++++++---+-++--+-++--/| |||| ||| | || +|| || ||||| ||| | | | || |/+++-+++-+\| |||| |||| | | |||| || ||||||| || ||||| | || | | |||| | ||||||| | || | || | |||| ||| | || +|| || ||||| ||| | | | || ||||| ||| ||| \+++--++++-+--+-++++-++-+++++++---++--+++++--+--++---+--+-++++----+-+++++++---+-++--/ || | |||| ||| | || +|^ || ||||| ||| | | | || ||||| ||| ||| ||| |||| | | |||| || ||||||| || ||||| | || | | |||| | ||||||| | || || | |||| ||| | || +|| || ||||| ||| | | | || ||||| ||| ||| ||| |||| | /+-++++-++-+++++++---++--+++++--+--++---+--+-++++----+-+++++++---+-++----++---+-++++-+++\| || +|| || /+++++--+++-+-+-+---++-+++++-+++-+++--+++--++++-+-++-++++-++\||||||| || |||\+--+--++---+--+-++++----+-+++++++---+-/| || | |||| ||||| || +|| || |||||| /+++-+-+-+---++\||||| ||| ||| ||| |||| | || |||| |||||||||| \+--+++-+--+--++---+--+-++++----+-+++++++---/ | || | |||| ||||| || +|| || |||||| |||| | | | |||||||| ||| ||| ||| |||| | |\-++++-++++++++++----+--+++-+--+--++---+--+-++++----+-++/|||| | || | |||| ||||| || +\+-++--++++++-++++-+-+-+---++++++++-+++-+++--+++--++++-+-+--/||| |||||||||| | \++-+--+--++---+--+-/||| /-+-++-++++-\ | || | |||| ||||| || + |/++--++++++-++++-+-+-+---++++++++-+++-+++--+++--++++\| | /+++-++++++++++----+---++-+--+-\|| | | ||| | | || ||||/+----+---\|| | |||| ||||| || + |||| |||||| |||| | | | |||||||| ||| ||| ||| |||||| | |||| |||||||||| | || | | ||| | | ||| | | || |||||| | ||| | |||| ||||| || + |||| |||||| |||| | | | |||||||| ||| ||| \++--++++++-+--++++-++++++++++----+---++-+--+-+++---+--+--+++--+-+-++-++++++----+---+++---+-++++-+++++-+/ + |||| |||\++-++++-/ | \---++++++++-+++-+++---++--++++++-+--++++-++++++++++----+---++-+--+-+++---+--+--+++--+-+-++-/||||| | ||| | |||| ||||| | + |||| ||| || |||| | |||||||| ||| ||| || |||||\-+--++++-++++++++++----+---++-+--+-+++---+--/ ||| | | || ||||| | ||| | |||| ||||| | + |||| ||| || |||| | ||||||||/+++-+++---++--+++++--+--++++-++++++++++----+---++-+--+-+++--\| ||| | | || ||||| | ||| | |||| ||||| | + |||| ||| || |||| /+-----++++++++++++-+++---++--+++++--+--++++-++++++++++----+---++-+\ | ||| || ||| | | || ||\++----+---+++---+-++++-++/|| | + |||\--+++-++-++++--++-----++++++++++++-+++---++--+++++--+--++++-+/|||||||| | || || | ||| || ||| | | || || || | ||| | |||| || || | + ||| ||| || |||| || |||||||||||| ||| || ||||| |/-++++-+-++++++++----+---++-++-+-+++--++-----+++-\| | || || || | ||| | |||| || || | + ||| ||| || |||| || |||||||||||| ||| || ||||| || |||| | |||||||| | || || | ||| || ||| || | || || || | ||| | |||| || || | + ||| ||| || |||| || |||||||||||| ||| || /+++++--++-++++-+-++++++++----+---++-++-+-+++--++-----+++-++-+-++--++-++----+---+++---+\|||| || || | + |||/--+++-++-++++--++-----++++++++++++-+++---++-++++++--++-++++-+-++++++++----+---++-++-+-+++\ || ||| || | || || || | ||| |||||| || || | + |||| ||| || |||| || |||||||||||| ||| || |||||| || |||| | |||||||| | || || | |||| || ||| || | || || || | ||| |||||| || || | + |||| ||| || |||| || |||||||||||| ||| || |||||| || |||| | |||||||| | || \+-+-++++-++-----+++-++-+-++--++-++----+---+++---++++/| || || | + |||| ||| || |||| || |||||||||||| ||| || |||||| || |||| | |||||||| | |\--+-+-++++-++-----+++-++-+-++--++-++----+---++/ |||| | || || | + \+++--+++-++-++++--++-----+++/|||||||| ||| || |||||| || |||| | |||||||| | | | | |||| |^ ||| || | || || || | || |||| | || || | + ||| ||| || |||| || ||| |||||||| ||| || ||\+++--++-++++-+-++++++++----+---+---+-+-++++-++-----/|| || | || || ^| | || |||| | || || | + ||| \++-++-++++--++-----+++-++++++++-+++---++-++-+++--++-++++-+-/||||||| | | | | |||| || || || | || || || | || |||| | || || | + ||| || || |||| || ||| |||||||| ||| || || ||| || |||| | ||\++++----/ | |/+-++++-++------++-++-+-++--++-++---\| || |||| | || || | + ||| || || |||| || ||| |||||||| ||| || || ||| || |||| | || |||| /-+---+++-++++-++------++-++-+-++--++-++---++---++----++++-+-++\|| | + ||| || || |||| || ||| |||||||| ||| || || ||| || \+++-+--++-++++------+-+---+++-/||| || || || | || || || || || |||| | ||||| | + ||| /++-++-++++--++-----+++-++++++++-+++---++-++-+++--++--+++-+--++-++++------+-+---+++--+++-++\/----++-++-+-++--++-++---++---++----++++-+\||||| | + ||| ||| || |||| || ||| |||||||\-+++---++-++-+++--++--+/| | || |||| | | ||| ||| |||| || || | || || || || || |||| ||||||| | + ||| ||| || |||\--++-----+++-+++++++--+++---++-++-+++--++--+-+-+--++-++++------+-+---+++--+++-++++----++-++-+-++--++-++---+/ || |||| ||||||| | + ||| ||| || ||| || ||| ||||||| ||| || || \++--++--+-+-+--++-++++------+-+---+++--+++-++++----++-++-+-/| || || | || |||| ||||||| | + \++--+++-++-+++---++-----+++-+++++++--+++---++-++--+/ || |/+-+--++-++++------+-+---+++--+++-++++----++\|| | | || || | || |||| ||||||| | + || ||| || ||| \+-----+++-+++++++--+++---++-++--+---++--+++-+--++-++++------+-+---/|| ||| |||| ||||| | | || || | || |||| ||||||| | + || ||| || ||| \-----+++-+++++/| ||| || || \---++--+++-+--++-++++------+-+----++--+++-++++----+/||| | | || || | || |||| ||||||| | + || ||| || ||| ||| \++++-+--+/| \+-++------++--+++-+--++-++++------+-+----++--+++-++++----+-+++-+--+--++-++---+----++----++++-++/|||| | + || ||| || ||| ||| |||| | | | | || || ||| | /++-++++------+-+-\ || ||| |||| | ||| | | || || | || |||| || |||| | + || ||| || |||/---------+++--++++-+--+-+----+-++------++--+++-+-+++-++++------+-+-+--++-\||| |||| | ||| | | || || | || |||| || |||| | + || ||| || |||| ||| |||| | \-+----+-++------++--+++-+-+++-++++------+-+-+--++-++++-++++----+-+++-/ | || || | || |||| || |||| | + || ||| || |||| ||| |||| | | | || || ||| | ||| |\++------+-+-+--++-++++-++++----+-+++----+--++-++---+----++----++++-++-+++/ | + || ||| || |||| ||| |||| \----+----+-++------++--+++-+-+++-+-++------+-+-+--+/ |||| |||| | ||| | || || | || |||| || ||| | + || ||| |\-++++---------+++--+/|| | | || || ||| | ||| | || | | | | |||| |||| \-+++----/ || || | || \+++-++-+++--/ + || ||| | |||| /------+++--+-++\ | | || || ||| | ||| | \+------+-+-+--+--++++-++++------+++-------++-++---+----++-----++/ || ||| + || ||| | |||| | ||| \-+++-----+----+-++------++--+++-+-+++-+--+------+-+-+--+--+/|| |||| ||| || || | || || || ||| + || ||| | |||| | ||| ||| | | || || ||| | ||| | | | | | | | || |||| ||| || || | || || || ||| + || ||| | |||| /+------+++----+++-----+----+-++------++--+++-+-+++-+--+------+-+-+--+--+-++-++++------+++-----\ || || | || || || ||| + || ||| | |||| || |\+----+++-----+----+-++------++--+++-+-++/ | | | |/+--+--+-++-++++------+++-----+-++-++---+----++----\|| || ||| + || ||| | |||| || | | ||| | | || || |\+-+-++--+--+------+-+++--+--+-++-++++------/|| /--+-++-++---+\ || ||| || ||| + \+--+++-+--++++-++------/ | ||| \----+-++------++--+-+-+-++--+--+------+-+++--+--+-/| |||| || | | || || || || ||| || ||| + | ||| | |\++-++--------+----+++----------+-++------++--/ | | \+--+--+------+-++/ | | | |||| || | | || || || || ||| || ||| + | ||| | | || || /---+----+++----------+\|| || | | | | | | || | | | |||| || | | || || || || ||| || ||| + | ||| | | || || | | ||| |||| || | | | | | | || \--+--+-++++-------++--+--+-++-++---/| || ||| || ||| + | ||| | | || || | | ||| |||| || | | | | | | \+------+--+-++++-------++--+--+-+/ \+----+---/| ||| || ||| + | ||| | | || || | | ||| |||| || | | | | \------+--+------+--+-++++-------++--+--+-/ | | | ||| || ||| + \--+++-+--+-++-++----+---+----+++----------++++------++----+-+--+--+----<----+--+------+--/ |||| || | | | | | ||| || ||| + \++-+--+-++-++----+---+----+++----------++++------++----+-+--+--+---------+--+------+----++/| || | | | | | ||| || ||| + || | | || || | | ||| /----++++------++----+-+--+--+---------+--+------+----++-+-------++--+-\| | | | ||| || ||| + || | | || ||/---+---+----+++-----+----++++------++----+-+--+--+---------+--+------+----++-+-------++--+-++-----+----+-\ | ||| || ||| + || | | || ||| | | ||| | |||| || | | | | \--+------+----++-+-------++--+-++-----+----+-+--+----+++--++-+/| + |\-+--+-++-+++---+---+----+++-----+----++++------++-<--/ | | | | | || | || | || | | | | ||| || | | + | | | || ||| | | ||| | |||| || | | | | | || \-------++--+-++-----+----+-+--+----+++--+/ | | + /+--+--+-++-+++---+---+----+++-----+----++++------++------+--+--+------------+----\ | || || | || | | | | ||| | | | + || | | |\-+++---+---+----+++-----+----++++------++------+--+--+------------+----+-/ || || | || | | | | ||| | | v + || | | | ||| | | |\+-----+----++++------++------+--+--+------------+----+------/| || | || | | | | ||| | | | + || | | | \++---+---+----+-+-----+----++++------++------+--+--+------------+----+-------+---------++--+-+/ | | | | ||| | | | + || | \-+---++---+---/ | | \----++++------++------+--+--+------------+----+-------+---------++--+-/ | | | | ||| | | | + || | \---++---+--------/ | |||| || | | | | | | |\--+--------/ | | | ||| | | | + || | || | | |||| || | | | \----+-------+---------+---+-------------+-+--+----/|| | | | + || \--------++---+----------+----------++++------++------/ | | | | | | | | | || | | | + || || | | |||| || | \-----------------+-------+---------+---+----<--------+-+--+-----+/ | | | + || |\---+----------+----------++++------++---------+--------------------+-------+---------+---+-------------+-/ | | | | | + || | | | |||\------++---------/ | | | | | | | | | | + |\-----------+----+----------+----------/|| || | \---------+---+-------------+----+-----+---+--/ | + \------------+----+----------+-----------++-------++------------------------------/ | | | \-----+---/ | + | | | || \+------------------------------------------------+---+-------------+----------+--------/ + | \----------+-----------/| \------------------------------------------------/ | | | + \---------------/ | \-------------/ | + \--------------------------------<-----------------------------------------------------/ diff --git a/2018/day-13/solution.js b/2018/day-13/solution.js new file mode 100644 index 0000000..c6a5aab --- /dev/null +++ b/2018/day-13/solution.js @@ -0,0 +1,23 @@ +const { loadInput } = require('./helpers') +const { Track } = require('./tracks') + +const init = (data) => { + const track = new Track(data) + while (track.collision === false) { + try { + track.advance() + } catch (err) { + console.error(`Reached a collision at ${track.collision.x},${track.collision.y} on frame ${track.frame}`) + } + } + const answer = [track.collision.x, track.collision.y] + // console.log(`Track state:`) + // console.log(track.display()) + const answer2 = '' + console.log(`-- Part 1 --`) + console.log(`Answer: ${answer}`) + console.log(`-- Part 2 --`) + console.log(`Answer: ${answer2}`) +} + +loadInput(init) diff --git a/2018/day-13/tracks.js b/2018/day-13/tracks.js new file mode 100644 index 0000000..afe89f1 --- /dev/null +++ b/2018/day-13/tracks.js @@ -0,0 +1,195 @@ +const { dynamicSortMultiple } = require('../day-04/helpers') + +class Track { + constructor (track) { + this.layout = [] + this.carts = [] + this.cartDirections = ['^', '>', 'v', '<'] + this.collision = false + this.frame = 0 + this.interSectionOrder = [-1, 0, 1] + this.trackTurns = ['\\', '/'] + this.trackTypes = this.trackTurns.concat(['-', '|', '+']) + this.setLayout(track) + } + + _isCollision (x, y) { return (this.carts.filter((c) => c.x === x && c.y === y).length > 1) } + _isIntersection (s) { return s === '+' } + _isTurn (s) { return this.trackTurns.indexOf(s) >= 0 } + + /** + * Determines the next direction for a cart rotating at an intersection + * Order of rotations is left (counterclockwise), straight, right (clockwise) + * @private + * @param {Object} cart the cart being turned + * @returns {String} value of new direction + */ + _intersect (cart) { + const i = this.interSectionOrder + let l = cart.lastIntersections + + // Figure out the new rotation + let r = i.indexOf(l[0]) + r = (r + 1 >= i.length) ? i[0] : i[r + 1] + + // Track the intersections + cart.lastIntersections.pop() + cart.lastIntersections.splice(0, 0, r) + + return this.cartDirections[this._roationDirection(this.cartDirections.indexOf(cart.direction), r)] + } + + /** + * Rotates the cart + * @private + * @param {String} s track segment the cart is now on + * @param {String} a x||y axis of travel + * @param {Number} d Index of absolute cart direction + * @returns {String} value of new direction + */ + _rotate (s, a, d) { + // Determine which way we're rotating + let r = ( + (this.trackTurns.indexOf(s) === 1 && a === 'y') || // vertical turns clockwise + (this.trackTurns.indexOf(s) === 0 && a === 'x') // horizontal turns clockwise + // (this.trackTurns.indexOf(s) === 0 && a === 'x') // horizontal turns counter-clockwise + // (this.trackTurns.indexOf(s) === 1 && a === 'y') // vertical turns counter-clockwise + ) ? 1 : -1 + // Find the value of the new direction + return this.cartDirections[this._roationDirection(d, r)] + } + + /** + * Determines the next clockwise or counter-clockwise direction + * @private + * @param {Number} d Index of current direction + * @param {Number} r +1 for clockwise (turn right), -1 for counterclockwise (turn left) + * @returns {Number} Index of new direction + */ + _roationDirection (d, r) { + if (d + r > this.cartDirections.length - 1) { + return 0 + } + if (d + r < 0) { + return this.cartDirections.length - 1 + } + return d + r + } + + /** + * Advances the state of the entire layout + */ + advance () { + this.frame++ + this.carts.sort(dynamicSortMultiple('y', 'x')).forEach((c) => { + try { + this.moveCart(c) + } catch (err) { + console.error(`Problem moving cart in frame ${this.frame}`) + console.error(err) + } + }) + } + + /** + * Displays the current state of the track with the carts placed + */ + display () { + let output = '' + const layout = JSON.parse(JSON.stringify(this.layout)) // Deep copy + // Include the carts + this.carts.forEach((cart) => { + // If another cart is at the spot, draw a collision instead + if (this.cartDirections.indexOf(layout[cart.y][cart.x]) >= 0) { + layout[cart.y][cart.x] = 'X' + } else { + layout[cart.y][cart.x] = cart.direction + } + }) + layout.forEach((y) => { + output += y.join('') + output += '\n' + }) + + return output + } + + /** + * Locates carts on the imported layout and pops them out to the carts list + */ + extractCarts () { + this.layout = this.layout.map((y, idy) => { + return y.map((x, idx) => { + // Pop the cart into the cart list with its location + if (this.cartDirections.indexOf(x) >= 0) { + this.carts.push({ + x: idx, + y: idy, + direction: x, + lastIntersections: [1, 0] // Assume the first ntersection the cart will turn left + }) + // Replace the cart on the track with a track segment + // (Assuming cart initial states aren't on instersections) + x = (this.cartDirections.indexOf(x) % 2 === 0) ? '|' : '-' + } + return x + }) + }) + } + + /** + * Gets the track segment at the specified coordinates + * @param {*} x Number + * @param {*} y Number + */ + getSegment (x, y) { + return this.layout[y][x] + } + + /** + * Moves the specified cart in the direction of travel, observing all rules + * @param {Objc} cart from the list of carts + */ + moveCart (cart) { + // Determine the direction of travel + const d = this.cartDirections.indexOf(cart.direction) // Absolute direction + const a = (d % 2 === 0) ? 'y' : 'x' // axis of travel + const l = (d % 3 === 0) ? -1 : 1 // (+/-) distance of travel on the axis + // move the cart + cart[a] = cart[a] + l + const s = this.getSegment(cart.x, cart.y) // Segment of track the cart is now on + + // Make sure cart hasn't run off the rails + if (this.trackTypes.indexOf(this.layout[cart.y][cart.x]) < 0) { + throw new Error(`cart ran off the track at ${cart.x}, ${cart.y}`) + } + // Check for collision + if (this._isCollision(cart.x, cart.y)) { + this.collision = { x: cart.x, y: cart.y } + throw new Error(`collision at ${cart.x}, ${cart.y}`) // Stop everything + } + // rotate the cart when entering a turn + if (this._isTurn(s)) { + cart.direction = this._rotate(s, a, d) + return + } + // rotate (or not) the cart when entering an intersection + if (this._isIntersection(s)) { + cart.direction = this._intersect(cart) + } + } + + /** + * Stores the provided track + * @param {String} track + */ + setLayout (track) { + this.layout = track.split('\n') + this.layout = this.layout.map((e) => { return e.split('') }) + this.extractCarts() + } +} + +module.exports = { + Track +} diff --git a/2018/day-13/tracks.test.js b/2018/day-13/tracks.test.js new file mode 100644 index 0000000..7c9ff5c --- /dev/null +++ b/2018/day-13/tracks.test.js @@ -0,0 +1,204 @@ +/* eslint-env mocha */ +const expect = require('chai').expect +const { + Track +} = require('./tracks') + +const data = `/->-\\ +| | /----\\ +| /-+--+-\\ | +| | | | v | +\\-+-/ \\-+--/ + \\------/ ` + +describe('--- Day 13: Mine Cart Madness ---', () => { + describe('Part 1:', () => { + describe('new Track(layout)', () => { + it('Initializes a new track with layout', () => { + const track = new Track(data) + expect(track.layout[4][12]).to.equal('/') + }) + it('splits out and stores the carts as addressable data', () => { + const expected = [{ + x: 2, + y: 0, + direction: '>', + lastIntersections: [1, 0] + }, { + x: 9, + y: 3, + direction: 'v', + lastIntersections: [1, 0] + }] + const track = new Track(data) + const actual = track.carts + expect(actual).to.deep.equal(expected) + }) + }) + describe('advance()', () => { + it('moves forward all carts on the layout', () => { + const test = `/---\\ +| | /----\\ +| /-+>-+-\\ | +| | | | | ^ +\\-+-/ \\-+--/ + \\------/ ` + const expected = `/---\\ +| | /----\\ +| /-+->+-\\ ^ +| | | | | | +\\-+-/ \\-+--/ + \\------/ `.trim() + const track = new Track(test) + track.advance() + const actual = track.display().trim() + expect(actual).to.equal(expected) + expect(track.frame).to.equal(1) + }) + }) + describe('moveCart(cart)', () => { + it('moves an individual cart right', () => { + const test = `->--` + const track = new Track(test) + const expected = `-->-` + track.moveCart(track.carts[0]) + const actual = track.display().trim() + expect(actual).to.equal(expected) + }) + it('moves an individual cart left', () => { + const test = `--<-` + const track = new Track(test) + const expected = `-<--` + track.moveCart(track.carts[0]) + const actual = track.display().trim() + expect(actual).to.equal(expected) + }) + it('moves an individual cart down', () => { + const test = `|\nv\n|\n|` + const track = new Track(test) + const expected = `|\n|\nv\n|` + track.moveCart(track.carts[0]) + const actual = track.display().trim() + expect(actual).to.equal(expected) + }) + it('moves an individual cart up', () => { + const test = `|\n|\n^\n|` + const track = new Track(test) + const expected = `|\n^\n|\n|` + track.moveCart(track.carts[0]) + const actual = track.display().trim() + expect(actual).to.equal(expected) + }) + it('rotates a cart when it enters turns', () => { + const tests = [ + `->-\\-`, + `->-/-`, + `-/-<-`, + `-\\-<-`, + `|\nv\n|\n\\\n|`, + `|\nv\n|\n/\n|`, + `|\n/\n|\n^\n|`, + `|\n\\\n|\n^\n|` + ] + const expected = [ + `---v-`, + `---^-`, + `-v---`, + `-^---`, + `|\n|\n|\n>\n|`, + `|\n|\n|\n<\n|`, + `|\n>\n|\n|\n|`, + `|\n<\n|\n|\n|` + ] + tests.forEach((test, idx) => { + const track = new Track(test) + track.moveCart(track.carts[0]) + track.moveCart(track.carts[0]) + const actual = track.display().trim() + expect(actual).to.equal(expected[idx]) + }) + }) + it('rotates a cart when it enters an intersection', () => { + const test = `->-+-` + const track = new Track(test) + const expected = `---^-` + track.moveCart(track.carts[0]) + track.moveCart(track.carts[0]) + const actual = track.display().trim() + expect(actual).to.equal(expected) + }) + it('tracks the direction through multiple intersections following the sequential rotation rules: left, straight, right', () => { + const expected = ` + ^ + | + +-+ + | + + + | +---+`.trim() + const test = ` + | + | + +-+ + | + + + | +->-+` + const track = new Track(test) + + for (let i = 0; i < 10; i++) { + track.moveCart(track.carts[0]) + } + const actual = track.display().trim() + expect(actual).to.equal(expected) + }) + it('only moves the specified cart', () => { + const test = `->--<-` + const expected = `---><-` + const track = new Track(test) + track.moveCart(track.carts[0]) + track.moveCart(track.carts[0]) + const actual = track.display().trim() + expect(actual).to.equal(expected) + }) + it('throws an error if the cart runs off the rails', () => { + const test = `->- -` + const track = new Track(test) + try { + track.moveCart(track.carts[0]) + } catch (err) { + expect(err).to.be.an('error') + } + }) + it('registers a collision', () => { + const test = `->-<-` + const expected = `---X-` + const track = new Track(test) + try { + track.moveCart(track.carts[0]) + track.moveCart(track.carts[0]) + } catch (err) { + const actual = track.display().trim() + expect(actual).to.equal(expected) + expect(track.collision).to.deep.equal({ x: 3, y: 0 }) + } + }) + }) + describe('display()', () => { + it('renders the current track state', () => { + const expected = data.trim() + const track = new Track(data) + const actual = track.display().trim() + expect(actual).to.equal(expected) + }) + }) + describe('getSegment(x,y)', () => { + it('queries the type of segment at location x,y', () => { + const expected = '-' + const track = new Track(data) + const actual = track.getSegment(5, 2) + expect(actual).to.equal(expected) + }) + }) + }) +}) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy