This commit is contained in:
Zoé Cassiopée Gauthier 2021-12-20 21:40:27 -05:00
parent 8384ef7a95
commit e37e81188d
5 changed files with 276 additions and 10 deletions

View File

@ -78,3 +78,7 @@ path = "src/day18.rs"
[[bin]] [[bin]]
name = "day19" name = "day19"
path = "src/day19.rs" path = "src/day19.rs"
[[bin]]
name = "day20"
path = "src/day20.rs"

102
inputs/day20.txt Normal file
View File

@ -0,0 +1,102 @@
##....#..#####...#######...##.#..####.###.##..###.####.#....##.##.##.#.#####.#.#.#..##.#...#.##.##.#...###.####..###..##.#.#.#....#.#.#..##.....#.##.#.....#..###...##...#####.##.#..#.....#.#.#.#.#.####.###...###..####..#....###..######....####.#..##.##.###.#..#.#.#.###..#.##...#.###.##.#.###.#...#...#....###.##...#.#####.#..#.##..#.##..###.#...###...#######.#.......#...##........######.#########..##.##...##.#...####.....#####....#######..#..###.#####..#.#......#.##..###.####..##...###.########..#.##..#..#..
.....#.#.##..##.##.#.###...#.#..#..###.#...#.#...####.###.....#..#.##...##.##.#.#.#.###.##.#...##.##
##..##...##..#..####.#.###....#.###..#..#.##......####..#.###...#####.#...#.##.###.###.##.##.##...##
.#..#.#.......#.#.#..#.#.#...###.###.###.###..#.##..##.###.##..#.#....##.#...#####..#..##..#####.##.
####.#######.##.#.....####..##...#.......#.###.##.###.###.#####..#.#######.#.#...#.#.#..####..#.#...
##.#.....##.#..#.#####.###.#.#..#.#..#.#.....###.#.##.#..#.###...#####..#..#.##...#.##......###.##..
#..####.######...#.#####.########.#.#..#.#..##.##..#.######.#..#...####.#.###.###..##..#.#.....###.#
...###..#.####.#..##..###....##....###.##.#...#..#..#..#...##.##.####...###.####.###..#.##.##..#..##
.#.#..#...#..###.####...##..#.#...#...##..####.#..#..#....#...##..#..#.##..##.#......#.###.###.#..#.
......##..#...#.....#.###....##...#.##...#....#..##..#..##..##..#...###############.#.####.###.##.#.
#####.##..#.#.#..#.#..###....#....#......#....##.#.##...##.##.#.#....#.##.##..##...####..##.....##..
#.#..#.##....#......##.###...#.##.#.##.#.#..######..#..##..####.#.#####..#.#..###....##..##.#.###.#.
.###.#####....#.#.###.####.......#.#.##.....###.##....#.#.#....#.##...###..#..#.##.#..##.#....#.#.##
..##...#####..####.####..##.###.#..#..######......##..###....##...#..#..#...#.##..##.####..#####..#.
.#..#.###..#.##..#.#.#.####.#..#.#..#..####.####..#.#..##..#.#....#.##.###..#.#####.#..#...###..#...
#.######..##.#.#.#..#..##....#.......#####.###.##.#.##..##.###..#.#...##.#.#...###.#..#..#....#.##.#
#..##.##..#....#.#.###...#.##.#.#.#.....###.##.###..##.#.....#..##.#...###.##.###########.#.#.#.#...
.##.########.###...####.....###....#.##...#..##..##.###......##.##.#.#.#...#...####...#.##...#..#.#.
##..#.#.#.####...##......#...##.....#.#.###....#.##.....#.#.##.#.########.#......#...##.##.#.#####.#
...#.#....##.###.#..####...##..##...#...###.###.#.###...##..#....##.##..#...#.#.#..##.###.##..#.##..
###.#.###..###.##..#....##.#..##.###.#....#......#######..#.#..#...###.#..##.###.##.#####.##..#.#.##
.#..##.#####..##.#..###.##.###.#.###.#....#.###.##.#.....#...#..#...###....#.#.####....##...........
.#.#..##.#.###.#....#.####.##..##.....#..###.#..#.#.#...#..#....####..#..#.#..###.##.#..####.####..#
##...###..##.######.#....#..#.#.###.###...##.##.#..####..#..####..##...##....####..#...##.#.#.###...
###.#...#..#.#..#..##.##.##..##.#####...#..##.#...##.#..#...##.####.#.##.##.#.##.#......###.#..#....
.....##.#...#.#.#.#...#.#.##.......#.#.######.#..#..##.##......#...#.###.#....###..#..#.##.##.##....
####..##.##...#.###..#...#.##.####.#..#.#..#.###..####.##.#..#..#.##...#####.#.###.##.###..#.##...#.
.##..#.#####.#.#....##.###..##..###.#.#.#.########.##..####.#.##...#.##.##.##.#.#....#.####.#....#.#
#####..#.#.#.####...#.......#.#..##....#.#.###.#..#.##.##.#.#..#....#..#######.#....#......#.####..#
.....###...##...####..#...###.##.#.#...#...#.#..#...#.##..#..#..###...####.#...#....#.##....###...#.
##.....##..#...##.#.#.###...#.###.###.#.#..#..##..##.#....#..#..#...####.#...#..#...##..###...##...#
#.#.##.##..##......##..####...#.##.####....##....####..##.#...###.#.##.#..###..#..###..###..#...#...
#.##..###.##.#..#####.#.##.##..###.#.##.....#.....#...#.....##.#..##..#....#.#..##...##.#.####.##...
..#.#.#.....##.#...#..#....##..##.##.##.#.#.##.#...####..#..#...###.#..##..##...#####.#...#.#...#.#.
.###.##.#.######..#.#..#...#.#...####.#..###.###..###.####.###..#...#.#...######..##......#..###...#
.#...#.#..#.###....#.##.####.########..###.#.#.#.#.###.#.#.##.##...######.#..#...#...#..#....##.####
###.#.#####..###..###.###...###.....#####.#...#.###.#...#..#..####...#.#..###..#.#.......#.....#..#.
.##.#.#..##..#..##..##.#.###.##.##..#.#.##.##.##.#####...###.##...#..##.##.#..##.#.#.###....##....#.
....##......#..##.###.##...#.######.#.#...#.###.######.#.####.....#.####.###....#..##..#.#.##.#...#.
..##.#.......#....#.#........#..#..#..#...#.#.####..#####....#.....##...##.....###.#.#..#.##..##...#
.#.###.###....#...#..###.#######..####.....#.##..#.#.#..##....#.##.#....#..#####.#..#.#######.#.#...
...#.....###.#...#.#####..#..#..#.##.##.#......###.#..#.##.##.####..##..###.....##..##.#.###...#.#.#
##...#.###.#.#.#.########.#.#.##..#.#..##....###.#..#.#........#.#......#.#.#...###...#.###.#.....##
##.#...#.##.#.###..#.#.#####.####..##....##.##....#.##.##.#.##..#..#...#...##.#####.##...#####..#...
..#..#..#####.#..#.#####..#..#......#.#......###..##..#..#.#..##.#####....####...#.##..#.##.#######.
.##.....#..#.##...#...#.###.#.####...##.#...###.#..###.....#..###..####...###...#...#...#...###.#.##
#.#####.##..#.##.###.....#.#####..#.#######...###.#####...#.###..#...#.....####..##..#.#..#..#.#.#.#
.##.###.##.#....#..###..###.##..####..#.....####.##.#..#.##.###...#.....#.####.##....#.#....#.##.#.#
#.#..########...#.#.##....##.#.#..##.#..#.#.#.##...#..##....######.#..##.#...#.#....##.#.##..##..#..
.##..#.##.##...####....#.##..####..##.##..#...#...#...######.#..##.#.....#.#......####...####...####
#..###..#.######...#...#..##.#....#..#..#####....#.####.#.#..###...#.####..#..####..##.##...#..#.#.#
#####.#.#.##..#....#.##.##.###...#.#.#.##..######.#.#...#.#.##.....##.###..#..##..#...#.#....###.#..
#..##.##.###.##..##.#.#.##.##..###.#..##.###...##.####.##..#.....##...##.#.######.###.##..###....##.
#.##....###..#..##.###.#...#.####.......#....##..#.#####..#.#.###########.##.##..#..##.#......######
###.##.###.##.#####..####.#####....#..##.###.....#..#.#.#..#.#.##..#.###..#..###..##.#####...#.#.#.#
#.####.....#.#.##..######......##.#..#.##.##.###..####..##..##.##.#.##.#.#.#..##..#.###.####..##..##
.....#...###...#...###.#.###.#.###.##.#.#...##.#.#..#..##..###.#..#.##..##..#.##..#.#.#.#.#...#.#.##
##..#####....#.##.#..##.##.....##....###..###...####....#..##..#..#.##.#.#.#.#.....#.##.....##..###.
######.#.##.###.#.#..##.#.##.##.....#.##..##.#..#...##.####..##.##..##...###.#......#####.....###.#.
.#..#.###.#..###.#.#.##..##..###....#.#..###.##.#.##..###...###...##.##.###...#..##.###....######...
##..#...#....#.#####..##.####...#...###........###..#...#.#.#.##....#..#.#...###.....###......####.#
.#..##..##...##..#...###..##.####.##.#..#.#.#....##.##...#....###...###.##.#####.##.#...##.####....#
......#....#####...####.##..#.##....###..#.#..#.....####.#...##..##..#.####...##..#..##.#.#.....####
##..##.##.#####.#.####..###.##.###..#...##..#.#.#....#######....#...#...###.######....#####.##.#...#
#.#.####..#.##..##.###.#.#...#.#......##.###...####...#.##...#.#.#...#.#.##..##...#...##...###.#####
##....#.#######...##...#....#.##.#..#...##..#.#.#####.###.###.#.#..##.###..##.###.#.#.##.....#..##.#
#.#.#.#...#..##.#..#####.#.###...###.#####....####...#.#.#...#...##.#...##.######...###...###.##..#.
.######...#....#..#......#.#####.#....####.#....#.##..#.#.#.#..#.##.##...##.#.##.#####.#.###.#..#.#.
#.##..#.#...####....##....#.######.###.##.######...##.....####.#.#..######.###...#.##.#####.###.....
..##.#.#..#.##.#.#.##.##..###..#..#.#.#...##.#..#......#.#######.###.##.#...####.##..###..#.##....#.
.#..#..####...##.###....###....#.#......#..#.###..#..#.###.#.....#.###.#..###..###..#.##.###...#...#
.##....#.#.###......#..#.#..###...######.#..#.####..###...#.##.#.##....###..#..###...##.#.#...#.##.#
.##...#..##.###.###.#....##......#.###.####...##..##.##..##...###.#.#.##.#..#...#.#..###...####.....
#...####.##......###.#####.#..##...#######.##.#####.###.####...##.##.#####...###.###.####..##.#...#.
###...#.#.#.#.###....##..###..####.##..#...#.#.##.##.##....##.##....#.....#...#......##....#...####.
##.####.....#.##.####..#.#....#######.######..##.##.#..###..#.....#..###..#.##..#...#...###.##.##..#
.#..#.#.#.#.####.#..#...#..#.##.#...#..#....#..##....#.##...#.##...#.#..##.#.##....#.#..#######..#..
.#...#.#.#........#.##...####..###..#####..#.#..#.....##...#.####....###.#...###..#####..#...###.##.
##.#.#..#.####..###.#.##.#...##.#..##.###...#.##.##.#####....##...#.#..####.#..#..##.#.#.#..#..##.#.
.#....#..#..#.##.######...#....##..##..#.#...#.##.##.#######..##.##..##..##.#..#.##..####...#.#....#
.#...#.......######...##..#..####....#.#...#..#.##.#.#.#..##.######...#...#.##.###.##..#.####.##..#.
..#.#....##..#..####.##..#..##..#.#.#.#.#...#...##..#..#######..##..##.#..##..#..####.#.#.#.#####..#
##...##.##..##....##..#........######.#.#.#..#.#.#.###...##.#.##..###....#.##...#.#...###.#.###..#..
##......###.#.#......##.#.##.####.#..#...###..##.######.######.#..#####.#...##..#.#..###.####.#.##.#
..##.#..#.#..#..###..##..##.##.#####..#.##....#..#.#.#..##...#.##.##.....##.##....###...#.#.###...#.
###...##.##....#..###...#.##.##.#.#.##.##.###......#...#...#.###.#....##.#.###....#.#.##.#...##.#..#
...#.#.#.#.##..#.#.##.....##.#.#..#...#...##.##..#..##.#####...######..#.##..#.#....###.#.#.##.#..#.
....#####...#.#.##.#...####.......#.#...#.......#.##.....##.##..##...##....###.#.###.....###..######
.###.#...##.#...#.##.##..#.#.######.#..##......##....#....#.....#.#.##.##.#####.#...##....#.........
..#...#..##.#.###.##....#...######....#.#....#.####...#...####.#..#.....####.#.....#..#..#.......#.#
#####..#.##..##.#####.#..##...##..####..###....#....##..#.##...#.#.#.#.#####.#.#...#..##....#.#.###.
..##.#.##.....#.##..###...#.##.#..##...##..#####..#..#..##.#####.####...#....#..##...#.#....####..#.
.##.##....#...#...####.##.#.....###.#...#.#..#..####.#.##.#.##.....#.#.#..#..######.#..#.##.##....#.
.....#.##.#......###.#.#..######..#..#..#####..###.......#.#.##..#.#....####..#..###..#....#.##....#
##.##..##....###.#####...####..##...........##.###..#...#..##...###.#.####....####..#.##.#.#.#.####.
.##.....##..#.#..#.#.....#.#.##....##.#..#.##.#..#.####.##........##..#.##...#.##.#.#.........####..
#..##..#.###.##..#.###..#######.#..#..#...###....#.##..#..###.###.#...#.##.....#......#...#..####...
.#..##....##.#..#..#.#...##.##.#.#.#..##..##...#....###..#.##..#.#.####.#.#.#.#..#.#...#.#..#..#..#.
#####..#.......#.###.#.######.##.###.#.###..###..##.#..#....#...#..##.##.#####..#.#.#.##......#.###.
....##...#.#.##.####.#....##.##.#####.#####.#.#...#.#..######..####.###...#.#...#.#....###.#....##.#
.###...##.#....#..##.######..........#.##..#.#....#.....#.#####..#.#..##......#..#...#.###.#.#.#...#

View File

@ -29,14 +29,14 @@ impl Value {
fn number(&self) -> Option<u32> { fn number(&self) -> Option<u32> {
match self { match self {
Self::Number(n, _) => Some(*n), Self::Number(n, _) => Some(*n),
_ => None _ => None,
} }
} }
fn index(&self) -> Option<usize> { fn index(&self) -> Option<usize> {
match self { match self {
Self::Number(_, i) => Some(*i), Self::Number(_, i) => Some(*i),
_ => None _ => None,
} }
} }
@ -104,8 +104,7 @@ fn find_exploded(tree: &Value, depth: usize) -> Option<&Value> {
if depth == 4 && tree.is_number_pair() { if depth == 4 && tree.is_number_pair() {
Some(tree) Some(tree)
} else { } else {
find_exploded(left, depth + 1) find_exploded(left, depth + 1).or_else(|| find_exploded(right, depth + 1))
.or_else(|| find_exploded(right, depth + 1))
} }
} else { } else {
None None
@ -114,11 +113,13 @@ fn find_exploded(tree: &Value, depth: usize) -> Option<&Value> {
fn find_number(tree: &mut Value, index: usize, n: u32) -> Option<&Value> { fn find_number(tree: &mut Value, index: usize, n: u32) -> Option<&Value> {
match tree { match tree {
Value::Pair(left, right) => find_number(left, index, n).or_else(|| find_number(right, index, n)), Value::Pair(left, right) => {
find_number(left, index, n).or_else(|| find_number(right, index, n))
}
Value::Number(_, i) if *i == index => { Value::Number(_, i) if *i == index => {
tree.add_number(n); tree.add_number(n);
Some(tree) Some(tree)
}, }
_ => None, _ => None,
} }
} }

View File

@ -17,15 +17,16 @@ impl FromStr for Report {
fn make_diffs(reports: &Vec<Report>) -> HashSet<Report> { fn make_diffs(reports: &Vec<Report>) -> HashSet<Report> {
let mut set = HashSet::new(); let mut set = HashSet::new();
for (i, report) in reports.iter().enumerate() { for (i, report) in reports.iter().enumerate() {}
}
set set
} }
fn main() { fn main() {
const INPUT: &str = include_str!("../inputs/day19.txt"); const INPUT: &str = include_str!("../inputs/day19.txt");
let reports = INPUT.split("\n\n").map(|scanner| scanner.lines().map(|line| line.parse().unwrap()).collect()).collect::<Vec<Vec<Report>>>(); let reports = INPUT
.split("\n\n")
.map(|scanner| scanner.lines().map(|line| line.parse().unwrap()).collect())
.collect::<Vec<Vec<Report>>>();
let diffs: Vec<HashSet<Report>> = reports.iter().map(make_diffs).collect(); let diffs: Vec<HashSet<Report>> = reports.iter().map(make_diffs).collect();
println!("solution {}", diffs.len()); println!("solution {}", diffs.len());
} }

158
src/day20.rs Normal file
View File

@ -0,0 +1,158 @@
use std::fmt;
use std::str::FromStr;
#[derive(Clone)]
struct Image {
image: Vec<Vec<char>>,
algorithm: Vec<char>,
background: bool,
}
impl Image {
fn is_light(&self, x: i32, y: i32) -> bool {
if x < 1 || y < 1 || x as usize > self.image[0].len() || y as usize > self.image.len() {
return self.background;
}
match self.image[(y - 1) as usize][(x - 1) as usize] {
'#' => true,
'.' => false,
p => panic!("invalid pixel '{}'", p),
}
}
fn enhance(self) -> Self {
let image = (0..self.image.len() + 2)
.map(|y| {
(0..self.image[0].len() + 2)
.map(|x| {
let mut idx = 0;
if self.is_light(x as i32 - 1, y as i32 - 1) {
idx += 0x100;
}
if self.is_light(x as i32, y as i32 - 1) {
idx += 0x080;
}
if self.is_light(x as i32 + 1, y as i32 - 1) {
idx += 0x040;
}
if self.is_light(x as i32 - 1, y as i32) {
idx += 0x020;
}
if self.is_light(x as i32, y as i32) {
idx += 0x010;
}
if self.is_light(x as i32 + 1, y as i32) {
idx += 0x008;
}
if self.is_light(x as i32 - 1, y as i32 + 1) {
idx += 0x004;
}
if self.is_light(x as i32, y as i32 + 1) {
idx += 0x002;
}
if self.is_light(x as i32 + 1, y as i32 + 1) {
idx += 0x001;
}
self.algorithm[idx]
})
.collect()
})
.collect();
let background = if self.algorithm[0] == '#' {
!self.background
} else {
false
};
Image {
image,
algorithm: self.algorithm,
background,
}
}
fn light_pixels(self) -> usize {
let result = self.enhance().enhance();
result
.image
.iter()
.map(|row| row.iter().filter(|p| **p == '#').count())
.sum()
}
fn super_light_pixels(self) -> usize {
let mut image = self;
for _ in 0..50 {
image = image.enhance();
}
image
.image
.iter()
.map(|row| row.iter().filter(|p| **p == '#').count())
.sum()
}
}
impl FromStr for Image {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (algorithm, image) = s.split_once("\n\n").unwrap();
let algorithm = algorithm.chars().collect();
let image = image
.lines()
.map(|line| line.trim_end().chars().collect())
.collect();
Ok(Image {
image,
algorithm,
background: false,
})
}
}
impl fmt::Debug for Image {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(
f,
"algorithm: {}\n",
self.algorithm.iter().collect::<String>()
)?;
for row in self.image.iter() {
writeln!(f, "{}", row.iter().collect::<String>())?;
}
Ok(())
}
}
fn main() {
const INPUT: &str = include_str!("../inputs/day20.txt");
let image: Image = INPUT.parse().unwrap();
println!("solution {}", image.clone().light_pixels());
println!("solution {}", image.super_light_pixels());
}
#[test]
fn test_light_pixels() {
const INPUT: &str = "..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..###..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#..#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#......#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#.....####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.......##..####..#...#.#.#...##..#.#..###..#####........#..####......#..#
#..#.
#....
##..#
..#..
..###";
let image: Image = INPUT.parse().unwrap();
assert_eq!(image.light_pixels(), 35);
}
#[test]
fn test_super_light_pixels() {
const INPUT: &str = "..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..###..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#..#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#......#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#.....####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.......##..####..#...#.#.#...##..#.#..###..#####........#..####......#..#
#..#.
#....
##..#
..#..
..###";
let image: Image = INPUT.parse().unwrap();
assert_eq!(image.super_light_pixels(), 3351);
}