Last active
December 1, 2019 23:03
-
-
Save theglauber/34046cac288b6314803dba6cabc36848 to your computer and use it in GitHub Desktop.
20191201 Advent of Code 2019 day 1
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
#!/bin/perl | |
use strict; | |
use integer; | |
sub fuel { | |
my $mass = $_[0] + 0; | |
my $fuel = $mass / 3 - 2; | |
return $fuel; | |
} | |
my $total = 0; | |
my $count = 0; | |
while (my $mass = <DATA>) { | |
chomp($mass); | |
my $fuel = fuel($mass); | |
$total += $fuel; | |
$count++; | |
print $count, ") ", $mass, " ", $fuel, " ", $total, "\n"; | |
} | |
print "Total = $total\n"; | |
__DATA__ | |
73617 | |
104372 | |
131825 | |
85022 | |
105514 | |
78478 | |
87420 | |
118553 | |
97680 | |
89479 | |
146989 | |
79746 | |
108085 | |
117895 | |
143811 | |
102509 | |
102382 | |
92975 | |
72978 | |
94208 | |
130521 | |
83042 | |
138605 | |
107566 | |
63374 | |
71176 | |
129487 | |
118408 | |
115425 | |
63967 | |
98282 | |
121829 | |
92834 | |
61084 | |
70122 | |
87221 | |
132773 | |
141347 | |
133225 | |
81199 | |
94994 | |
60881 | |
110074 | |
63499 | |
143107 | |
76618 | |
86818 | |
135394 | |
106908 | |
96085 | |
99801 | |
112903 | |
51751 | |
56002 | |
70924 | |
62180 | |
133025 | |
68025 | |
122660 | |
64898 | |
77339 | |
62109 | |
133891 | |
134460 | |
84224 | |
54836 | |
59748 | |
125540 | |
67796 | |
71845 | |
92899 | |
130103 | |
74612 | |
136820 | |
96212 | |
132002 | |
97405 | |
82629 | |
63717 | |
62805 | |
112693 | |
147810 | |
139827 | |
116220 | |
69711 | |
50236 | |
137833 | |
103743 | |
147456 | |
112098 | |
84867 | |
75615 | |
132738 | |
81072 | |
89444 | |
58443 | |
94465 | |
112494 | |
82127 | |
132533 |
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
#!/bin/perl -w | |
use strict; | |
use integer; | |
sub m2f { | |
my ($m, $accumulator) = @_; | |
my $f = $m / 3 - 2; | |
if ($f > 0) { | |
my $f2 = $accumulator + $f; | |
return m2f($f, $f2); | |
} else { | |
return $accumulator; | |
} | |
} | |
my $total = 0; | |
my $count = 0; | |
while (my $mass = <DATA>) { | |
chomp($mass); | |
my $fuel = m2f($mass, 0); | |
$total += $fuel; | |
$count++; | |
print $count, ") ", $mass, " ", $fuel, " ", $total, "\n"; | |
} | |
print "Total = $total\n"; | |
__DATA__ | |
73617 | |
104372 | |
131825 | |
85022 | |
105514 | |
78478 | |
87420 | |
118553 | |
97680 | |
89479 | |
146989 | |
79746 | |
108085 | |
117895 | |
143811 | |
102509 | |
102382 | |
92975 | |
72978 | |
94208 | |
130521 | |
83042 | |
138605 | |
107566 | |
63374 | |
71176 | |
129487 | |
118408 | |
115425 | |
63967 | |
98282 | |
121829 | |
92834 | |
61084 | |
70122 | |
87221 | |
132773 | |
141347 | |
133225 | |
81199 | |
94994 | |
60881 | |
110074 | |
63499 | |
143107 | |
76618 | |
86818 | |
135394 | |
106908 | |
96085 | |
99801 | |
112903 | |
51751 | |
56002 | |
70924 | |
62180 | |
133025 | |
68025 | |
122660 | |
64898 | |
77339 | |
62109 | |
133891 | |
134460 | |
84224 | |
54836 | |
59748 | |
125540 | |
67796 | |
71845 | |
92899 | |
130103 | |
74612 | |
136820 | |
96212 | |
132002 | |
97405 | |
82629 | |
63717 | |
62805 | |
112693 | |
147810 | |
139827 | |
116220 | |
69711 | |
50236 | |
137833 | |
103743 | |
147456 | |
112098 | |
84867 | |
75615 | |
132738 | |
81072 | |
89444 | |
58443 | |
94465 | |
112494 | |
82127 | |
132533 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment