summary refs log tree commit diff homepage
diff options
context:
space:
mode:
-rw-r--r--src/bin/day09.rs45
1 files changed, 16 insertions, 29 deletions
diff --git a/src/bin/day09.rs b/src/bin/day09.rs
index fb3046f..0c6ca98 100644
--- a/src/bin/day09.rs
+++ b/src/bin/day09.rs
@@ -1,17 +1,19 @@
 use std::io::{self, Read};
-use std::str::FromStr;
 
-struct Chunk {
-    data: String,
-    repeat: u32,
+enum Chunk<'a> {
+    Literal(&'a str),
+    Repeat(u32, &'a str),
 }
 
-impl Chunk {
+impl<'a> Chunk<'a> {
     fn len(&self) -> usize {
-        self.data.len() * self.repeat as usize
+        match *self {
+            Chunk::Literal(s) => s.len(),
+            Chunk::Repeat(n, s) => s.len() * n as usize,
+        }
     }
 
-    fn parse(s: &str) -> Result<(Self, &str), ()> {
+    fn parse(s: &'a str) -> Result<(Self, &'a str), ()> {
         if s.starts_with('(') {
             let mut iter = s[1..].splitn(3, |ch| ch == 'x' || ch == ')');
 
@@ -19,39 +21,24 @@ impl Chunk {
             let repeat = iter.next().ok_or(())?.parse().map_err(|_| ())?;
             let (data, rest) = iter.next().ok_or(())?.split_at(len);
 
-            let chunk = Chunk {
-                data: data.to_owned(),
-                repeat: repeat,
-            };
-
-            Ok((chunk, rest))
+            Ok((Chunk::Repeat(repeat, data), rest))
         } else {
             let paren = s.find('(').unwrap_or(s.len());
-
-            let chunk = Chunk {
-                data: s[..paren].to_owned(),
-                repeat: 1,
-            };
-            let rest = &s[paren..];
-
-            Ok((chunk, rest))
+            Ok((Chunk::Literal(&s[..paren]), &s[paren..]))
         }
     }
 }
 
-struct File {
-    chunks: Vec<Chunk>,
+struct File<'a> {
+    chunks: Vec<Chunk<'a>>,
 }
 
-impl File {
+impl<'a> File<'a> {
     fn len(&self) -> usize {
         self.chunks.iter().map(Chunk::len).sum()
     }
-}
 
-impl FromStr for File {
-    type Err = ();
-    fn from_str(mut s: &str) -> Result<Self, ()> {
+    fn parse(mut s: &'a str) -> Result<Self, ()> {
         let mut chunks = Vec::new();
 
         while !s.is_empty() {
@@ -65,7 +52,7 @@ impl FromStr for File {
 }
 
 fn solve(input: &str) -> usize {
-    let file: File = input.parse().unwrap();
+    let file = File::parse(input).unwrap();
     file.len()
 }
 
ref='/cgit-pink/commit/ui-view.c?h=1.4.1&id=9a8f88658d51aeb86a79ac1121de13562ad2601f&follow=1'>Add ui-commit.c + misc ui cleanupsLars Hjemli 2006-12-15Add a common commit parserLars Hjemli 2006-12-14Add simple pager to log pageLars Hjemli 2006-12-13Add separate makefile-rule to clear current cacheLars Hjemli 2006-12-13Remove implementation details from READMELars Hjemli 2006-12-13Small layout adjustments to summary and blob viewLars Hjemli 2006-12-13Add display of tree content w/ui-tree.cLars Hjemli 2006-12-12cache_lock: do xstrdup/free on lockfileLars Hjemli 2006-12-11Don't truncate valid cachefilesLars Hjemli 2006-12-11Move global variables + callback functions into shared.cLars Hjemli 2006-12-11Move functions for generic object output into ui-view.cLars Hjemli 2006-12-11Move log-functions into ui-log.cLars Hjemli 2006-12-11Move repo summary functions into ui-summary.cLars Hjemli 2006-12-11Move functions for repolist output into ui-repolist.cLars Hjemli 2006-12-11Move common output-functions into ui-shared.cLars Hjemli 2006-12-11Rename config.c to parsing.c + move cgit_parse_query from cgit.c to parsing.cLars Hjemli 2006-12-11Avoid infinite loops in caching layerLars Hjemli 2006-12-11Let 'make install' clear all cachefilesLars Hjemli 2006-12-11Fix cache algorithm loopholeLars Hjemli 2006-12-10Add version identifier in generated filesLars Hjemli 2006-12-10Add license file and copyright noticesLars Hjemli 2006-12-10Add caching infrastructureLars Hjemli