summary refs log tree commit diff homepage
path: root/src/bin
diff options
context:
space:
mode:
authorJune McEnroe <programble@gmail.com>2016-12-10 18:07:27 -0500
committerJune McEnroe <programble@gmail.com>2016-12-10 18:08:09 -0500
commitdab2762e3bb31879384c14c4859cae7cab267a83 (patch)
treed79e41107cc77b191f384f15873f8c0692088c7b /src/bin
parentDay 9 (diff)
downloadaoc-dab2762e3bb31879384c14c4859cae7cab267a83.tar.gz
aoc-dab2762e3bb31879384c14c4859cae7cab267a83.zip
Refactor day 9
Diffstat (limited to 'src/bin')
-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()
 }
 
bin/up.sh?id=60eccd34482fe1f6daf15d0e0ff95b2dc6c6b17c&follow=1'>Don't use $ inside $(())June McEnroe 2020-03-03Remove setoptJune McEnroe 2020-03-03Use getopts in shell scriptsJune McEnroe WTF why did no one tell me about this? 2020-02-27Style %T outside of Rs in italicJune McEnroe 2020-02-26Add Fierce Femmes and Notorious LiarsJune McEnroe 2020-02-23Add This Is How You Lose the Time WarJune McEnroe 2020-02-22Add See Ya LaterJune McEnroe 2020-02-20Remove wiki scriptJune McEnroe Wikipedia seems to have removed the one-sentence extracts from the opensearch results. Too bad. It's not a wiki script, what we need is a command that fetches single-sentence summaries of articles on Wikipedia. 2020-02-19Add The Obelisk GateJune McEnroe 2020-02-17Add Four Tet — HandsJune McEnroe One from the cafe that caught my attention. 2020-02-12Simplify macOS notify-sendJune McEnroe 2020-02-12Add imbox and notemap to pageJune McEnroe 2020-02-12Collapse simple linksJune McEnroe 2020-02-12Move catgirl up the pageJune McEnroe 2020-02-12Update catgirl pty grabJune McEnroe 2020-02-12Link to cgit /about pages where appropriateJune McEnroe 2020-02-11Separate LINKS from BINS for html to workJune McEnroe 2020-02-11Add margin to Bl-bullet itemsJune McEnroe 2020-02-10Match URLs inside parens or with paired parens insideJune McEnroe 2020-02-10Duplicate effective URL before passing it back to curlJune McEnroe Apparently sometimes it didn't like receiving its own internal storage to parse again. Understandable. 2020-02-09Add To Be Taught, If FortunateJune McEnroe 2020-02-04Add The Future of Another TimelineJune McEnroe Wow. One of the best I've read. 2020-01-31Reorganize the Makefile for the umpteenth timeJune McEnroe Broke out LDLIBS for each bin, and made everything more uniform. 2020-01-28Change scout sensitivity to 1.4June McEnroe idk it seems to work. 2020-01-28Import shows.txtJune McEnroe