Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// Mix a list according to some rules
- /// 1) Once.
- /// 2) Ten times withe a key.
- ///
- import 'package:collection/collection.dart';
- import 'package:more/more.dart';
- buildList(List<String> lines, {key = 1}) =>
- lines.map((e) => (int.parse(e) * key).toInt()).indexed().toList();
- process(List<Indexed<int>> ns) {
- for (var n in 0.to(ns.length)) {
- var ix = ns.indexWhere((e) => e.index == n);
- var el = ns.removeAt(ix);
- ns.insert((ix + el.value) % ns.length, el);
- }
- return ns;
- }
- int calcAnswer(List<Indexed<int>> ns) {
- var ix = ns.indexWhere((e) => e.value == 0);
- return [1000, 2000, 3000].map((e) => ns[(ix + e) % ns.length].value).sum;
- }
- part1(List<String> lines) => calcAnswer(process(buildList(lines)));
- part2(List<String> lines) => calcAnswer(
- 0.to(10).fold(buildList(lines, key: 811589153), (s, _) => process(s)));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement