summary refs log tree commit diff homepage
path: root/2017/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--2017/src/bin/day09.rs61
1 files changed, 61 insertions, 0 deletions
diff --git a/2017/src/bin/day09.rs b/2017/src/bin/day09.rs
new file mode 100644
index 0000000..a8492d0
--- /dev/null
+++ b/2017/src/bin/day09.rs
@@ -0,0 +1,61 @@
+use std::io::{self, Read};
+
+#[derive(Debug, Clone, Copy)]
+enum State {
+    Group,
+    Garbage,
+    Ignore,
+}
+
+use self::State::*;
+
+#[derive(Debug, Clone, Copy)]
+struct Machine(State, u32);
+
+impl Machine {
+    fn next(self, input: char) -> Self {
+        match (self.0, input) {
+            (Ignore, _) => Machine(Garbage, self.1),
+            (Garbage, '!') => Machine(Ignore, self.1),
+            (Garbage, '>') => Machine(Group, self.1),
+            (Garbage, _) => self,
+            (Group, '<') => Machine(Garbage, self.1),
+            (Group, '{') => Machine(Group, self.1 + 1),
+            (Group, '}') => Machine(Group, self.1 - 1),
+            (Group, ',') => self,
+            _ => unimplemented!(),
+        }
+    }
+}
+
+fn solve1(input: &str) -> u32 {
+    let mut score = 0;
+    let mut state = Machine(Group, 0);
+    for c in input.chars() {
+        let next = state.next(c);
+        if next.1 > state.1 {
+            score += next.1;
+        }
+        state = next;
+    }
+    score
+}
+
+fn main() {
+    let mut input = String::new();
+    io::stdin().read_to_string(&mut input).unwrap();
+
+    println!("Part 1: {}", solve1(input.trim()));
+}
+
+#[test]
+fn part1() {
+    assert_eq!(1, solve1("{}"));
+    assert_eq!(6, solve1("{{{}}}"));
+    assert_eq!(5, solve1("{{},{}}"));
+    assert_eq!(16, solve1("{{{},{},{{}}}}"));
+    assert_eq!(1, solve1("{<a>,<a>,<a>,<a>}"));
+    assert_eq!(9, solve1("{{<ab>},{<ab>},{<ab>},{<ab>}}"));
+    assert_eq!(9, solve1("{{<!!>},{<!!>},{<!!>},{<!!>}}"));
+    assert_eq!(3, solve1("{{<a!>},{<a!>},{<a!>},{<ab>}}"));
+}
d>2013-05-23Replace VimClojure with vim-clojure-staticJune McEnroe 2013-05-18Do not run gitgutter eagerlyJune McEnroe 2013-05-15Remove tabularJune McEnroe 2013-05-15Use long names for all settingsJune McEnroe 2013-05-15Add vim-gitgutterJune McEnroe 2013-04-23Remove unused pluginsJune McEnroe 2013-04-23Switch to base16-default colorschemeJune McEnroe 2013-03-26Replace nerdcommenter with vim-commentaryJune McEnroe 2013-03-24Add paredit.vimJune McEnroe 2013-03-14Update base16-vimJune McEnroe It's still crap 2013-03-12Hide mode from below statuslineJune McEnroe 2013-03-12Switch to powerlineJune McEnroe Which I'm not going to bother configuring because it thinks it's all fancy and for other things that aren't Vim so it stores its config file in ~/.config which I keep in a different repo and I only want to use it for Vim so fuck you, Powerline. </rant> 2013-03-12Remove vim-powerlineJune McEnroe 2013-02-03Update base16 and refheapJune McEnroe 2013-02-03Ignore plugin/private.vimJune McEnroe 2012-09-29Disable GUI dialogsJune McEnroe