summary refs log tree commit diff homepage
path: root/2017/src/bin
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2017-12-04 00:35:06 -0500
committerJune McEnroe <june@causal.agency>2020-11-22 00:14:25 -0500
commitb442d299ebbf35f89199fe9957665c6983e4f5ee (patch)
treed0f594b14e9bd65d16f43e9b62594f3aa98fb0fb /2017/src/bin
parentDay 4 (diff)
downloadaoc-b442d299ebbf35f89199fe9957665c6983e4f5ee.tar.gz
aoc-b442d299ebbf35f89199fe9957665c6983e4f5ee.zip
Day 4, part 2
That ugly sort map though.
Diffstat (limited to '2017/src/bin')
-rw-r--r--2017/src/bin/day04.rs28
1 files changed, 27 insertions, 1 deletions
diff --git a/2017/src/bin/day04.rs b/2017/src/bin/day04.rs
index 2d6ca52..784a246 100644
--- a/2017/src/bin/day04.rs
+++ b/2017/src/bin/day04.rs
@@ -5,7 +5,23 @@ fn solve1(input: &str) -> u32 {
     let mut valid = 0;
     for phrase in input.lines() {
         let words = phrase.split_whitespace();
-        if words.clone().count() == words.collect::<HashSet<&str>>().len() {
+        if words.clone().count() == words.collect::<HashSet<_>>().len() {
+            valid += 1;
+        }
+    }
+    valid
+}
+
+fn solve2(input: &str) -> u32 {
+    let mut valid = 0;
+    for phrase in input.lines() {
+        let count = phrase.split_whitespace().count();
+        let words = phrase.split_whitespace()
+            .map(str::chars)
+            .map(Iterator::collect::<Vec<_>>)
+            .map(|mut v| { v.sort(); v })
+            .collect::<HashSet<_>>();
+        if count == words.len() {
             valid += 1;
         }
     }
@@ -17,6 +33,7 @@ fn main() {
     io::stdin().read_to_string(&mut input).unwrap();
 
     println!("Part 1: {}", solve1(&input));
+    println!("Part 2: {}", solve2(&input));
 }
 
 #[test]
@@ -25,3 +42,12 @@ fn part1() {
     assert_eq!(0, solve1("aa bb cc dd aa"));
     assert_eq!(1, solve1("aa bb cc dd aaa"));
 }
+
+#[test]
+fn part2() {
+    assert_eq!(1, solve2("abcde fghij"));
+    assert_eq!(0, solve2("abcde xyz ecdab"));
+    assert_eq!(1, solve2("a ab abc abd abf abj"));
+    assert_eq!(1, solve2("iiii oiii ooii oooi oooo"));
+    assert_eq!(0, solve2("oiii ioii iioi iiio"));
+}
2014-04-17remove trailing whitespaces from source filesChristian Hesse 2014-04-12git: update to 1.9.2Christian Hesse Everything works just bumping the version in Makefile and commit hash in submodule. No code changes required. 2014-04-05Fix cgit_parse_url when a repo url is contained in another repo urlJulian Maurice For example, if I have two repos (remove-suffix is enabled): /foo /foo/bar http://cgit/foo/bar/ is interpreted as "repository 'foo', command 'bar'" instead of "repository 'foo/bar'" 2014-03-20Makefile: use more reliable git tarball mirrorJason A. Donenfeld 2014-03-20git: update to 1.9.1Christian Hesse