Advent of Code Solutions Thread: Day Two

Only shitty solutions allowed

38
Jump in the discussion.

No email address required.

C solution

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>

typedef enum Move Move;
typedef enum End  End;

enum Move {
	ROCK     = 0,
	PAPER    = 1,
	SCISSORS = 2,
};

static Move better[] = { PAPER,    SCISSORS, ROCK  };
static Move worse[]  = { SCISSORS, ROCK,     PAPER };

enum End {
	LOSE = 0,
	DRAW = 1,
	WIN  = 2,
};

static int compute_score(FILE* input, int (*match_points)(char x, char y)) {
	int c     = fgetc(input);
	int score = 0;

	while (c != EOF) {
		assert(c == 'A'  c == 'B'  c == 'C');
		char x = c;
		c      = fgetc(input);
		
		assert(c == ' ');
		c      = fgetc(input);
		
		assert(c == 'X'  c == 'Y'  c == 'Z');
		char y = c;
		c      = fgetc(input);
		
		score += match_points(x, y);

		assert(c == '\n');
		c      = fgetc(input);
	}

	return score;
}

static int part1(char x, char y) {
	Move opp   = x - 'A';
	Move you   = y - 'X';
	int  score = 0;
	
	if (you == better[opp]) // win
		score += 6;
	else if (you == opp) // draw
		score += 3;
	
	return score + you + 1;
}

static int part2(char x, char y) {
	Move opp   = x - 'A';
	End  end   = y - 'X';
	Move you;
	int  score = 0;

	if (end == LOSE)
		you = worse[opp];
	else if (end == DRAW)
		you = opp;
	else if (end == WIN)
		you = better[opp];

	return end * 3 + you + 1;
}

int main() {
	FILE* input = fopen("inputs/2.txt", "r");
	if (input == NULL) {
		perror("fopen");
		return EXIT_FAILURE;
	}

	printf("part1: %d\n", compute_score(input, part1));
	rewind(input);
	printf("part2: %d\n", compute_score(input, part2));
}
Jump in the discussion.

No email address required.

haskell solution:

import Data.Char (ord)

data Move = Rock | Paper | Scissors deriving (Enum, Eq)
data End  = Lose | Draw  | Win  deriving Enum

better :: Move -> Move
better Rock     = Paper
better Paper    = Scissors
better Scissors = Rock

worse :: Move -> Move
worse Rock     = Scissors
worse Paper    = Rock
worse Scissors = Paper

fromBase :: Enum a => Char -> Char -> a
fromBase b c = toEnum $ ord c - ord b

score :: Move -> Move -> Integer
score opp you =
  fromIntegral $ (fromEnum you) + 1 +
  if      opp == you        then 3
  else if better opp == you then 6
  else                           0

part1 :: String -> Integer
part1 [oppRaw, ' ', youRaw] = score (fromBase 'A' oppRaw) (fromBase 'X' youRaw)

part2 :: String -> Integer
part2 [oppRaw, ' ', endRaw] =
  let
    opp = fromBase 'A' oppRaw
    end = fromBase 'X' endRaw
    you = case end of { Lose -> worse opp; Draw -> opp; Win -> better opp; }
  in
    score opp you

main :: IO ()
main
  =   (,)
  <$> solve part1
  <*> solve part2
  >>= print
  where solve f = sum <$> (map f) <$> lines <$> readFile "inputs/2.txt"
Jump in the discussion.

No email address required.

This is one of the worst posts I have EVER seen. Delete it.

Jump in the discussion.

No email address required.

This is one of the worst posts I have EVER seen. Delete it.

Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.