Day 2: Rock Paper Scissors [AOC2022 Python]

preview_player
Показать описание
In Day 2, each line of input represents a game of Rock Paper Scissors. In part 1, I get both players selection, and I calculate a score. In part 2, I get the first players selection and the result, and do a similar score calculation.

#adventofcode #python

[00:00] Introduction
[00:11] Part 1 description
[01:12] Starting part 1 solve
[01:40] Generating a map for the nine possible inputs
[04:00] Applying map to each line, solving part 1
[05:12] Part 2 description
[05:34] Updating new map for part 2
[07:00] Applying part 2 map, solving part 2
Рекомендации по теме
Комментарии
Автор

You can use ascii indices to decide win or loss or draw

let other_move = "A"
let my_move = "X"
other_move_idx = other_move - "A"
my_move_idx = my_move - "X"

if my_move_idx == other_move_idx then DRAW

if my_move_idx == (other_move_idx + 1) % 3. then WIN

if my_move_idx == (other_move_idx - 1 + 3) % 3. then LOSE

saurabhshinde
Автор

I like your steps but stackoverflow seemed to vote for mylist = f.read().splitlines(). Spent way too much time trying to program logic and lookups with various dictionaries instead of just statically defining everything from the beginning.

Adrian-gvfu