diff --git a/odin/day02/day02.odin b/odin/day02/day02.odin index 38e619c..f3ce131 100644 --- a/odin/day02/day02.odin +++ b/odin/day02/day02.odin @@ -37,13 +37,31 @@ determine_score :: proc(friendly: Move, enemy: Move) -> int { return result + int(friendly) } -determine_score_alt :: proc(friendly: Move, outcome: Move) -> int { - // X = lose round - // Y = draw round - // Z = win round - // 3 defeats 2, 2 defeats 1, 1 defeats 3 - // If w := enemy input and v := friendly input, then w = (v - 1) % 2 is a defeat - return 0 +/// Scoring for Part 2 of the puzzle +determine_score_alt :: proc(outcome: Move, enemy: Move) -> int { + // Redefine the outcomes so that + // draw = 0, win = 1, lose = 2 + outcome_ : int; + switch outcome { + // X = lose + case .ROCK: + outcome_ = 2 + // Y = draw + case .PAPER: + outcome_ = 0 + // Z = win + case .SCISSORS: + outcome_ = 1 + } + // Convert enemy from range [1, 3] to range [0, 2] + enemy_ := int(enemy) - 1 + // Assert that (friendly - enemy = outcome) % 3 (see scratch.py) + // Thus, (friendly = outcome + enemy) % 3 + friendly_ := (outcome_ + enemy_) % 3 + // Add 1 back in because we subtracted it before (going from [0, 2] to [1, 3]) + friendly_ = friendly_ + 1 + // Return score as before + return determine_score(cast(Move)friendly_, enemy) } main :: proc () { @@ -56,7 +74,7 @@ main :: proc () { fh, errno := os.open(os.args[1]) if errno != os.ERROR_NONE { fmt.printf("Cannot open %s: %s\n", os.args[1], os.get_last_error_string()) - os.exit(int(errno)) + os.exit(-1) } defer os.close(fh) @@ -64,6 +82,7 @@ main :: proc () { defer delete(buffer); num_read, error := os.read(fh, buffer) + score : int = 0; total_score : int = 0; friendly : Move enemy : Move @@ -73,8 +92,9 @@ main :: proc () { chr = buffer[0] switch chr { case '\n': - fmt.printf("Score for this line: %d\n", determine_score(friendly, enemy)) - total_score += determine_score(friendly, enemy) + score = determine_score_alt(friendly, enemy) + fmt.printf("Score for this line: %d\n", score) + total_score += score case 'A': enemy = .ROCK case 'B': @@ -92,7 +112,7 @@ main :: proc () { } } if chr != '\n' { - total_score += determine_score(friendly, enemy) + total_score += determine_score_alt(friendly, enemy) } /// Handle potential errors if error != os.ERROR_NONE { diff --git a/odin/day02/scratch.py b/odin/day02/scratch.py new file mode 100644 index 0000000..4a441bb --- /dev/null +++ b/odin/day02/scratch.py @@ -0,0 +1,51 @@ +rock = 0 +paper = 1 +scissors = 2 + +print("Friendly first, then enemy") + +result = (rock - paper) % 3 +print(f"{rock} - {paper} % 3 = {result}") + +result = (paper - scissors) % 3 +print(f"{paper} - {scissors} % 3 = {result}") + +result = (scissors - rock) % 3 +print(f"{scissors} - {rock} % 3 = {result}") + +print("lose = 2") +lose = 2 + +result = (paper - rock) % 3 +print(f"{paper} - {rock} % 3 = {result}") + +result = (scissors - paper) % 3 +print(f"{scissors} - {paper} % 3 = {result}") + +result = (rock - scissors) % 3 +print(f"{rock} - {scissors} % 3 = {result}") + +print("win = 1") +win = 1 + +result = (paper - paper) % 3 +print(f"{paper} - {paper} % 3 = {result}") + +result = (scissors - scissors) % 3 +print(f"{scissors} - {scissors} % 3 = {result}") + +result = (rock - rock) % 3 +print(f"{rock} - {rock} % 3 = {result}") + +print("draw = 0") +draw = 0 + +print("(friendly - outcome = enemy) % 3") +outcome = (rock - win) % 3 +print(f"{outcome} = {scissors}") + +outcome = (paper - win) % 3 +print(f"{outcome} = {rock}") + +outcome = (scissors - win) % 3 +print(f"{outcome} = {paper}")