Advent of Code solutions

fixing 2021

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