For all of the programmers that are celebrating the festive season, the Advent of Code is here! If you wish to talk about some of the challenges or participate, please visit the website at ! The input (and hence answers) are different for everyone, but you can still share the code you used to solve them. There is no API for the site so you'll need to read your input from a file.
Best of luck to all! Let me know how you go! Please keep discussions to English only! If you post code on elitepvpers please make sure its in spoiler tags - click Hastebin / Github links at your own discretion knowing that it may contain spoilers!
Wow ... Day 1 Part 2 really took a lot of cycles (142). I even considered my input to be broken.
I updated my post - feel free to post your solutions as long as they're in a spoiler tag (if it's a code block on the forum), or feel free to just post a link to your Github repository / Hastebin if you feel like sharing your answers! I've updated the main post to include a link to my solutions.
Glad you found the puzzles challenging, things like this keep the mind active!
Glad you found the puzzles challenging, things like this keep the mind active!
I chose std::list, which was not a good choice regarding speed. So finding the frequency initially took roughly 1:30min. Thats why I considered my set to be wrong ;D
I'm now using unordered_set, which is way faster in this case.
I chose std::list, which was not a good choice regarding speed. So finding the frequency initially took roughly 1:30min. Thats why I considered my set to be wrong ;D
I'm now using unordered_set, which is way faster in this case.
#include <cstdio>
#include <unordered_set>
int main()
{
typedef int num_t;
num_t freq = 0;
int inp = 0;
std::unordered_set<num_t> freqs;
FILE* f = fopen("advent_1_2.txt", "r");
while (1)
{
while(fscanf(f, "%d", &inp) == 1)
{
freq += inp;
if (freqs.find(freq) != freqs.end())
{
printf("freq is %d\n", freq);
return 0;
}
freqs.emplace(freq);
}
rewind(f);
}
return 1;
}
You can make the code even simpler by using the return values of std::unordered_list#emplace - it returns a pair of <Iterator, bool>. The bool value will be false if a duplicate entry exists as a set only allows one of each element in its collection
Then I'd go so far as to say you're being rather silly, especially in this scenario! Firstly, it's important to make the distinction that the Advent of Code is by no means a programming challenge, but a problem solving exercise.
Second, LINQ in this use case is not providing any functionality that any other language with high order functions like Map, Reduce, Fold and Zip would provide. Would you consider it cheating if I wrote my answers in Haskell? The code would be much more concise and be using the exact same functions that LINQ provides.
Edit: After writing that response I decided to try a functional language for the first time ever. Could probably be improved a lot and isn't totally idiomatic F# but it's my first program ever using the language. I don't fully understand the functional idioms just yet :P
Then I'd go so far as to say you're being rather silly, especially in this scenario! Firstly, it's important to make the distinction that the Advent of Code is by no means a programming challenge, but a problem solving exercise.
Second, LINQ in this use case is not providing any functionality that any other language with high order functions like Map, Reduce, Fold and Zip would provide. Would you consider it cheating if I wrote my answers in Haskell? The code would be much more concise and be using the exact same functions that LINQ provides.
Edit: After writing that response I decided to try a functional language for the first time ever. Could probably be improved a lot and isn't totally idiomatic F# but it's my first program ever using the language. I don't fully understand the functional idioms just yet :P
#include <iostream>
#include <iterator>
#include <regex>
#include <set>
#include <string>
#include <vector>
int main(int argc, char *argv[])
{
std::vector<std::vector<std::pair<int, int>>> v;
std::vector<int> ids;
std::set<int> non_overlapped;
std::set<std::pair<int, int>> intersections;
std::string line;
std::regex re("^#\\s*(\\d+)\\s*@\\s*(\\d+),(\\d+):\\s*(\\d+)x(\\d+)$");
std::smatch match;
while (std::getline(std::cin, line)) {
std::regex_match(line, match, re);
int id = std::stoi(match[1]);
ids.push_back(id);
non_overlapped.insert(id);
std::vector<std::pair<int, int>> s;
for (int i = std::stoi(match[2]); i < std::stoi(match[2]) + std::stoi(match[4]); ++i) {
for (int j = std::stoi(match[3]); j < std::stoi(match[3]) + std::stoi(match[5]); ++j) {
s.emplace_back(i, j);
}
}
int j = 0;
for (const auto &s2 : v) {
std::vector<std::pair<int, int>> intersection;
std::set_intersection(s.begin(), s.end(),
s2.begin(), s2.end(),
std::back_inserter(intersection));
intersections.insert(intersection.begin(), intersection.end());
if (!intersection.empty()) {
non_overlapped.erase(id);
non_overlapped.erase(ids[j]);
}
++j;
}
v.push_back(s);
}
std::cout << intersections.size() << std::endl;
if (!non_overlapped.empty()) {
std::cout << *non_overlapped.begin() << std::endl;
}
return 0;
}
Edit: I would like to solve one of those with Haskell as well, but it's been ages since I was exposed to Haskell in uni and I forgot the whole syntax. The last time I tried to get into Haskell again it gave me autism.
Reading is definitely not one of my strengths. I didn't read day 3 to the end and "accidentally" build an algorithm to find the largest single rectangle of all overlapping claims. lol.
Advent Advent ein Lichtlein brennt 11/30/2011 - Off Topic - 76 Replies Erst eins dan zwei dann drei dann vier...
Weihnachten steht kurtz vor der tür und ich dachte mir ich verlose hier ein
paar kleinigkeiten...
zu gewinnen gibt es 23x "10 e*gold"
und 1x eine fette "25€ Paysafecard"
also eigentlich eine art Adventskalender.
Final Fantasy VII - Advent Children 09/19/2005 - Main - 7 Replies hiho
wollt ma fragen ob den hier schon jemand geschaut hat. wenn ja, wie ihr den film so findet?? :p
ich hab ihn seit kurzen fertig geladen, werd den vlt später gucken^^
mfg
hoffe das thema gabs net schon. hab suche benutzt, aber nix gefunden :o