Skip to content

Instantly share code, notes, and snippets.

@mykdavies
Last active November 5, 2023 10:27
Show Gist options
  • Save mykdavies/96a60324a73eb1dc73d0d9378af0068c to your computer and use it in GitHub Desktop.
Save mykdavies/96a60324a73eb1dc73d0d9378af0068c to your computer and use it in GitHub Desktop.
AOC2022 day02
/// Play Rock Paper Scissors
/// 1) By playing the given move.
/// 2) By playing for the desired result.
/// https://dartpad.dev/?id=96a60324a73eb1dc73d0d9378af0068c
import 'package:collection/collection.dart';
var scores = {
'AX': 1 + 3,
'AY': 2 + 6,
'AZ': 3 + 0,
'BX': 1 + 0,
'BY': 2 + 3,
'BZ': 3 + 6,
'CX': 1 + 6,
'CY': 2 + 0,
'CZ': 3 + 3,
};
var strategy = {
'Z': ['AY', 'BZ', 'CX'],
'X': ['AZ', 'BX', 'CY'],
'Y': ['AX', 'BY', 'CZ']
};
int part1(List<String> lines) =>
lines.map((e) => scores[e.split(' ').join('')]!).toList().sum;
int part2(List<String> lines) => lines
.map((e) => e.split(' '))
.map((l) =>
scores[strategy[l.last]!.firstWhere((e) => e.startsWith(l.first))]!)
.sum;
var testdata = """A Y
B X
C Z""".split('\n');
void main(List<String> args) {
assert(part1(testdata) == 15);
assert(part2(testdata) == 12);
print('tests succeeded');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment