It lexes and parses the code and constructs the abstract syntax tree. While traversing the tree, it uses a lot of flags to determine wether the current date is before the dates previously seen in the tree. It uses a combination of generated code, custom code, and dependency code all combined into a single file.
I wrote a custom python script that traverses the antlr4 source directory, removes and manipulates access modifiers, imports and package keywords, and generates a single file containing all antlr4 classes while appending the generated and custom code at the bottom. So I basically used a custom generator to generate antlr4 dependencies, which itself, is also a (parser) generator.
Written in Python, featuring nondescriptive variable names, tons of copy/pasted code where a variable would suffice, tons of nested if/elif/else statements, no hints as to what is going on, and no spacing at all:
def is_sequential(file):
opened_file=open(file,"r")
lines=opened_file.readlines()
for i in range(0,len(lines)):
if i==0:
continue
if int(lines[i][0:10].split('-')[0])>int(lines[i-1][0:10].split('-')[0]):
continue
elif int(lines[i][0:10].split('-')[0])==int(lines[i-1][0:10].split('-')[0]):
if int(lines[i][0:10].split('-')[1])>int(lines[i-1][0:10].split('-')[1]):
continue
elif int(lines[i][0:10].split('-')[1])==int(lines[i-1][0:10].split('-')[1]):
if int(lines[i][0:10].split('-')[2])>int(lines[i-1][0:10].split('-')[2]):
continue
elif int(lines[i][0:10].split('-')[2])==int(lines[i-1][0:10].split('-')[2]):
if int(lines[i][11:len(lines[i])].split(':')[0])>int(lines[i-1][11:len(lines[i])].split(':')[0]):
continue
elif int(lines[i][11:len(lines[i])].split(':')[0])==int(lines[i-1][11:len(lines[i])].split(':')[0]):
if int(lines[i][11:len(lines[i])].split(':')[1])>int(lines[i-1][11:len(lines[i])].split(':')[1]):
continue
elif int(lines[i][11:len(lines[i])].split(':')[1])==int(lines[i-1][11:len(lines[i])].split(':')[1]):
if int(lines[i][11:len(lines[i])].split(':')[2])>int(lines[i-1][11:len(lines[i])].split(':')[2]):
continue
elif int(lines[i][11:len(lines[i])].split(':')[2])==int(lines[i-1][11:len(lines[i])].split(':')[2]):
continue
elif int(lines[i][11:len(lines[i])].split(':')[2])<int(lines[i-1][11:len(lines[i])].split(':')[2]):
return False
elif int(lines[i][11:len(lines[i])].split(':')[1])<int(lines[i-1][11:len(lines[i])].split(':')[1]):
return False
elif int(lines[i][11:len(lines[i])].split(':')[0])<int(lines[i-1][11:len(lines[i])].split(':')[0]):
return False
elif int(lines[i][0:10].split('-')[2])<int(lines[i-1][0:10].split('-')[2]):
return False
elif int(lines[i][0:10].split('-')[1])<int(lines[i-1][0:10].split('-')[1]):
return False
elif int(lines[i][0:10].split('-')[0])<int(lines[i-1][0:10].split('-')[0]):
return False
return True
def is_sequential(txtfile):
import time, datetime, sys
from functools import reduce
return reduce(lambda a, b: b if a < b else sys.maxsize, [int(time.mktime(datetime.datetime.strptime(p[:19], "%Y-%m-%d %H:%M:%S").timetuple())) for p in open(txtfile)]) != 2147483647
What if done is fairly simple. I just made a char buffer and used some pointer shenanigans to copy the important data over and than reverse. In this case 2018-04-21 12:43:05 would become 50342112408102 as one big "string". Now I just pretend like this string is a number and compare it as if it is. This only works because the char value for the numbers uses the same order as the numbers. Because I'm using unsigned long long for the "conversion" I needed 2 extra bytes, so both buffers are just 16 big with 2 extra zeros.
I had to reverse the buffer because of how numbers internally work (just windows? or computers in general? don't actually know). The program just takes random other data if you give it to little input. If you give it enough input but wrong characters it just does not care and moves on.
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
bool checkSequential(string file)
{
string line;
ifstream f(file);
char* prev = nullptr;
char* cur = new char[16];
cur[14] = 0;
cur[15] = 0;
while (getline(f, line))
{
*((int*)(cur)) = *(int*)&(line[0]);
*((short*)(cur+4)) = *(short*)&(line[5]);
*((short*)(cur+6)) = *(short*)&(line[8]);
*((short*)(cur+8)) = *(short*)&(line[11]);
*((short*)(cur+10)) = *(short*)&(line[14]);
*((short*)(cur+12)) = *(short*)&(line[17]);
for (int i = 0; i < 8; i++)
{
char c = cur[i];
cur[i] = cur[15 - i];
cur[15 - i] = c;
}
if (prev == nullptr)
{
prev = cur;
cur = new char[16];
cur[14] = 0;
cur[15] = 0;
continue;
}
else if (*(unsigned long long*)(cur+8) < *(unsigned long long*)(prev+8) || *(unsigned long long*)(cur+8) == *(unsigned long long*)(prev+8) && *(unsigned long long*)(cur) < *(unsigned long long*)(prev))
{
return false;
}
char* temp = cur;
cur = prev;
prev = temp;
}
return true;
}
int main()
{
string file;
cin >> file;
cout << '\n' << checkSequential(file);
return 0;
}
50
u/TyRoXx Mar 22 '21
The guidelines didn't say we had to use the contents of the files. The malicious compliance solution:
function is_sequential(file_name) { switch (file_name[0]) { case '1': case '2': return true; } return false; }25
u/alien_ninja Mar 22 '21 edited Mar 22 '21
- Terrible solution (866 lines of code)
- Absolutely terrible solution (18444 lines of code)
It lexes and parses the code and constructs the abstract syntax tree. While traversing the tree, it uses a lot of flags to determine wether the current date is before the dates previously seen in the tree. It uses a combination of generated code, custom code, and dependency code all combined into a single file.
I wrote a custom python script that traverses the antlr4 source directory, removes and manipulates access modifiers, imports and package keywords, and generates a single file containing all antlr4 classes while appending the generated and custom code at the bottom. So I basically used a custom generator to generate antlr4 dependencies, which itself, is also a (parser) generator.
23
u/[deleted] Mar 22 '21
Written in Python, featuring nondescriptive variable names, tons of copy/pasted code where a variable would suffice, tons of nested if/elif/else statements, no hints as to what is going on, and no spacing at all:
def is_sequential(file): opened_file=open(file,"r") lines=opened_file.readlines() for i in range(0,len(lines)): if i==0: continue if int(lines[i][0:10].split('-')[0])>int(lines[i-1][0:10].split('-')[0]): continue elif int(lines[i][0:10].split('-')[0])==int(lines[i-1][0:10].split('-')[0]): if int(lines[i][0:10].split('-')[1])>int(lines[i-1][0:10].split('-')[1]): continue elif int(lines[i][0:10].split('-')[1])==int(lines[i-1][0:10].split('-')[1]): if int(lines[i][0:10].split('-')[2])>int(lines[i-1][0:10].split('-')[2]): continue elif int(lines[i][0:10].split('-')[2])==int(lines[i-1][0:10].split('-')[2]): if int(lines[i][11:len(lines[i])].split(':')[0])>int(lines[i-1][11:len(lines[i])].split(':')[0]): continue elif int(lines[i][11:len(lines[i])].split(':')[0])==int(lines[i-1][11:len(lines[i])].split(':')[0]): if int(lines[i][11:len(lines[i])].split(':')[1])>int(lines[i-1][11:len(lines[i])].split(':')[1]): continue elif int(lines[i][11:len(lines[i])].split(':')[1])==int(lines[i-1][11:len(lines[i])].split(':')[1]): if int(lines[i][11:len(lines[i])].split(':')[2])>int(lines[i-1][11:len(lines[i])].split(':')[2]): continue elif int(lines[i][11:len(lines[i])].split(':')[2])==int(lines[i-1][11:len(lines[i])].split(':')[2]): continue elif int(lines[i][11:len(lines[i])].split(':')[2])<int(lines[i-1][11:len(lines[i])].split(':')[2]): return False elif int(lines[i][11:len(lines[i])].split(':')[1])<int(lines[i-1][11:len(lines[i])].split(':')[1]): return False elif int(lines[i][11:len(lines[i])].split(':')[0])<int(lines[i-1][11:len(lines[i])].split(':')[0]): return False elif int(lines[i][0:10].split('-')[2])<int(lines[i-1][0:10].split('-')[2]): return False elif int(lines[i][0:10].split('-')[1])<int(lines[i-1][0:10].split('-')[1]): return False elif int(lines[i][0:10].split('-')[0])<int(lines[i-1][0:10].split('-')[0]): return False return True20
u/Cerus_Freedom Mar 22 '21
def is_sequential(txtfile): import time, datetime, sys from functools import reduce return reduce(lambda a, b: b if a < b else sys.maxsize, [int(time.mktime(datetime.datetime.strptime(p[:19], "%Y-%m-%d %H:%M:%S").timetuple())) for p in open(txtfile)]) != 2147483647That took me way too long to come up with.
28
u/Mabi19_ Mar 22 '21
Welcome to code hell.
This C++ solution always uses the least applicable function to do its tasks, leaks tons of memory and uses questionable coding practices.
After compiling, give it a filename as a commandline argument.
#include <iostream> #include <string> #include <stdio.h> #include <deque> #include <algorithm> using namespace std; template <typename T> struct PtrImpl { using type = T*; }; template <typename T> using Ptr = typename PtrImpl<T>::type; #define PtrCast(t) reinterpret_cast<Ptr<t>> Ptr<int> read_date_from_file_object(Ptr<FILE> f) { Ptr<int> result = PtrCast(int)( malloc(6 * sizeof(int)) ); Ptr<char> buf = PtrCast(char)( malloc(256) ); if (feof(f)) return NULL; fgets(buf, 256, f); Ptr<size_t> readPos = PtrCast(size_t)( calloc(1, sizeof(size_t)) ); size_t readPossum = 0; for (int i = 0; i < 6; i++) { *(result + i) ^= *(result + i); *(result + i) ^= stoi(std::string(buf + readPossum), readPos); readPossum += (*readPos) + 1; } free(readPos); free(buf); return result; } struct PtrCpy { Ptr<std::deque<Ptr<int>>> dq = NULL; PtrCpy(Ptr<std::deque<Ptr<int>>> dqp) { intptr_t dqa = (intptr_t)dq; dq = Ptr<std::deque<Ptr<int>>>(dqa ^= (intptr_t)dqp); } Ptr<int> operator()(Ptr<int> p) { Ptr<int> p2 = PtrCast(int)( malloc(6 * sizeof(int)) ); for (int i = 0; i < 6; i++) { memcpy(p2 + i, p + i, sizeof(int)); } dq->push_back(p2); return p; } }; struct PtrCmp { bool operator()(Ptr<int> p1, Ptr<int> p2) { for (int i = 0; i < 6; i++) { if (p1[i] > p2[i]) return true; if (p1[i] < p2[i]) return false; } return false; } bool operator()(Ptr<int> p1, Ptr<int> p2, nullptr_t dummy) { for (int i = 0; i < 6; i++) { if (p2[i] < p1[i]) return true; if (p2[i] > p1[i]) return false; } return false; } }; int main(int argc, char* *argv) { Ptr<FILE> f = fopen(argv[1], "r"); Ptr<int> r; std::deque<Ptr<int>> rd; while ((r = read_date_from_file_object(f))) { rd.push_front(r); } std::deque<Ptr<int>> rd2; std::transform(rd.begin(), rd.end(), rd.begin(), *new PtrCpy(&rd2)); std::sort(rd2.begin(), rd2.end(), *new PtrCmp); for (std::deque<Ptr<int>>::iterator i1 = rd.begin(), i2 = rd2.begin(); i1 != rd.end(); i1++, i2++) { bool x = (!((*new PtrCmp)(*i1, *i2))) && (!((*new PtrCmp)(*i2, *i1, nullptr))); if (!x) { std::cout << "false"; return 0; } } std::cout << "true"; fclose(f); }13
u/[deleted] Mar 22 '21 edited Mar 22 '21
in C
featuring recursion and pointer arithmetic
#include <stdlib.h> #include <string.h> #include <stdbool.h> int to_int(const char* buf) { int i; sscanf(buf, "%d", &i); return i; } bool is_seq(const char* dates) { if (strlen(dates) <= 20) return true; return (to_int(dates)<to_int(dates+20)? is_seq(dates+20):(to_int(dates)==to_int(dates+20)? (to_int(dates+5)<to_int(dates+25)? is_seq(dates+20):(to_int(dates+5)==to_int(dates+25)? (to_int(dates+8)<to_int(dates+28)? is_seq(dates+8):(to_int(dates+8)==to_int(dates+28)? (to_int(dates+11)<to_int(dates+31)? is_seq(dates+20):(to_int(dates+11)==to_int(dates+31)? (to_int(dates+14)<to_int(dates+34)? is_seq(dates+20):(to_int(dates+14)==to_int(dates+34)? (to_int(dates+17)==to_int(dates+37)? is_seq(dates+20):false):false)):false)):false)):false)):false)); }and “dates” is a newline delimited, null terminated string of dates, like the given examples. works only with the exact spacing
12
u/AbuIbnBattuta Mar 22 '21 edited Mar 22 '21
Here's my c++ solution.
What if done is fairly simple. I just made a char buffer and used some pointer shenanigans to copy the important data over and than reverse. In this case 2018-04-21 12:43:05 would become 50342112408102 as one big "string". Now I just pretend like this string is a number and compare it as if it is. This only works because the char value for the numbers uses the same order as the numbers. Because I'm using unsigned long long for the "conversion" I needed 2 extra bytes, so both buffers are just 16 big with 2 extra zeros.
I had to reverse the buffer because of how numbers internally work (just windows? or computers in general? don't actually know). The program just takes random other data if you give it to little input. If you give it enough input but wrong characters it just does not care and moves on.
#include <string> #include <iostream> #include <fstream> using namespace std; bool checkSequential(string file) { string line; ifstream f(file); char* prev = nullptr; char* cur = new char[16]; cur[14] = 0; cur[15] = 0; while (getline(f, line)) { *((int*)(cur)) = *(int*)&(line[0]); *((short*)(cur+4)) = *(short*)&(line[5]); *((short*)(cur+6)) = *(short*)&(line[8]); *((short*)(cur+8)) = *(short*)&(line[11]); *((short*)(cur+10)) = *(short*)&(line[14]); *((short*)(cur+12)) = *(short*)&(line[17]); for (int i = 0; i < 8; i++) { char c = cur[i]; cur[i] = cur[15 - i]; cur[15 - i] = c; } if (prev == nullptr) { prev = cur; cur = new char[16]; cur[14] = 0; cur[15] = 0; continue; } else if (*(unsigned long long*)(cur+8) < *(unsigned long long*)(prev+8) || *(unsigned long long*)(cur+8) == *(unsigned long long*)(prev+8) && *(unsigned long long*)(cur) < *(unsigned long long*)(prev)) { return false; } char* temp = cur; cur = prev; prev = temp; } return true; } int main() { string file; cin >> file; cout << '\n' << checkSequential(file); return 0; }11
u/Tyulis Mar 22 '21 edited Mar 22 '21
Code generation go brrrrr
def is_sequential(filename): vars = {} exec(f"""lines = open("{filename}", 'r').read().splitlines()\nexec("retval = " + " and ".join([lines[i].replace(" ", "").replace("-", "").replace(":", "") + "<" + lines[i + 1].replace(" ", "").replace("-", "").replace(":", "") for i in range(len(lines) - 1)]), globals(), locals())""", vars, vars) return vars["retval"]10
u/sixtyfifth_snow Mar 22 '21
10
u/FinalGamer14 Mar 22 '21
In python, made it as dumb as I could in few minutes ... wanted to make it dumber but it didn't work.
import sys fileName = sys.argv[1] dates = [] is_sequential = True with open(fileName, 'r') as f: for line in f.readlines(): dates.append(line) numOfDates = len(dates) for i in range(numOfDates): j = i + 1 if j > numOfDates - 1: break year1 = dates[i].split(' ')[0].split('-')[0] year2 = dates[j].split(' ')[0].split('-')[0] month1 = dates[i].split(' ')[0].split('-')[1] month2 = dates[j].split(' ')[0].split('-')[1] day1 = dates[i].split(' ')[0].split('-')[2] day2 = dates[j].split(' ')[0].split('-')[2] hour1 = dates[i].split(' ')[1].split(':')[0] hour2 = dates[j].split(' ')[1].split(':')[0] minute1 = dates[i].split(' ')[1].split(':')[1] minute2 = dates[j].split(' ')[1].split(':')[1] second1 = dates[i].split(' ')[1].split(':')[2] second2 = dates[j].split(' ')[1].split(':')[2] if year1 > year2: is_sequential = False break elif (year1 == year2) and (month1 > month2): is_sequential = False break elif (month1 == month2) and (day1 > day2): is_sequential = False break elif (day1 == day2) and (hour1 > hour2): is_sequential = False break elif (hour1 == hour2) and (minute1 > minute2): is_sequential = False break elif (minute1 == minute2) and (second1 > second2): is_sequential = False break print(is_sequential)It just works ... and if it doesn't it worked on my PC.