Skip to content

Instantly share code, notes, and snippets.

@stenehall
Created December 1, 2019 20:31
Show Gist options
  • Save stenehall/cc5aaa6804e8844c5c0e35ab03eda226 to your computer and use it in GitHub Desktop.
Save stenehall/cc5aaa6804e8844c5c0e35ab03eda226 to your computer and use it in GitHub Desktop.
JS Bin Code advent day 1 - 2 // source https://jsbin.com/tomuvej
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Code advent day 1 - 2">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.22.1/ramda.min.js"></script>
<script id="jsbin-javascript">
/*
- A module of mass 14 requires 2 fuel. This fuel requires no further fuel (2 divided by 3 and rounded down is 0, which would call for a negative fuel), so the total fuel required is still just 2.
- At first, a module of mass 1969 requires 654 fuel. Then, this fuel requires 216 more fuel (654 / 3 - 2). 216 then requires 70 more fuel, which requires 21 fuel, which requires 5 fuel, which requires no further fuel. So, the total fuel required for a module of mass 1969 is 654 + 216 + 70 + 21 + 5 = 966.
- The fuel required by a module of mass 100756 and its fuel is: 33583 + 11192 + 3728 + 1240 + 411 + 135 + 43 + 12 + 2 = 50346.
*/
const log = str => console.log(str)
const max = val => Math.max(0, val - 2)
const divide = val => val / 3
const calc = R.compose(max, Math.floor, divide)
const one = mass => {
if (mass <= 0) {
return 0
}
const val = calc(mass)
return val + one(val)
}
const logCalc = R.compose(log, calc)
const logOne = R.compose(log,one)
logOne(1969) // -> 966
logOne(100756) // -> 50346
const masss = `90859
127415
52948
92106
106899
72189
60084
79642
138828
103609
149073
127749
86976
104261
139341
81414
102622
131030
120878
96809
130331
119212
52317
108990
136871
67279
76541
113254
77739
75027
53863
97732
65646
87851
63712
92660
131821
127837
52363
70349
66110
132249
50319
125948
98360
137675
61957
143540
137402
135774
51376
144833
118646
128136
141140
82856
63345
143617
79733
73449
116126
73591
63899
110409
79602
77485
64050
131760
90509
112728
55181
55329
98597
126579
108227
80707
92962
90396
123775
125248
128814
64593
63108
76486
107135
111064
142569
68579
149006
52258
143477
131889
142506
146732
58663
92013
62410
71035
51208
66372`
const oneMasss = masss.split('\n').map(one)
console.log(oneMasss.reduce((a,c) => a+c))
</script>
<script id="jsbin-source-javascript" type="text/javascript">/*
- A module of mass 14 requires 2 fuel. This fuel requires no further fuel (2 divided by 3 and rounded down is 0, which would call for a negative fuel), so the total fuel required is still just 2.
- At first, a module of mass 1969 requires 654 fuel. Then, this fuel requires 216 more fuel (654 / 3 - 2). 216 then requires 70 more fuel, which requires 21 fuel, which requires 5 fuel, which requires no further fuel. So, the total fuel required for a module of mass 1969 is 654 + 216 + 70 + 21 + 5 = 966.
- The fuel required by a module of mass 100756 and its fuel is: 33583 + 11192 + 3728 + 1240 + 411 + 135 + 43 + 12 + 2 = 50346.
*/
const log = str => console.log(str)
const max = val => Math.max(0, val - 2)
const divide = val => val / 3
const calc = R.compose(max, Math.floor, divide)
const one = mass => {
if (mass <= 0) {
return 0
}
const val = calc(mass)
return val + one(val)
}
const logCalc = R.compose(log, calc)
const logOne = R.compose(log,one)
logOne(1969) // -> 966
logOne(100756) // -> 50346
const masss = `90859
127415
52948
92106
106899
72189
60084
79642
138828
103609
149073
127749
86976
104261
139341
81414
102622
131030
120878
96809
130331
119212
52317
108990
136871
67279
76541
113254
77739
75027
53863
97732
65646
87851
63712
92660
131821
127837
52363
70349
66110
132249
50319
125948
98360
137675
61957
143540
137402
135774
51376
144833
118646
128136
141140
82856
63345
143617
79733
73449
116126
73591
63899
110409
79602
77485
64050
131760
90509
112728
55181
55329
98597
126579
108227
80707
92962
90396
123775
125248
128814
64593
63108
76486
107135
111064
142569
68579
149006
52258
143477
131889
142506
146732
58663
92013
62410
71035
51208
66372`
const oneMasss = masss.split('\n').map(one)
console.log(oneMasss.reduce((a,c) => a+c))
</script></body>
</html>
/*
- A module of mass 14 requires 2 fuel. This fuel requires no further fuel (2 divided by 3 and rounded down is 0, which would call for a negative fuel), so the total fuel required is still just 2.
- At first, a module of mass 1969 requires 654 fuel. Then, this fuel requires 216 more fuel (654 / 3 - 2). 216 then requires 70 more fuel, which requires 21 fuel, which requires 5 fuel, which requires no further fuel. So, the total fuel required for a module of mass 1969 is 654 + 216 + 70 + 21 + 5 = 966.
- The fuel required by a module of mass 100756 and its fuel is: 33583 + 11192 + 3728 + 1240 + 411 + 135 + 43 + 12 + 2 = 50346.
*/
const log = str => console.log(str)
const max = val => Math.max(0, val - 2)
const divide = val => val / 3
const calc = R.compose(max, Math.floor, divide)
const one = mass => {
if (mass <= 0) {
return 0
}
const val = calc(mass)
return val + one(val)
}
const logCalc = R.compose(log, calc)
const logOne = R.compose(log,one)
logOne(1969) // -> 966
logOne(100756) // -> 50346
const masss = `90859
127415
52948
92106
106899
72189
60084
79642
138828
103609
149073
127749
86976
104261
139341
81414
102622
131030
120878
96809
130331
119212
52317
108990
136871
67279
76541
113254
77739
75027
53863
97732
65646
87851
63712
92660
131821
127837
52363
70349
66110
132249
50319
125948
98360
137675
61957
143540
137402
135774
51376
144833
118646
128136
141140
82856
63345
143617
79733
73449
116126
73591
63899
110409
79602
77485
64050
131760
90509
112728
55181
55329
98597
126579
108227
80707
92962
90396
123775
125248
128814
64593
63108
76486
107135
111064
142569
68579
149006
52258
143477
131889
142506
146732
58663
92013
62410
71035
51208
66372`
const oneMasss = masss.split('\n').map(one)
console.log(oneMasss.reduce((a,c) => a+c))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment