From df51dcdaa86578d4ead87e00c672b11383757e25 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Wed, 8 Dec 2021 18:49:38 -0800 Subject: [PATCH] Day 8 part 1 add a bunch of variations --- 2021/src/day8.rs | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/2021/src/day8.rs b/2021/src/day8.rs index 5be26ea..ef2e180 100644 --- a/2021/src/day8.rs +++ b/2021/src/day8.rs @@ -127,7 +127,7 @@ use std::{ use anyhow::Result; use aoc_runner_derive::aoc; -#[aoc(day8, part1)] +#[aoc(day8, part1, original)] fn part1(input: &str) -> Result { Ok(input .lines() @@ -143,6 +143,44 @@ fn part1(input: &str) -> Result { .sum()) } +#[aoc(day8, part1, no_result)] +fn part1_no_result(input: &str) -> usize { + input + .lines() + .map(|l| { + l.split_once(" | ") + .unwrap() + .1 + .split(' ') + // 1 | 7 | 4 | 8 + .filter(|s| matches!(s.len(), 2 | 3 | 4 | 7)) + .count() + }) + .sum() +} + +#[aoc(day8, part1, flat_map)] +fn part1_flat_map(input: &str) -> usize { + input + .lines() + .flat_map(|l| l.split_once(" | ").unwrap().1.split(' ')) + // 1 | 7 | 4 | 8 + .filter(|s| matches!(s.len(), 2 | 3 | 4 | 7)) + .count() +} + +#[aoc(day8, part1, glenng)] +fn part1_glenng(input: &str) -> usize { + input + .split('\n') + .flat_map(|line| { + let (_, output) = line.split_once(" | ").unwrap(); + output.split(' ') + }) + .filter(|s| [2usize, 3, 4, 7].contains(&s.len())) + .count() +} + #[derive(Copy, Clone, Eq, Hash, PartialEq)] struct Segment(u8);