1
0
Fork 0
advent-of-code/day5/day5.erl

28 lines
12 KiB
Erlang
Raw Normal View History

2017-12-05 12:45:34 -05:00
-module(day5).
-export([start/0]).
calculate_jumps(Instructions)
-> calculate_jumps(array:from_list(Instructions), 0, 0, length(Instructions)).
calculate_jumps(_Stack, Ip, Steps, StackSize)
when Ip >= StackSize; Ip < 0 -> Steps;
calculate_jumps(Stack, Ip, Steps, StackSize)
-> Instr = array:get(Ip, Stack),
calculate_jumps(array:set(Ip, Instr + 1, Stack), Ip + Instr, Steps + 1, StackSize).
calculate_neg_jumps(Instructions)
-> calculate_neg_jumps(array:from_list(Instructions), 0, 0, length(Instructions)).
calculate_neg_jumps(_Stack, Ip, Steps, StackSize)
when Ip >= StackSize; Ip < 0 -> Steps;
calculate_neg_jumps(Stack, Ip, Steps, StackSize)
-> Instr = array:get(Ip, Stack),
NextInstr = if Instr >= 3 -> Instr - 1 ; true -> Instr + 1 end,
calculate_neg_jumps(array:set(Ip, NextInstr, Stack), Ip + Instr, Steps + 1, StackSize).
start()
-> io:fwrite("~B~n", [calculate_jumps([0, 3, 0, 1, -3])]),
io:fwrite("~B~n", [calculate_jumps([0, 0, 1, 2, 0, 1, 0, 2, -2, -6, 0, -7, 2, 2, 1, -2, 1, 1, -11, -14, -16, -14, -12, -5, -2, -21, -15, -9, 0, -13, -4, -19, -31, 1, -15, -1, -23, -18, 0, -18, -11, -21, -6, 2, -38, 2, -38, -40, -15, -2, -23, -42, -23, -36, -37, -34, -26, -51, -14, -8, -56, -11, -14, -40, -39, -26, -35, -48, -52, -18, -16, -11, -38, -4, -70, -20, -10, -37, -75, -33, -39, -37, -74, -17, -83, -13, -75, -56, -44, -40, -20, -18, -74, -32, -48, -11, -80, -88, -33, -67, -96, -67, -49, -55, -101, -8, -77, -33, -93, -22, -21, -99, -33, -12, -111, -105, -77, -76, -90, -63, -76, -55, -65, -106, -56, -93, -121, -31, -86, -102, -116, -120, -17, -95, -110, -128, -122, -135, -133, -124, -50, -32, -86, -11, -127, -42, -9, -41, -28, -93, -13, -16, -8, -138, -111, -103, -25, -22, -133, -52, -27, -6, -67, -72, -115, -133, -146, -46, -128, -99, -115, -16, -113, -105, 1, -76, -2, 1, -66, -164, -87, -8, -92, -180, -137, -39, -127, -124, -66, -96, -185, -13, -65, -178, -16, -32, -24, -84, -49, -7, -198, -101, -119, -187, -71, -125, -130, -72, -130, -43, -136, -171, -32, -210, -69, -195, -207, -197, -190, -117, -196, -143, -128, -155, -160, -53, -205, -15, -86, -227, -128, -115, -58, -96, -52, -100, -41, -84, -61, -50, -91, -158, -30, -230, -125, -177, -181, -47, -43, -241, -52, -102, -161, -16, -130, -21, -22, -249, -78, -101, -51, -229, -148, -132, -213, -264, -175, -128, -102, -26, -86, -84, -100, -132, -228, -118, -46, -77, -230, -98, -193, -191, -142, -193, -183, -58, -59, -254, -256, -108, -12, -46, -11, -143, -22, -121, -68, -282, -94, -22, -172, -246, -174, -76, -3, -117, -237, -258, -29, -105, -32, -177, -125, -70, -176, -113, -152, -227, -298, -199, -179, -162, -241, -230, -8, -202, -296, -194, -82, -188, -136, -24, -171, -185, -272, -124, -306, -230, -249, -64, -139, -143, -293, -218, -167, -208, -254, -171, -119, -82, -284, -262, -252, -92, -230, -193, -173, -269, -173, -13, 0, -148, -151, -241, -120, -20, -22, -218, -6, -314, -347, -58, -180, -242, -333, -247, -67, -137, -102, -32, -102, -88, -42, -284, -109, -281, -43, -63, -205, -268, -273, -235, -119, -185, -62, -56, -260, -47, -353, -123, -312, 0, -76, -352, -365, -284, -105, -62, -165, -292, -5, -46, -31, -116, -147, -34, -149, -79, -139, -132, -84, -58, -130, -125, -188, -227, -2, -220, -88, -96, -38, -308, -214, -357, -19, -410, -292, -150, -151, 1, -252, -172, -414, -15, -207, -184, -286, -161, -55, -158, -330, -53, -310, -295, -248, -126, -425, -10, -289, -148, -146, -436, -279, -48, -321, -196, -162, -55, -125, -62, -381, -337, -85, -428, -470, -180, -392, -450, -376, -73, -206, -115, -289, -318, -22, -200, -176, -410, -380, -470, -418, -163, -424, -288, -374, -358, -386, -340, -403, -81, -7, -378, 1, -320, -323, -288, -439, -437, -90, -159, -379, -69, -236, -168, -232, -232, -42, -257, -437, -40, -390, -241, -54, -247, -76, -325, -351, -259, -328, -283, -5, -169, -114, -252, -131, -223, -446, -482, -7, -285, -486, -260, -421, -498, -32, -182, -317, -201, -370, -518, -129, -359, -67, -209, -541, -323, -132, -197, -70, -171, -258, -372, -228, -4, -360, -144, -307, -226, -533, -435, -318, -284, -284, -150, -539, -46, -81, -144, -188, -264, -136, -138, -106, -65, -262, -250, -176, -261, -112, -227, -304, -159, -425, -117, -441, -111, -468, -2, -84, -197, 1, -575, -305, -401, -114, -143, -180, -16, -450, -24, -357, -104, -434, -435, -345, -247, -484, -282, -263, -389, -463, -178, -144, -256, -94, -522, -302, -524, -309, -275, -350, -596, -462, -317, -543, -8, -374, -404, -302, -610, -111, -358, 2, -353, -397, -165, -324, -574, -618, -168, -636, -389, -609, -54, -121, -256, -604, -588, -454, -100, -212, -65, -300, -592, -43, -230, -652, -638, -67, -499, -614, -419, -577, -96, -257, -519, -458, -14, -26, -370, -187, -319, -528, -436, -87, -673, -535, -473, -670, -594, -589, -629, -180, -13, -668, -369, -679, -587, -624, -123, -363, -241, -343, -259, -123, -319, -608, -470, -336, -291, -676, -535, -226, -11, -224, -43, -30, -605, -670, -544, -248, -609, -144, -328, -177, -360, -628, -51, -724, -407, -625, -433, -192, -455, -603, -581, -33, -696, -160, -464
io:fwrite("~B~n", [calculate_neg_jumps([0, 3, 0, 1, -3])]),
io:fwrite("~B~n", [calculate_neg_jumps([0, 0, 1, 2, 0, 1, 0, 2, -2, -6, 0, -7, 2, 2, 1, -2, 1, 1, -11, -14, -16, -14, -12, -5, -2, -21, -15, -9, 0, -13, -4, -19, -31, 1, -15, -1, -23, -18, 0, -18, -11, -21, -6, 2, -38, 2, -38, -40, -15, -2, -23, -42, -23, -36, -37, -34, -26, -51, -14, -8, -56, -11, -14, -40, -39, -26, -35, -48, -52, -18, -16, -11, -38, -4, -70, -20, -10, -37, -75, -33, -39, -37, -74, -17, -83, -13, -75, -56, -44, -40, -20, -18, -74, -32, -48, -11, -80, -88, -33, -67, -96, -67, -49, -55, -101, -8, -77, -33, -93, -22, -21, -99, -33, -12, -111, -105, -77, -76, -90, -63, -76, -55, -65, -106, -56, -93, -121, -31, -86, -102, -116, -120, -17, -95, -110, -128, -122, -135, -133, -124, -50, -32, -86, -11, -127, -42, -9, -41, -28, -93, -13, -16, -8, -138, -111, -103, -25, -22, -133, -52, -27, -6, -67, -72, -115, -133, -146, -46, -128, -99, -115, -16, -113, -105, 1, -76, -2, 1, -66, -164, -87, -8, -92, -180, -137, -39, -127, -124, -66, -96, -185, -13, -65, -178, -16, -32, -24, -84, -49, -7, -198, -101, -119, -187, -71, -125, -130, -72, -130, -43, -136, -171, -32, -210, -69, -195, -207, -197, -190, -117, -196, -143, -128, -155, -160, -53, -205, -15, -86, -227, -128, -115, -58, -96, -52, -100, -41, -84, -61, -50, -91, -158, -30, -230, -125, -177, -181, -47, -43, -241, -52, -102, -161, -16, -130, -21, -22, -249, -78, -101, -51, -229, -148, -132, -213, -264, -175, -128, -102, -26, -86, -84, -100, -132, -228, -118, -46, -77, -230, -98, -193, -191, -142, -193, -183, -58, -59, -254, -256, -108, -12, -46, -11, -143, -22, -121, -68, -282, -94, -22, -172, -246, -174, -76, -3, -117, -237, -258, -29, -105, -32, -177, -125, -70, -176, -113, -152, -227, -298, -199, -179, -162, -241, -230, -8, -202, -296, -194, -82, -188, -136, -24, -171, -185, -272, -124, -306, -230, -249, -64, -139, -143, -293, -218, -167, -208, -254, -171, -119, -82, -284, -262, -252, -92, -230, -193, -173, -269, -173, -13, 0, -148, -151, -241, -120, -20, -22, -218, -6, -314, -347, -58, -180, -242, -333, -247, -67, -137, -102, -32, -102, -88, -42, -284, -109, -281, -43, -63, -205, -268, -273, -235, -119, -185, -62, -56, -260, -47, -353, -123, -312, 0, -76, -352, -365, -284, -105, -62, -165, -292, -5, -46, -31, -116, -147, -34, -149, -79, -139, -132, -84, -58, -130, -125, -188, -227, -2, -220, -88, -96, -38, -308, -214, -357, -19, -410, -292, -150, -151, 1, -252, -172, -414, -15, -207, -184, -286, -161, -55, -158, -330, -53, -310, -295, -248, -126, -425, -10, -289, -148, -146, -436, -279, -48, -321, -196, -162, -55, -125, -62, -381, -337, -85, -428, -470, -180, -392, -450, -376, -73, -206, -115, -289, -318, -22, -200, -176, -410, -380, -470, -418, -163, -424, -288, -374, -358, -386, -340, -403, -81, -7, -378, 1, -320, -323, -288, -439, -437, -90, -159, -379, -69, -236, -168, -232, -232, -42, -257, -437, -40, -390, -241, -54, -247, -76, -325, -351, -259, -328, -283, -5, -169, -114, -252, -131, -223, -446, -482, -7, -285, -486, -260, -421, -498, -32, -182, -317, -201, -370, -518, -129, -359, -67, -209, -541, -323, -132, -197, -70, -171, -258, -372, -228, -4, -360, -144, -307, -226, -533, -435, -318, -284, -284, -150, -539, -46, -81, -144, -188, -264, -136, -138, -106, -65, -262, -250, -176, -261, -112, -227, -304, -159, -425, -117, -441, -111, -468, -2, -84, -197, 1, -575, -305, -401, -114, -143, -180, -16, -450, -24, -357, -104, -434, -435, -345, -247, -484, -282, -263, -389, -463, -178, -144, -256, -94, -522, -302, -524, -309, -275, -350, -596, -462, -317, -543, -8, -374, -404, -302, -610, -111, -358, 2, -353, -397, -165, -324, -574, -618, -168, -636, -389, -609, -54, -121, -256, -604, -588, -454, -100, -212, -65, -300, -592, -43, -230, -652, -638, -67, -499, -614, -419, -577, -96, -257, -519, -458, -14, -26, -370, -187, -319, -528, -436, -87, -673, -535, -473, -670, -594, -589, -629, -180, -13, -668, -369, -679, -587, -624, -123, -363, -241, -343, -259, -123, -319, -608, -470, -336, -291, -676, -535, -226, -11, -224, -43, -30, -605, -670, -544, -248, -609, -144, -328, -177, -360, -628, -51, -724, -407, -625, -433, -192, -455, -603, -581, -33, -696, -160,