Solve problems for day 1
This commit is contained in:
parent
4e60624181
commit
af43fb352d
66
q1.cpp
Normal file
66
q1.cpp
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
#include <algorithm>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
int q1(std::ifstream &in) {
|
||||||
|
std::vector<int> l1;
|
||||||
|
std::vector<int> l2;
|
||||||
|
|
||||||
|
// for (std::string line; std::getline(in, line);) {
|
||||||
|
// std::string left = line.substr(0, line.find(" "));
|
||||||
|
// std::string right = line.substr(line.rfind(" "));
|
||||||
|
// l1.push_back(std::stoi(left));
|
||||||
|
// l2.push_back(std::stoi(right));
|
||||||
|
// }
|
||||||
|
|
||||||
|
for (std::string line; std::getline(in, line);) {
|
||||||
|
int left, right;
|
||||||
|
std::stringstream s(line);
|
||||||
|
s >> left >> right;
|
||||||
|
l1.push_back(left);
|
||||||
|
l2.push_back(right);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::sort(l1.begin(), l1.end());
|
||||||
|
std::sort(l2.begin(), l2.end());
|
||||||
|
|
||||||
|
int sum = 0;
|
||||||
|
for (int i = 0; i < l1.size(); i++) {
|
||||||
|
sum += abs(l1[i]-l2[i]);
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
int q2(std::ifstream &in) {
|
||||||
|
std::vector<int> l1;
|
||||||
|
std::unordered_map<int, int> l2;
|
||||||
|
|
||||||
|
for (std::string line; std::getline(in, line);) {
|
||||||
|
std::string left = line.substr(0, line.find(" "));
|
||||||
|
std::string right = line.substr(line.rfind(" "));
|
||||||
|
l1.push_back(std::stoi(left));
|
||||||
|
l2[std::stoi(right)]++;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::sort(l1.begin(), l1.end());
|
||||||
|
|
||||||
|
int sum = 0;
|
||||||
|
for (int i = 0; i < l1.size(); i++) {
|
||||||
|
sum += l1[i] * l2[l1[i]];
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::ifstream input("i1.txt");
|
||||||
|
std::cout << "Part 1: " << q1(input) << std::endl;
|
||||||
|
|
||||||
|
// Could re-read the file, or just seek to the beginning.
|
||||||
|
// input = std::ifstream("i1.txt");
|
||||||
|
input.clear();
|
||||||
|
input.seekg(0);
|
||||||
|
std::cout << "Part 2: " << q2(input) << std::endl;
|
||||||
|
}
|
32
q1.py
Normal file
32
q1.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
from typing import Counter
|
||||||
|
|
||||||
|
|
||||||
|
def q1(text: str):
|
||||||
|
l1 = []
|
||||||
|
l2 = []
|
||||||
|
for line in text.splitlines():
|
||||||
|
left, right = list(map(int, line.replace(" ", " ").split()))
|
||||||
|
l1.append(left)
|
||||||
|
l2.append(right)
|
||||||
|
|
||||||
|
l1.sort()
|
||||||
|
l2.sort()
|
||||||
|
|
||||||
|
return sum(abs(left-right) for left, right in zip(l1, l2))
|
||||||
|
|
||||||
|
def q2(text: str):
|
||||||
|
l1 = []
|
||||||
|
l2 = Counter()
|
||||||
|
for line in text.splitlines():
|
||||||
|
left, right = list(map(int, line.replace(" ", " ").split()))
|
||||||
|
l1.append(left)
|
||||||
|
l2[right] += 1
|
||||||
|
|
||||||
|
return sum(left * l2[left] for left in l1)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("i1.txt") as f:
|
||||||
|
print(q1(f.read()))
|
||||||
|
|
||||||
|
with open("i1.txt") as f:
|
||||||
|
print(q2(f.read()))
|
Loading…
Reference in New Issue
Block a user