summary refs log tree commit diff homepage
path: root/2017/src/bin/day08.rs
diff options
context:
space:
mode:
Diffstat (limited to '2017/src/bin/day08.rs')
-rw-r--r--2017/src/bin/day08.rs57
1 files changed, 57 insertions, 0 deletions
diff --git a/2017/src/bin/day08.rs b/2017/src/bin/day08.rs
new file mode 100644
index 0000000..e5a9ad0
--- /dev/null
+++ b/2017/src/bin/day08.rs
@@ -0,0 +1,57 @@
+use std::collections::HashMap;
+use std::io::{self, Read};
+
+fn solve1(input: &str) -> i32 {
+    let mut regs = HashMap::new();
+    for line in input.lines() {
+        let mut words = line.split_whitespace();
+        let dest = words.next().unwrap();
+        let op = words.next().unwrap();
+        let val = words.next().unwrap();
+        assert_eq!(Some("if"), words.next());
+        let src = words.next().unwrap();
+        let cmp = words.next().unwrap();
+        let rhs = words.next().unwrap();
+
+        let mut val: i32 = val.parse().unwrap();
+        if op == "dec" {
+            val = -val;
+        }
+        let rhs: i32 = rhs.parse().unwrap();
+        let src = *regs.entry(src.to_owned()).or_insert(0);
+        let dest = regs.entry(dest.to_owned()).or_insert(0);
+
+        let cond = match cmp {
+            "==" => src == rhs,
+            "!=" => src != rhs,
+            "<=" => src <= rhs,
+            ">=" => src >= rhs,
+            "<" => src < rhs,
+            ">" => src > rhs,
+            _ => unimplemented!(),
+        };
+        if cond {
+            *dest += val;
+        }
+    }
+    regs.values().cloned().max().unwrap()
+}
+
+fn main() {
+    let mut input = String::new();
+    io::stdin().read_to_string(&mut input).unwrap();
+
+    println!("Part 1: {}", solve1(&input));
+}
+
+#[test]
+fn part1() {
+    assert_eq!(1, solve1(
+"\
+b inc 5 if a > 1
+a inc 1 if b < 5
+c dec -10 if a >= 1
+c inc -20 if c == 10
+"
+    ));
+}
href='/src/commit/bin/man1/hi.1?id=48014f1c32317a838a9724e6ee1354236ff6ead2&follow=1'>Remove hi line numberingJune McEnroe Tags are much better for referring to specific parts of a file and line numbering is better done by a post-processing tool such as cat -n or producing a two-column HTML <table>. 2019-02-18Add Tag class to hiJune McEnroe 2019-02-17Generate HTML with hi -n -f html -o anchorJune McEnroe Running hi twice to insert stuff between the head and the content is a bit of a hack but oh well. 2019-02-17Add hi -f html -o anchor for line number linksJune McEnroe 2019-02-17Simplify temp trap in upJune McEnroe 2019-02-17Add line numbers to hiJune McEnroe Renames previous -n option to -m to stay consistent with cat -n. Prefixing lines with line numbers affects where the first tab indent ends up relative to the text above it. Not sure if it's worth fixing somehow. 2019-02-17Always split spans after newlinesJune McEnroe Simplifies ANSI and IRC output code, and prepares for line numbered output. 2019-02-15Color format specifiers light cyan in vimJune McEnroe 2019-02-15Highlight Interp as yellowJune McEnroe 2019-02-15Highlight strings in sh command substitutionsJune McEnroe 2019-02-15Add nmap gpJune McEnroe 2019-02-14Avoid newline when copying URL to pasteboardJune McEnroe 2019-02-13Add forgotten "sixth" book of H2G2June McEnroe