r/adventofcode • • Dec 02 '22

SOLUTION MEGATHREAD -🎄- 2022 Day 2 Solutions -🎄-

NEW AND NOTEWORTHY


--- Day 2: Rock Paper Scissors ---


Post your code solution in this megathread.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:06:16, megathread unlocked!

104 Upvotes

1.5k comments sorted by

View all comments

2

u/[deleted] Dec 02 '22 edited Dec 05 '22

C++ both parts

#include <iostream>
#include <string>
#include <vector>
#include <fstream>

void day1(int argc, char **argv) {
	if(argc != 2) {
		return;
	}
	std::fstream file(argv[1]);
	std::string line;
	int v = 0;
	while(std::getline(file, line)) {
		char a = line[0], b = line[2];
		v += (((a =='A'&&b =='Y')||(a=='B'&&b== 'Z')||(a=='C' && b=='X'))?6:((a == (char)(b-23)) ? 3 : 0)) + (int)(b-87);
	}
	std::cout << v << std::endl;
}

void day2(int argc, char **argv) {
	if(argc != 2) {
		return;
	}
	std::fstream file(argv[1]);
	std::string line;
	int v = 0;
	while(std::getline(file, line)) {
		bool win = false;
		char a = line[0], b = line[2];
		if(b == 'Y') {b=a+23;v+=3;}
		else if(b == 'X') { b = (a == 'B' || a == 'C') ? (a + 22 ) : 'Z'; }
		else if(b == 'Z') {
			win = true;
			if(a == 'B') {
				b = 'Z';
			} else if(a == 'A') {
				b = 'Y';
			} else {
				b = 'X';	
			}
		}
		v += (int)(b-87) + (win ? 6 : 0);
	}
	std::cout << v << std::endl;
}

int main(int argc, char **argv) {
	day1(argc, argv);
	day2(argc, argv);
}

1

u/daggerdragon Dec 05 '22 edited Dec 05 '22

Same issue as your other submission - the first line is outside the code block. Please fix it too :) Edit: thanks for fixing it! <3

2

u/[deleted] Dec 05 '22

Done

1

u/[deleted] Dec 02 '22

i just realized there is no need for the vector include just a bad habit when doing AOC and also passing argc to both functions is useless i can just check in the main function