Advent of Code solutions

fixing 2021

+18 -30
+1 -3
2021/1/p1.cpp
··· 1 #include <iostream> 2 - #include <fstream> 3 4 using namespace std; 5 6 int main() { 7 - ifstream input("input"); 8 int previous = 2147483647, current = 0; 9 int total = 0; 10 11 - while (input >> current) { 12 if (current > previous) { 13 total++; 14 }
··· 1 #include <iostream> 2 3 using namespace std; 4 5 int main() { 6 int previous = 2147483647, current = 0; 7 int total = 0; 8 9 + while (cin >> current) { 10 if (current > previous) { 11 total++; 12 }
+1 -2
2021/1/p2.cpp
··· 4 using namespace std; 5 6 int main() { 7 - ifstream input("input"); 8 int total = 0; 9 int a = 0, b = 0, c = 0, d = 0; 10 int rownumber = 0; 11 12 - while (input >> d) { 13 if (rownumber >= 3) { 14 if (a + b + c < b + c + d) { 15 total++;
··· 4 using namespace std; 5 6 int main() { 7 int total = 0; 8 int a = 0, b = 0, c = 0, d = 0; 9 int rownumber = 0; 10 11 + while (cin >> d) { 12 if (rownumber >= 3) { 13 if (a + b + c < b + c + d) { 14 total++;
+1 -5
2021/2/p1.pl
··· 1 #!/usr/bin/perl 2 3 - open(FH, '<', 'input'); 4 - 5 $horizontal = 0; 6 $depth = 0; 7 8 - while(<FH>) { 9 $command = $_; 10 11 if ($command =~ /forward (\d+)/) { ··· 18 } 19 20 print $depth * $horizontal . "\n"; 21 - 22 - close(FH);
··· 1 #!/usr/bin/perl 2 3 $horizontal = 0; 4 $depth = 0; 5 6 + while(<>) { 7 $command = $_; 8 9 if ($command =~ /forward (\d+)/) { ··· 16 } 17 18 print $depth * $horizontal . "\n";
+1 -5
2021/2/p2.pl
··· 1 #!/usr/bin/perl 2 3 - open(FH, '<', 'input'); 4 - 5 $horizontal = 0; 6 $depth = 0; 7 $aim = 0; 8 9 - while(<FH>) { 10 $command = $_; 11 12 if ($command =~ /forward (\d+)/) { ··· 20 } 21 22 print $depth * $horizontal . "\n"; 23 - 24 - close(FH);
··· 1 #!/usr/bin/perl 2 3 $horizontal = 0; 4 $depth = 0; 5 $aim = 0; 6 7 + while(<>) { 8 $command = $_; 9 10 if ($command =~ /forward (\d+)/) { ··· 18 } 19 20 print $depth * $horizontal . "\n";
+6 -4
2021/3/p1.rb
··· 1 - counts = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 2 3 - File.readlines("input").each do |line| 4 line.chomp.chars.each_with_index do |char, i| 5 counts[i] += 1 if char == "1" 6 end 7 end ··· 12 return binary 13 end 14 15 - gamma = counts.map { |n| n > 500 }.reduce(0, &method(:from_binary)) 16 - epsilon = counts.map { |n| n < 500 }.reduce(0, &method(:from_binary)) 17 18 puts gamma * epsilon
··· 1 + counts = [] 2 3 + lines = ARGF.readlines() 4 + lines.each do |line| 5 line.chomp.chars.each_with_index do |char, i| 6 + counts[i] ||= 0 7 counts[i] += 1 if char == "1" 8 end 9 end ··· 14 return binary 15 end 16 17 + gamma = counts.map { |n| n >= lines.length / 2 }.reduce(0, &method(:from_binary)) 18 + epsilon = counts.map { |n| n < lines.length / 2 }.reduce(0, &method(:from_binary)) 19 20 puts gamma * epsilon
+4 -3
2021/3/p2.rb
··· 1 - lines = File.readlines("input").map { |l| l.chomp } 2 3 def count lines, i 4 return lines ··· 8 9 oxygen = lines 10 12.times do |i| 11 - c = count(oxygen, i) >= oxygen.length / 2 ? "1" : "0" 12 oxygen = oxygen.filter { |line| line[i] == c } 13 break if oxygen.length == 1 14 end 15 16 carbon = lines 17 12.times do |i| 18 - c = count(carbon, i) >= carbon.length / 2 ? "0" : "1" 19 carbon = carbon.filter { |line| line[i] == c } 20 break if carbon.length == 1 21 end 22 23 puts(oxygen.first.to_i(2) * carbon.first.to_i(2))
··· 1 + lines = ARGF.readlines().map { |l| l.chomp } 2 3 def count lines, i 4 return lines ··· 8 9 oxygen = lines 10 12.times do |i| 11 + c = count(oxygen, i) >= oxygen.length / 2.0 ? "1" : "0" 12 oxygen = oxygen.filter { |line| line[i] == c } 13 break if oxygen.length == 1 14 end 15 16 carbon = lines 17 12.times do |i| 18 + c = count(carbon, i) >= carbon.length / 2.0 ? "0" : "1" 19 carbon = carbon.filter { |line| line[i] == c } 20 break if carbon.length == 1 21 end 22 + 23 24 puts(oxygen.first.to_i(2) * carbon.first.to_i(2))
+1 -3
2021/4/p1.pl
··· 96 parse_boards(Rest, Boards). 97 98 input(Calls, Boards) :- 99 - open("input", read, Fd), 100 - read_stream_to_codes(Fd, Contents), 101 - close(Fd), 102 split_string(Contents, "\n", " ", [CallStr | BoardStr]), 103 split_string(CallStr, ",", " ", CallStrs), 104 convert_all(CallStrs, Calls),
··· 96 parse_boards(Rest, Boards). 97 98 input(Calls, Boards) :- 99 + read_stream_to_codes(user_input, Contents), 100 split_string(Contents, "\n", " ", [CallStr | BoardStr]), 101 split_string(CallStr, ",", " ", CallStrs), 102 convert_all(CallStrs, Calls),
+1 -3
2021/4/p2.pl
··· 96 parse_boards(Rest, Boards). 97 98 input(Calls, Boards) :- 99 - open("input", read, Fd), 100 - read_stream_to_codes(Fd, Contents), 101 - close(Fd), 102 split_string(Contents, "\n", " ", [CallStr | BoardStr]), 103 split_string(CallStr, ",", " ", CallStrs), 104 convert_all(CallStrs, Calls),
··· 96 parse_boards(Rest, Boards). 97 98 input(Calls, Boards) :- 99 + read_stream_to_codes(user_input, Contents), 100 split_string(Contents, "\n", " ", [CallStr | BoardStr]), 101 split_string(CallStr, ",", " ", CallStrs), 102 convert_all(CallStrs, Calls),
+2 -2
Justfile
··· 11 12 default_year := if current_year != "" { current_year } else { env_year } 13 14 - p1 input="input": (part "p1") 15 - p2 input="input": (part "p2") 16 compile part: (_compile current_year current_day part) 17 run part input="input": (_run current_year current_day part input) 18
··· 11 12 default_year := if current_year != "" { current_year } else { env_year } 13 14 + p1 input="input": (part "p1" current_day current_year input) 15 + p2 input="input": (part "p2" current_day current_year input) 16 compile part: (_compile current_year current_day part) 17 run part input="input": (_run current_year current_day part input) 18