|
| 1 | +use crate::util; |
| 2 | +use itertools::Itertools; |
| 3 | + |
| 4 | +pub fn run() { |
| 5 | + let raw_input = util::read_input("inputs/day8.txt").unwrap(); |
| 6 | + let input = process(&raw_input); |
| 7 | + println!("Part 1: {}", part_1(&input)); |
| 8 | + println!("Part 2: {}", part_2(&input)); |
| 9 | +} |
| 10 | + |
| 11 | +fn process(input: &str) -> Vec<(Vec<&str>, Vec<&str>)> { |
| 12 | + input |
| 13 | + .lines() |
| 14 | + .map(|line| { |
| 15 | + let (input, output) = line.trim().split_once(" | ").unwrap(); |
| 16 | + ( |
| 17 | + input.split_whitespace().collect(), |
| 18 | + output.split_whitespace().collect(), |
| 19 | + ) |
| 20 | + }) |
| 21 | + .collect() |
| 22 | +} |
| 23 | + |
| 24 | +fn part_1(input: &Vec<(Vec<&str>, Vec<&str>)>) -> u32 { |
| 25 | + let unique_segment_counts = vec![2, 3, 4, 7]; |
| 26 | + let outputs: Vec<Vec<&str>> = input.iter().map(|line| line.1.clone()).collect(); |
| 27 | + outputs |
| 28 | + .iter() |
| 29 | + .map(|out| { |
| 30 | + out.iter() |
| 31 | + .filter(|digit| unique_segment_counts.contains(&digit.len())) |
| 32 | + .collect() |
| 33 | + }) |
| 34 | + .fold(0, |unique_segment_counter, uniques: Vec<&&str>| { |
| 35 | + unique_segment_counter + uniques.len() as u32 |
| 36 | + }) |
| 37 | +} |
| 38 | + |
| 39 | +fn part_2(input: &Vec<(Vec<&str>, Vec<&str>)>) -> u32 { |
| 40 | + input |
| 41 | + .iter() |
| 42 | + .map(|(inputs, output)| { |
| 43 | + let mut patterns = vec![""; 10]; |
| 44 | + // check for 1, 4, 7, and 8. remove from inputs and store in mapping tracker |
| 45 | + let mut inputs = inputs.clone(); |
| 46 | + inputs.retain(|input| match input.len() { |
| 47 | + 2 => { |
| 48 | + patterns[1] = input; |
| 49 | + false |
| 50 | + } |
| 51 | + 3 => { |
| 52 | + patterns[7] = input; |
| 53 | + false |
| 54 | + } |
| 55 | + 4 => { |
| 56 | + patterns[4] = input; |
| 57 | + false |
| 58 | + } |
| 59 | + 7 => { |
| 60 | + patterns[8] = input; |
| 61 | + false |
| 62 | + } |
| 63 | + _ => true, |
| 64 | + }); |
| 65 | + // extract 3 and 9 |
| 66 | + inputs.retain(|input| { |
| 67 | + let contains_4 = patterns[4].chars().all(|segment| input.contains(segment)); |
| 68 | + let contains_7 = patterns[7].chars().all(|segment| input.contains(segment)); |
| 69 | + let contains_1 = patterns[1].chars().all(|segment| input.contains(segment)); |
| 70 | + match input.len() { |
| 71 | + 6 if contains_4 && contains_7 => { |
| 72 | + patterns[9] = input; |
| 73 | + false |
| 74 | + } |
| 75 | + 5 if contains_1 => { |
| 76 | + patterns[3] = input; |
| 77 | + false |
| 78 | + } |
| 79 | + _ => true, |
| 80 | + } |
| 81 | + }); |
| 82 | + // check for 2 |
| 83 | + inputs.retain(|input| { |
| 84 | + if input.len() == 5 && !input.chars().all(|segment| patterns[9].contains(segment)) { |
| 85 | + patterns[2] = input; |
| 86 | + return false; |
| 87 | + } |
| 88 | + true |
| 89 | + }); |
| 90 | + // 5 is the only thing left with length 5 |
| 91 | + inputs.retain(|input| { |
| 92 | + if input.len() == 5 { |
| 93 | + patterns[5] = input; |
| 94 | + return false; |
| 95 | + } |
| 96 | + true |
| 97 | + }); |
| 98 | + |
| 99 | + // 6 will contain 5 and isn't also 9 |
| 100 | + inputs.retain(|input| { |
| 101 | + let contains_5 = patterns[5].chars().all(|segment| input.contains(segment)); |
| 102 | + match input.len() { |
| 103 | + 6 if input != &patterns[9] && contains_5 => { |
| 104 | + patterns[6] = input; |
| 105 | + false |
| 106 | + } |
| 107 | + _ => true, |
| 108 | + } |
| 109 | + }); |
| 110 | + |
| 111 | + // 0 is the only one left |
| 112 | + patterns[0] = inputs.first().unwrap(); |
| 113 | + let patterns: Vec<String> = patterns |
| 114 | + .iter() |
| 115 | + .map(|digit| digit.chars().sorted().collect::<String>()) |
| 116 | + .collect(); |
| 117 | + output |
| 118 | + .iter() |
| 119 | + .fold("".to_string(), |num, digit| { |
| 120 | + let digit_numeric = patterns |
| 121 | + .iter() |
| 122 | + .position(|pos| digit.chars().sorted().collect::<String>() == *pos); |
| 123 | + if let Some(digit_num) = digit_numeric { |
| 124 | + return num + &digit_num.to_string(); |
| 125 | + } |
| 126 | + num |
| 127 | + }) |
| 128 | + .parse::<u32>() |
| 129 | + .unwrap() |
| 130 | + }) |
| 131 | + .sum() |
| 132 | +} |
0 commit comments