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()));
+}
3 16:27:22 -0400'>2022-07-13Set push.autoSetupRemoteJune McEnroe 2022-07-08Remove TOURJune McEnroe 2022-07-03Add The Bone Shard EmperorJune McEnroe 2022-06-25Bump xterm font size to 12June McEnroe 2022-06-10Handle subshells (and functions) inside substitutionsJune McEnroe 2022-06-10Switch to jorts Install scriptJune McEnroe 2022-06-08Indicate if still reading or no resultsJune McEnroe 2022-06-08Add Maiden, Mother, CroneJune McEnroe 2022-06-05FIRST SHOW IN 2.5 YEARS BABEY!!!June McEnroe 2022-06-03Set line number on File linesJune McEnroe 2022-06-03Stop polling stdin after EOFJune McEnroe 2022-06-02Set TABSIZE=4June McEnroe 2022-06-02Do basic match highlightingJune McEnroe 2022-06-02Clean up parsing a littleJune McEnroe 2022-06-02Don't duplicate path stringJune McEnroe 2022-06-02Use stderr instead of /dev/tty, realloc buffer if lines too longJune McEnroe 2022-06-02Add initial working version of qfJune McEnroe 2022-05-29Set prompt for okshJune McEnroe