generated from janezicmatej/aoc-template
Compare commits
No commits in common. "000002707d1b5ee7eb3c66f2394729dc1cb4161a" and "00000240166c41e14859994af149c99dda5cce3b" have entirely different histories.
000002707d
...
0000024016
|
@ -1,15 +0,0 @@
|
|||
#.##..##.
|
||||
..#.##.#.
|
||||
##......#
|
||||
##......#
|
||||
..#.##.#.
|
||||
..##..##.
|
||||
#.#.##.#.
|
||||
|
||||
#...##..#
|
||||
#....#..#
|
||||
..##..###
|
||||
#####.##.
|
||||
#####.##.
|
||||
..##..###
|
||||
#....#..#
|
|
@ -78,13 +78,10 @@ mod tests {
|
|||
use super::*;
|
||||
#[test]
|
||||
fn test_part_one() {
|
||||
assert_eq!(
|
||||
part_one(&aoc::template::read_file("examples", 9)),
|
||||
Some(114)
|
||||
);
|
||||
assert_eq!(part_one(&aoc::template::read_file("examples", 9)), None);
|
||||
}
|
||||
#[test]
|
||||
fn test_part_two() {
|
||||
assert_eq!(part_two(&aoc::template::read_file("examples", 9)), Some(2));
|
||||
assert_eq!(part_two(&aoc::template::read_file("examples", 9)), None);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,16 +2,20 @@ use std::collections::HashSet;
|
|||
|
||||
fn parse_input(input: &str) -> (Vec<(usize, usize)>, HashSet<usize>, HashSet<usize>) {
|
||||
let mut galaxies = Vec::new();
|
||||
let mut rows = HashSet::from_iter(0..input.lines().count());
|
||||
let mut columns = HashSet::from_iter(0..input.lines().next().unwrap().len());
|
||||
let mut rows = HashSet::new();
|
||||
let mut columns = HashSet::from_iter(1..(input.lines().next().unwrap().len()));
|
||||
|
||||
for (y, line) in input.lines().enumerate() {
|
||||
let mut found = false;
|
||||
for (x, c) in line.chars().enumerate() {
|
||||
if c == '#' {
|
||||
galaxies.push((y, x));
|
||||
rows.remove(&y);
|
||||
found = true;
|
||||
}
|
||||
columns.remove(&x);
|
||||
}
|
||||
if !found {
|
||||
rows.insert(y);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,66 +0,0 @@
|
|||
fn mirror_h(shape: &Vec<Vec<char>>, smudges: usize) -> Option<usize> {
|
||||
(1..shape.len())
|
||||
.filter(|&i| {
|
||||
shape
|
||||
.iter()
|
||||
.skip(i)
|
||||
.zip(shape.iter().take(i).rev())
|
||||
.map(|(x, y)| {
|
||||
x.iter()
|
||||
.zip(y.iter())
|
||||
.map(|(xx, yy)| (xx != yy) as usize)
|
||||
.sum::<usize>()
|
||||
})
|
||||
.sum::<usize>()
|
||||
== smudges
|
||||
})
|
||||
.max()
|
||||
}
|
||||
fn mirror_v(shape: &Vec<Vec<char>>, smudges: usize) -> Option<usize> {
|
||||
let shape = (0..shape[0].len())
|
||||
.map(|col| (0..shape.len()).map(|row| shape[row][col]).collect())
|
||||
.collect();
|
||||
|
||||
mirror_h(&shape, smudges)
|
||||
}
|
||||
|
||||
fn solve(input: &str, smudges: usize) -> usize {
|
||||
input
|
||||
.split("\n\n")
|
||||
.map(|x| x.lines().map(|line| line.chars().collect()).collect())
|
||||
.map(|shape| {
|
||||
let v = mirror_v(&shape, smudges).unwrap_or_default();
|
||||
let h = mirror_h(&shape, smudges).unwrap_or_default();
|
||||
v + h * 100
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
pub fn part_one(input: &str) -> Option<usize> {
|
||||
Some(solve(input, 0))
|
||||
}
|
||||
|
||||
pub fn part_two(input: &str) -> Option<usize> {
|
||||
Some(solve(input, 1))
|
||||
}
|
||||
|
||||
aoc::solution!(13);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn test_part_one() {
|
||||
assert_eq!(
|
||||
part_one(&aoc::template::read_file("examples", 13)),
|
||||
Some(405)
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn test_part_two() {
|
||||
assert_eq!(
|
||||
part_two(&aoc::template::read_file("examples", 13)),
|
||||
Some(400)
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue