Finish Day 2

This commit is contained in:
2026-02-17 19:46:29 -05:00
parent 7b500eca53
commit c36f402874
2 changed files with 82 additions and 11 deletions

View File

@@ -37,13 +37,31 @@ determine_score :: proc(friendly: Move, enemy: Move) -> int {
return result + int(friendly) return result + int(friendly)
} }
determine_score_alt :: proc(friendly: Move, outcome: Move) -> int { /// Scoring for Part 2 of the puzzle
// X = lose round determine_score_alt :: proc(outcome: Move, enemy: Move) -> int {
// Y = draw round // Redefine the outcomes so that
// Z = win round // draw = 0, win = 1, lose = 2
// 3 defeats 2, 2 defeats 1, 1 defeats 3 outcome_ : int;
// If w := enemy input and v := friendly input, then w = (v - 1) % 2 is a defeat switch outcome {
return 0 // 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 () { main :: proc () {
@@ -56,7 +74,7 @@ main :: proc () {
fh, errno := os.open(os.args[1]) fh, errno := os.open(os.args[1])
if errno != os.ERROR_NONE { if errno != os.ERROR_NONE {
fmt.printf("Cannot open %s: %s\n", os.args[1], os.get_last_error_string()) 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) defer os.close(fh)
@@ -64,6 +82,7 @@ main :: proc () {
defer delete(buffer); defer delete(buffer);
num_read, error := os.read(fh, buffer) num_read, error := os.read(fh, buffer)
score : int = 0;
total_score : int = 0; total_score : int = 0;
friendly : Move friendly : Move
enemy : Move enemy : Move
@@ -73,8 +92,9 @@ main :: proc () {
chr = buffer[0] chr = buffer[0]
switch chr { switch chr {
case '\n': case '\n':
fmt.printf("Score for this line: %d\n", determine_score(friendly, enemy)) score = determine_score_alt(friendly, enemy)
total_score += determine_score(friendly, enemy) fmt.printf("Score for this line: %d\n", score)
total_score += score
case 'A': case 'A':
enemy = .ROCK enemy = .ROCK
case 'B': case 'B':
@@ -92,7 +112,7 @@ main :: proc () {
} }
} }
if chr != '\n' { if chr != '\n' {
total_score += determine_score(friendly, enemy) total_score += determine_score_alt(friendly, enemy)
} }
/// Handle potential errors /// Handle potential errors
if error != os.ERROR_NONE { if error != os.ERROR_NONE {

51
odin/day02/scratch.py Normal file
View File

@@ -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}")