function hw1()
clear
clc
clf
%Generate plot corresponding to problem 1 - requires pvdhighs.dat and
%pvdlows.dat to be in same directory as hw1.m
problem1();
%Find probabilities of Monty-Hall problem using monte carlo
problem2()
fprintf('Done!\n');
end

function problem1()
load pvdhighs.dat
load pvdlows.dat

meanhigh = ones(length(pvdlows))*mean(pvdhighs(:,2));
meanlow = ones(length(pvdlows))*mean(pvdlows(:,2));
time = pvdlows(:,1);
plot(time,pvdlows(:,2),time,pvdhighs(:,2),time,meanlow,time,meanhigh)
end

function problem2()
nA = 0;
nB = 0;
ntrials = 100000; %number of trials to perform
for trials = 1:ntrials
     %run a single trial and save the success of either strategy as A and B
    [A,B]=runMH();
    nA = nA + A;
    nB = nB + B;
end
%Compute average success rate of either strategy
fprintf('Problem 2:\n');
fprintf('Strategy 1: %e\nStrategy 2: %e\n',nA/ntrials,nB/ntrials);
end

function [A,B] = runMH()

%Doors is a 3-element array representing 3 doors, 0 means goat and 1 means
%car
doors = zeros(3,1);
%Stick a car behind one of the doors
doors(randi(3)) = 1;

%guess a door
guess = randi(3);

%strategy A - check you guessed right on door 
A = doors(guess)==1;

%strategy B - find which unpicked door has a goat. from that pair of doors,
%guess the door that doesn't have a goat and return a 1 if that was 
%the door with a car

if guess == 1
   doors2 = [doors(2) doors(3)];
   if doors2(1)==0
       guess2 = 2;
   else
       guess2 = 1;
   end
   B = (doors2(guess2))==1;
end
if guess == 2
   doors2 = [doors(1) doors(3)];
   if doors2(1)==0
       guess2 = 2;
   else
       guess2 = 1;
   end   
   B = (doors2(guess2))==1;
end
if guess == 3
   doors2 = [doors(1) doors(2)];
   if doors2(1)==0
       guess2 = 2;
   else
       guess2 = 1;
   end
   B = (doors2(guess2))==1;
end


end
