summary refs log tree commit diff homepage
path: root/src/bin
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2016-12-06 01:32:18 -0500
committerJune McEnroe <june@causal.agency>2020-11-22 00:14:24 -0500
commitcf1bf859b995a8dabf23a46b69c574ee18caee4b (patch)
treee68776f94ddf67d9d354456896e714831d038795 /src/bin
parentDay 6 (gross though) (diff)
downloadaoc-cf1bf859b995a8dabf23a46b69c574ee18caee4b.tar.gz
aoc-cf1bf859b995a8dabf23a46b69c574ee18caee4b.zip
Day 6, less gross?
Diffstat (limited to 'src/bin')
-rw-r--r--src/bin/day06.rs62
1 files changed, 13 insertions, 49 deletions
diff --git a/src/bin/day06.rs b/src/bin/day06.rs
index bdd9147..72ee658 100644
--- a/src/bin/day06.rs
+++ b/src/bin/day06.rs
@@ -1,46 +1,12 @@
+use std::collections::HashMap;
 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 frequencies(chars: &[char]) -> HashMap<char, u32> {
+    let mut map = HashMap::new();
+    for &ch in chars {
+        *map.entry(ch).or_insert(0) += 1;
     }
+    map
 }
 
 fn solve(input: &str) -> String {
@@ -48,19 +14,17 @@ fn solve(input: &str) -> String {
     let mut columns = vec![Vec::new(); len];
 
     for line in input.lines() {
-        for (i, c) in line.chars().enumerate() {
-            columns[i].push(c);
+        for (i, ch) in line.chars().enumerate() {
+            columns[i].push(ch);
         }
     }
 
     columns.into_iter()
-        .map(|mut column| {
-            column.sort();
-            Runs::from(column.into_iter())
-                .max_by_key(|run| run.1)
-                .unwrap()
-                .0
-        })
+        .map(|column| frequencies(&column))
+        .map(IntoIterator::into_iter)
+        .map(|iter| iter.max_by_key(|&(_, v)| v))
+        .map(Option::unwrap)
+        .map(|(ch, _)| ch)
         .collect()
 }