github.com/cycloss/advent-of-code@v0.0.0-20221210145555-15039b95faa6/2021/day14/parser.dart (about)

     1  class Parser {
     2    final List<String> lines;
     3    List<String> out = [];
     4    Parser(this.lines);
     5    int index = 0;
     6  
     7    Problem processLines() {
     8      index = 0;
     9      var polymer = parsePolymer();
    10      index += 2;
    11      var rules = <Pattern, String>{};
    12      for (; index < lines.length; index++) {
    13        if (lines[index] == "") {
    14          continue;
    15        }
    16        var raw = lines[index].split(" ");
    17        var rawPattern = raw[0].split("");
    18        var pattern = Pattern(rawPattern[0], rawPattern[1]);
    19        rules[pattern] = raw[2];
    20      }
    21      return Problem(polymer, rules);
    22    }
    23  
    24    LinkedList parsePolymer() {
    25      var line = lines[index];
    26      var raw = line.split("");
    27      return LinkedList(raw);
    28    }
    29  }
    30  
    31  class Problem {
    32    LinkedList polymer;
    33    final Map<Pattern, String> rules;
    34    Problem(this.polymer, this.rules);
    35  }
    36  
    37  class Pattern {
    38    final String first, second;
    39    Pattern(this.first, this.second);
    40  
    41    @override
    42    int get hashCode {
    43      return Object.hashAll([first, second]);
    44    }
    45  
    46    @override
    47    bool operator ==(o) {
    48      return o is Pattern && first == o.first && second == o.second;
    49    }
    50  }
    51  
    52  class LinkedList {
    53    Node? head;
    54  
    55    LinkedList(List<String> values) {
    56      late Node current;
    57      for (var i = 0; i < values.length; i++) {
    58        if (head == null) {
    59          head = Node(values[i]);
    60          current = head!;
    61        } else {
    62          var next = Node(values[i]);
    63          current.next = next;
    64          current = next;
    65        }
    66      }
    67    }
    68  }
    69  
    70  class Node {
    71    String value;
    72    Node? next;
    73    Node(this.value);
    74  }