advent-of-code

advent of code (partial solutions)
Log | Files | Refs | README

commit 04ea046478277fe2a0c268babb39dd12a11cb7cc
parent bddd9e2eaa0c5e47afb477652fa8764d8cfbb142
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Wed,  8 Dec 2021 21:28:33 -0800

bad day 8 solutoin

Diffstat:
A2021/aoc08.rs | 133+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 133 insertions(+), 0 deletions(-)

diff --git a/2021/aoc08.rs b/2021/aoc08.rs @@ -0,0 +1,133 @@ +use std::collections::HashSet; +use std::error::Error; +use std::io::{self, BufRead}; + +const segments: &[i32; 8] = &[0, 0, 1, 1, 1, 0, 0, 1]; + +// number translation + +fn main() -> Result<(), Box<dyn Error>> { + let mut stdin = io::stdin(); + let lines: Vec<String> = stdin.lock().lines().map(|l| l.unwrap()).collect(); + let part1: i32 = lines + .iter() + .map(|l| { + let r: Vec<&str> = l.split("|").collect(); + r[1].split(" ") + .filter(|i| *i != "") + .map(|i| segments[i.len()]) + .sum::<i32>() + }) + .sum(); + println!("{}", part1); + + let part2: i32 = lines + .iter() + .map(|l| { + let r: Vec<&str> = l.split("|").collect(); + let mut inputs: Vec<HashSet<char>> = + r[0].split(" ").map(|c| c.chars().collect()).collect(); + let mut mapping: [char; 7] = [' ', ' ', ' ', ' ', ' ', ' ', ' ']; + // Mapping : clockwise from top, then middle + inputs.sort_by_key(|x| x.len()); + // first element == {} + let mut one: HashSet<char> = inputs[1].clone(); + let seven: &HashSet<char> = &inputs[2]; + let four: &HashSet<char> = &inputs[3]; + + mapping[0] = *(seven - &one).iter().next().unwrap(); // top + + let mut fourhand: HashSet<char> = four - &one; + + let mut tmp = &(&inputs[4] & &inputs[5]) & &inputs[6]; + mapping[5] = *(&fourhand - &tmp).iter().next().unwrap(); + fourhand.remove(&mapping[5]); + mapping[6] = *fourhand.iter().next().unwrap(); + tmp.remove(&mapping[0]); + tmp.remove(&mapping[6]); + mapping[3] = *tmp.iter().next().unwrap(); + + // zero + let mut zero: &HashSet<char> = &HashSet::new(); + for ud in 7..=9 { + if !inputs[ud].contains(&mapping[6]) { + zero = &inputs[ud]; + } + } + + let t = &arr_to_set(mapping); + for ud in 4..=6 { + let diff = (&inputs[ud] - t); + if diff.len() == 1 { + mapping[2] = *diff.iter().next().unwrap(); // bottom right + } + } + + one.remove(&mapping[2]); + mapping[1] = *one.iter().next().unwrap(); + + // life is suffering + mapping[4] = *(&"abcdefg".chars().collect::<HashSet<char>>() - &arr_to_set(mapping)) + .iter() + .next() + .unwrap(); + println!("{:?}", mapping); + // decoding done + // + // + let mut total = 0; + for n in r[1].split(" ") { + total *= 10; + total += get_number(n, &mapping); + } + total + }) + .sum(); + + println!("{:?}", part2); + + // 1 4 7 8 + Ok(()) +} + +fn get_number(i: &str, map: &[char; 7]) -> i32 { + let s: HashSet<char> = i.chars().collect(); + if i.len() == 2 { + return 1; + } + if i.len() == 3 { + return 7; + } + if i.len() == 4 { + return 4; + } + if i.len() == 7 { + return 8; + } + if i.len() == 5 { + if s.contains(&map[4]) { + return 2; + } + if s.contains(&map[1]) { + return 3; + } + return 5; + } + // len 6 + if !s.contains(&map[6]) { + return 0; + } + if !s.contains(&map[1]) { + return 6; + } + return 9; +} +fn arr_to_set(i: [char; 7]) -> HashSet<char> { + let mut o = HashSet::new(); + for c in i.iter() { + if *c != ' ' { + o.insert(*c); + } + } + o +}