Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///INTERPRET SOME MACHINE CODE.
- // We want to build a pool of states, and understand which lead where.
- // Once we have these, we want to find the most (then least) *expensive* route where
- // at each step cost = (10* cost sofar) + next digit. (ie number made by
- // joining all digits so far).
- import 'dart:io';
- import 'package:more/more.dart';
- import 'package:collection/collection.dart';
- late List<List<int>> params;
- buildParams(List<String> lines) {
- var sections = lines.splitBefore((e) => e.startsWith('inp'));
- params = sections
- .map((s) => [
- for (var i in [4, 5, 15]) int.parse(s[i].split(' ')[2])
- ])
- .toList();
- }
- int buildNodes([bool mn = false]) {
- // `z` is the only part of state that actually gets used.
- // build a map of current `z`s and their best(highest/lowest) costs.
- Map<int, int> queue = {0: 0};
- for (var i in 0.to(14)) {
- print('next section - queue ${queue.length}');
- var nextMap = <int, int>{};
- for (var e in queue.entries) {
- var current = e.key;
- var c0 = e.value * 10;
- for (var d in 1.to(10)) {
- var c = c0 + d;
- var endState = runSectionNumber(i, d, current);
- // Only keep track of the identical states that were most
- // expensive to create.
- if (!mn && (nextMap[endState] ?? 0) < c) {
- nextMap[endState] = c;
- } else {
- if ((nextMap[endState] ?? maxSafeInteger) > c) {
- nextMap[endState] = c;
- }
- }
- }
- }
- queue = nextMap;
- }
- // We now have a list of final state Nodes.
- var good = queue.keys.where((e) => e == 0);
- var cost =
- mn ? good.map((e) => queue[e]).min() : good.map((e) => queue[e]).max();
- return cost!;
- }
- int runSectionNumber(int sn, int digit, int z) {
- int w = digit;
- List<int> p = params[sn];
- var x = z % 26;
- z = z ~/ p[0];
- x += p[1];
- x = (x != w) ? 1 : 0;
- var y = x == 1 ? 26 : 1;
- z *= y;
- y = w + p[2];
- y *= x;
- z += y;
- return z;
- }
- main() {
- var lines = File('data/data24_01.txt').readAsLinesSync();
- var t = DateTime.now();
- buildParams(lines);
- print(buildNodes()); // 93499629698999
- print(buildNodes(true)); // 11164118121471
- print('${DateTime.now().difference(t).inMilliseconds} milliseconds');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement