Advertisement
mykdavies

AOC 2022 Day 20

Dec 20th, 2022 (edited)
1,197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 0.86 KB | None | 0 0
  1. /// Mix a list according to some rules
  2. /// 1) Once.
  3. /// 2) Ten times withe a key.
  4. ///
  5.  
  6. import 'package:collection/collection.dart';
  7. import 'package:more/more.dart';
  8.  
  9. buildList(List<String> lines, {key = 1}) =>
  10.     lines.map((e) => (int.parse(e) * key).toInt()).indexed().toList();
  11.  
  12. process(List<Indexed<int>> ns) {
  13.   for (var n in 0.to(ns.length)) {
  14.     var ix = ns.indexWhere((e) => e.index == n);
  15.     var el = ns.removeAt(ix);
  16.     ns.insert((ix + el.value) % ns.length, el);
  17.   }
  18.   return ns;
  19. }
  20.  
  21. int calcAnswer(List<Indexed<int>> ns) {
  22.   var ix = ns.indexWhere((e) => e.value == 0);
  23.   return [1000, 2000, 3000].map((e) => ns[(ix + e) % ns.length].value).sum;
  24. }
  25.  
  26. part1(List<String> lines) => calcAnswer(process(buildList(lines)));
  27.  
  28. part2(List<String> lines) => calcAnswer(
  29.     0.to(10).fold(buildList(lines, key: 811589153), (s, _) => process(s)));
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement