This commit is contained in:
Drew Galbraith 2023-12-13 00:25:30 -08:00
parent 1bc006c82e
commit c19ee00d71
3 changed files with 1427 additions and 0 deletions

1319
input/day13.txt Normal file

File diff suppressed because it is too large Load Diff

105
src/day13.rs Normal file
View File

@ -0,0 +1,105 @@
use std::io;
fn try_vertical(rows: &Vec<Vec<char>>, c: usize, smudge: bool) -> bool {
let mut left = c - 1;
let mut right = c;
let mut one_err = false;
loop {
for row in rows {
if row[left] != row[right] {
if smudge && !one_err {
one_err = true;
continue;
}
return false;
}
}
if left == 0 || right == rows[0].len() - 1 {
if smudge {
return one_err;
}
return true;
}
left -= 1;
right += 1;
}
}
fn find_vertical(rows: &Vec<Vec<char>>, smudge: bool) -> Option<usize> {
for i in 1..rows[0].len() {
if try_vertical(rows, i, smudge) {
return Some(i);
}
}
None
}
fn try_horizontal(rows: &Vec<Vec<char>>, r: usize, smudge: bool) -> bool {
let mut top = r - 1;
let mut bottom = r;
let mut one_err = false;
loop {
for i in 0..rows[0].len() {
if rows[top][i] != rows[bottom][i] {
if smudge && !one_err {
one_err = true;
continue;
}
return false;
}
}
if top == 0 || bottom == rows.len() - 1 {
if smudge {
return one_err;
}
return true;
}
top -= 1;
bottom += 1;
}
}
fn find_horizontal(rows: &Vec<Vec<char>>, smudge: bool) -> Option<usize> {
for i in 1..rows.len() {
if try_horizontal(rows, i, smudge) {
return Some(i);
}
}
None
}
pub fn summarize(smudge: bool) -> usize {
let mut buffer = String::new();
while io::stdin().read_line(&mut buffer).unwrap() > 0 {}
let valleys = buffer.split("\n\n");
let mut sum = 0;
for valley in valleys {
let rowsplit = valley.split("\n");
let mut rows = Vec::new();
for row in rowsplit {
if row.len() > 0 {
rows.push(row.chars().collect());
}
}
match find_vertical(&rows, smudge) {
Some(x) => {
sum += x;
continue;
}
_ => {}
}
match find_horizontal(&rows, smudge) {
Some(x) => {
sum += 100 * x;
}
_ => panic!("No match mound {:?}", rows),
}
}
sum
}

View File

@ -4,6 +4,7 @@ mod day1;
mod day10;
mod day11;
mod day12;
mod day13;
mod day2;
mod day3;
mod day4;
@ -44,6 +45,8 @@ fn main() {
"day11b" => println!("Sum: {}", day11::sum_lengths(999999)),
"day12a" => println!("Sum: {}", day12::sum_arrangements(false)),
"day12b" => println!("Sum: {}", day12::sum_arrangements(true)),
"day13a" => println!("Summary: {}", day13::summarize(false)),
"day13b" => println!("Summary: {}", day13::summarize(true)),
_ => println!("Unrecognized day: {}", args[1]),
}
}