Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- // return result of game (loss, draw, win) = (0,1,2)
- int game_result( int me, int elf ) {
- // res is basically (me - elf) mod 3, BUT...
- // +1 to shift to loss as 0 instead of draw
- // +3 to keep +'ve for mod
- return ((me - elf + 4) % 3);
- }
- // return score for a move
- int move_score( int move ) {
- return (move + 1);
- }
- // return move to get result against elf
- int get_move( int result, int elf ) {
- // res = (me - elf + 1) mod 3 (see game_result())
- // me = (res + elf - 1) mod 3
- // = (res + elf + 2) mod 3 (keep +'ve for mod)
- return ((elf + result + 2) % 3);
- }
- int main() {
- char line[10];
- int part1 = 0;
- int part2 = 0;
- while (fgets( line, sizeof(line), stdin ) != NULL) {
- int elf = line[0] - 'A'; // convert elf move to [0,2]
- int resp = line[2] - 'X'; // convert response to [0,2]
- // Part 1: take response as move to make
- part1 += 3 * game_result( resp, elf ) + move_score( resp );
- // Part 2: response is result of game desired
- part2 += 3 * resp + move_score( get_move( resp, elf ) );
- }
- printf( "Part 1: %d\n", part1 );
- printf( "Part 2: %d\n", part2 );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement