summary refs log tree commit diff homepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/bin/day06.rs95
1 files changed, 95 insertions, 0 deletions
diff --git a/src/bin/day06.rs b/src/bin/day06.rs
new file mode 100644
index 0000000..bdd9147
--- /dev/null
+++ b/src/bin/day06.rs
@@ -0,0 +1,95 @@
+use std::io::{self, Read};
+
+struct Runs<T, I> {
+    inner: I,
+    current: Option<T>,
+    count: usize,
+}
+
+impl<T, I> From<I> for Runs<T, I> {
+    fn from(iter: I) -> Self {
+        Runs {
+            inner: iter,
+            current: None,
+            count: 0,
+        }
+    }
+}
+
+// Gross.
+impl<T: Copy + PartialEq, I: Iterator<Item = T>> Iterator for Runs<T, I> {
+    type Item = (T, usize);
+
+    fn next(&mut self) -> Option<(T, usize)> {
+        for c in &mut self.inner {
+            match self.current {
+                None => {
+                    self.current = Some(c);
+                    self.count = 1;
+                },
+                Some(r) if r == c => {
+                    self.count += 1;
+                },
+                Some(r) => {
+                    self.current = Some(c);
+                    let run = self.count;
+                    self.count = 1;
+                    return Some((r, run));
+                },
+            }
+        }
+
+        self.current.take().map(|c| (c, self.count))
+    }
+}
+
+fn solve(input: &str) -> String {
+    let len = input.find('\n').unwrap_or(input.len());
+    let mut columns = vec![Vec::new(); len];
+
+    for line in input.lines() {
+        for (i, c) in line.chars().enumerate() {
+            columns[i].push(c);
+        }
+    }
+
+    columns.into_iter()
+        .map(|mut column| {
+            column.sort();
+            Runs::from(column.into_iter())
+                .max_by_key(|run| run.1)
+                .unwrap()
+                .0
+        })
+        .collect()
+}
+
+fn main() {
+    let mut input = String::new();
+    io::stdin().read_to_string(&mut input).unwrap();
+
+    println!("Part 1: {}", solve(&input));
+}
+
+#[test]
+fn part1() {
+    let input = "
+eedadn
+drvtee
+eandsr
+raavrd
+atevrs
+tsrnev
+sdttsa
+rasrtv
+nssdts
+ntnada
+svetve
+tesnvt
+vntsnd
+vrdear
+dvrsen
+enarar
+";
+    assert_eq!("easter", solve(input.trim()));
+}
pan>Add 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 2020-02-19Add The Obelisk GateJune McEnroe 2020-02-17Add Four Tet — HandsJune McEnroe 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 2020-02-09Add To Be Taught, If FortunateJune McEnroe 2020-02-04Add The Future of Another TimelineJune McEnroe 2020-01-31Reorganize the Makefile for the umpteenth timeJune McEnroe 2020-01-28Change scout sensitivity to 1.4June McEnroe 2020-01-28Import shows.txtJune McEnroe