2024-01-14 21:04:10 +00:00
|
|
|
//! Day 08 of 2021.
|
|
|
|
|
2021-12-08 14:44:13 +00:00
|
|
|
mod display;
|
|
|
|
|
|
|
|
use display::{CharSet, Display};
|
|
|
|
|
2022-10-02 20:20:09 +00:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2024-01-14 21:04:10 +00:00
|
|
|
/// Get the solution for day 08 of 2021.
|
2022-10-02 20:20:09 +00:00
|
|
|
pub fn solution() -> Solution {
|
|
|
|
Solution::new(Day::new(8, 2021), part_1, part_2).with_expected(521, 1016804)
|
2021-12-08 14:44:13 +00:00
|
|
|
}
|
|
|
|
|
2024-01-14 21:04:10 +00:00
|
|
|
/// The logic to solve part one.
|
2022-10-02 20:20:09 +00:00
|
|
|
fn part_1(input: &str) -> Result<String> {
|
2021-12-08 14:44:13 +00:00
|
|
|
Ok(
|
|
|
|
input
|
|
|
|
.lines()
|
|
|
|
.map(|line| {
|
|
|
|
line
|
|
|
|
.split(" | ")
|
|
|
|
.nth(1)
|
2022-10-02 20:20:09 +00:00
|
|
|
.ok_or_else(|| eyre!("Invalid input: {}", line))
|
2021-12-08 14:44:13 +00:00
|
|
|
})
|
|
|
|
.collect::<Result<Vec<_>>>()?
|
|
|
|
.into_iter()
|
2022-10-02 20:20:09 +00:00
|
|
|
.flat_map(|line| line.split(' ').map(Display::parse))
|
2021-12-08 14:44:13 +00:00
|
|
|
.filter(|display| [2, 4, 3, 7].contains(&display.0))
|
2022-10-02 20:20:09 +00:00
|
|
|
.count()
|
|
|
|
.to_string(),
|
2021-12-08 14:44:13 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-01-14 21:04:10 +00:00
|
|
|
/// The logic to solve part two.
|
2022-10-02 20:20:09 +00:00
|
|
|
fn part_2(input: &str) -> Result<String> {
|
2021-12-08 14:44:13 +00:00
|
|
|
let mut sum = 0;
|
|
|
|
|
|
|
|
for line in input.lines() {
|
|
|
|
let mut split = line.split(" | ");
|
|
|
|
|
|
|
|
// Figure out the displays from the signal side.
|
|
|
|
let displays = Display::figure_out_from_others(
|
|
|
|
split
|
|
|
|
.next()
|
2022-10-02 20:20:09 +00:00
|
|
|
.ok_or_else(|| eyre!("Invalid input: {}", line))?
|
|
|
|
.split(' ')
|
2021-12-08 14:44:13 +00:00
|
|
|
.map(Display::parse)
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// Get all the CharSets from the encoded side.
|
|
|
|
let encoded = split
|
|
|
|
.next()
|
2022-10-02 20:20:09 +00:00
|
|
|
.ok_or_else(|| eyre!("Invalid input: {}", line))?
|
|
|
|
.split(' ')
|
2021-12-08 14:44:13 +00:00
|
|
|
.map(str::chars)
|
2022-10-02 20:20:09 +00:00
|
|
|
.map(CharSet::from_iter);
|
2021-12-08 14:44:13 +00:00
|
|
|
|
|
|
|
// Loop through the encoded numbers backwards so we can use the loop index
|
|
|
|
// to multiply it by 10 to the power of the index.
|
|
|
|
// So 123 would be (3 * 1) + (2 * 10) + (1 * 100).
|
2022-10-02 20:20:09 +00:00
|
|
|
for (index, set) in encoded.rev().enumerate() {
|
2021-12-08 14:44:13 +00:00
|
|
|
let decoded_number = displays
|
|
|
|
.iter()
|
|
|
|
.find(|display| display.1 == &set)
|
|
|
|
.map(|display| display.0)
|
2022-10-02 20:20:09 +00:00
|
|
|
.ok_or_else(|| eyre!("Impossible to decode {:?}", set))?;
|
2021-12-08 14:44:13 +00:00
|
|
|
|
|
|
|
sum += 10_isize.pow(index as u32) * decoded_number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-02 20:20:09 +00:00
|
|
|
Ok(sum.to_string())
|
2021-12-08 14:44:13 +00:00
|
|
|
}
|