Created
December 5, 2019 13:39
-
-
Save klebervirgilio/4b5305a3c2735d825e1687383761ef1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
input = [ | |
51_360, 95_527, 72_603, 128_601, 68_444, 138_867, 67_294, | |
134_343, 62_785, 53_088, 134_635, 137_884, 97_654, 103_704, | |
138_879, 87_561, 83_922, 68_414, 84_876, 105_143, 76_599, | |
98_924, 57_080, 63_590, 50_126, 111_872, 55_754, 64_410, | |
78_488, 56_557, 105_446, 127_182, 59_451, 87_249, 61_652, | |
131_698, 148_820, 95_742, 68_223, 121_744, 65_678, 99_745, | |
64_089, 75_610, 106_085, 100_364, 116_959, 122_862, 56_580, | |
109_631, 82_895, 79_666, 133_474, 50_579, 83_473, 140_028, | |
125_999, 68_225, 131_345, 90_797, 84_914, 81_915, 65_369, | |
71_230, 50_379, 106_385, 118_503, 119_640, 138_540, 70_678, | |
95_881, 100_282, 123_060, 147_368, 93_030, 82_553, 131_271, | |
147_675, 111_126, 115_183, 82_956, 145_698, 99_261, 52_768, | |
99_207, 123_551, 64_738, 117_275, 98_136, 111_592, 78_576, | |
118_613, 130_351, 68_567, 72_356, 85_608, 129_414, 66_521, | |
76_924, 130_449 | |
] | |
def mass(input) | |
input.fdiv(3).floor - 2 | |
end | |
def fuel(input, acc = 0) | |
r = mass(input) | |
return acc if r <= 0 | |
acc = fuel(r, acc + r) if r.positive? | |
acc | |
end | |
puts input.sum(&method(:mass)) | |
puts input.sum(&method(:fuel)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment