Day 13 complete
This commit is contained in:
parent
31cdd8b327
commit
b8f91dee00
107
src/13part1.rs
Normal file
107
src/13part1.rs
Normal file
|
@ -0,0 +1,107 @@
|
|||
use std::fs;
|
||||
|
||||
fn main() {
|
||||
let file_path = "input/13input.txt";
|
||||
let contents = fs::read_to_string(file_path).expect("should read file");
|
||||
let mut lines = contents.lines();
|
||||
let mut matrices: Vec<Vec<Vec<char>>> = vec![Vec::new()];
|
||||
loop {
|
||||
let line = lines.next();
|
||||
if line.is_none() {
|
||||
break;
|
||||
}
|
||||
if line.unwrap() == "" {
|
||||
matrices.push(Vec::new());
|
||||
continue;
|
||||
}
|
||||
let matrices_len = matrices.len();
|
||||
matrices[matrices_len-1].push(line.unwrap().chars().collect());
|
||||
}
|
||||
let mut total = 0;
|
||||
for i in 0..matrices.len() {
|
||||
let mut mirrored_lower = false;
|
||||
let mut mirrored_upper = false;
|
||||
let mut center_lower = matrices[i].len()/2;
|
||||
let mut center_upper = matrices[i].len()/2+1;
|
||||
while center_lower > 0 && center_upper < matrices[i].len() {
|
||||
mirrored_lower = true;
|
||||
mirrored_upper = true;
|
||||
let mut first_pointer_low = center_lower-1;
|
||||
let mut second_pointer_low = center_lower;
|
||||
while second_pointer_low < matrices[i].len() && mirrored_lower {
|
||||
mirrored_lower &= matrices[i][first_pointer_low] == matrices[i][second_pointer_low];
|
||||
if first_pointer_low > 0 {
|
||||
first_pointer_low -= 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
second_pointer_low += 1;
|
||||
}
|
||||
if mirrored_lower {
|
||||
total += 100*center_lower;
|
||||
break;
|
||||
}
|
||||
let mut first_pointer_up = center_upper-1;
|
||||
let mut second_pointer_up = center_upper;
|
||||
while second_pointer_up < matrices[i].len() && mirrored_upper {
|
||||
mirrored_upper &= matrices[i][first_pointer_up] == matrices[i][second_pointer_up];
|
||||
if first_pointer_up > 0 {
|
||||
first_pointer_up -= 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
second_pointer_up += 1;
|
||||
}
|
||||
if mirrored_upper {
|
||||
total += 100*center_upper;
|
||||
break;
|
||||
}
|
||||
center_lower -= 1;
|
||||
center_upper += 1;
|
||||
}
|
||||
if mirrored_upper || mirrored_lower {
|
||||
continue;
|
||||
}
|
||||
center_lower = matrices[i][0].len()/2;
|
||||
center_upper = matrices[i][0].len()/2+1;
|
||||
while center_lower > 0 && center_upper < matrices[i][0].len() {
|
||||
mirrored_lower = true;
|
||||
mirrored_upper = true;
|
||||
let mut first_pointer_low = center_lower-1;
|
||||
let mut second_pointer_low = center_lower;
|
||||
while second_pointer_low < matrices[i][0].len() && mirrored_lower {
|
||||
for j in 0..matrices[i].len() {
|
||||
mirrored_lower &= matrices[i][j][first_pointer_low] == matrices[i][j][second_pointer_low];
|
||||
}
|
||||
if first_pointer_low == 0 {
|
||||
break;
|
||||
}
|
||||
first_pointer_low -= 1;
|
||||
second_pointer_low += 1;
|
||||
}
|
||||
if mirrored_lower {
|
||||
total += center_lower;
|
||||
break;
|
||||
}
|
||||
let mut first_pointer_up = center_upper-1;
|
||||
let mut second_pointer_up = center_upper;
|
||||
while second_pointer_up < matrices[i][0].len() && mirrored_upper {
|
||||
for j in 0..matrices[i].len() {
|
||||
mirrored_upper &= matrices[i][j][first_pointer_up] == matrices[i][j][second_pointer_up];
|
||||
}
|
||||
if first_pointer_up == 0 {
|
||||
break;
|
||||
}
|
||||
first_pointer_up -= 1;
|
||||
second_pointer_up += 1;
|
||||
}
|
||||
if mirrored_upper {
|
||||
total += center_upper;
|
||||
break;
|
||||
}
|
||||
center_lower -= 1;
|
||||
center_upper += 1;
|
||||
}
|
||||
}
|
||||
println!("{}", total);
|
||||
}
|
121
src/13part2.rs
Normal file
121
src/13part2.rs
Normal file
|
@ -0,0 +1,121 @@
|
|||
use std::fs;
|
||||
|
||||
fn main() {
|
||||
let file_path = "input/13input.txt";
|
||||
let contents = fs::read_to_string(file_path).expect("should read file");
|
||||
let mut lines = contents.lines();
|
||||
let mut matrices: Vec<Vec<Vec<char>>> = vec![Vec::new()];
|
||||
loop {
|
||||
let line = lines.next();
|
||||
if line.is_none() {
|
||||
break;
|
||||
}
|
||||
if line.unwrap() == "" {
|
||||
matrices.push(Vec::new());
|
||||
continue;
|
||||
}
|
||||
let matrices_len = matrices.len();
|
||||
matrices[matrices_len-1].push(line.unwrap().chars().collect());
|
||||
}
|
||||
let mut total = 0;
|
||||
for i in 0..matrices.len() {
|
||||
let mut center_lower = matrices[i].len()/2;
|
||||
let mut center_upper = matrices[i].len()/2+1;
|
||||
let mut smudge_count = 0;
|
||||
while center_lower > 0 && center_upper < matrices[i].len() {
|
||||
smudge_count = 0;
|
||||
let mut first_pointer_low = center_lower-1;
|
||||
let mut second_pointer_low = center_lower;
|
||||
while second_pointer_low < matrices[i].len() && smudge_count < 2 {
|
||||
if matrices[i][first_pointer_low] != matrices[i][second_pointer_low] {
|
||||
for j in 0..matrices[i][first_pointer_low].len() {
|
||||
if matrices[i][first_pointer_low][j] != matrices[i][second_pointer_low][j] {
|
||||
smudge_count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if first_pointer_low == 0 {
|
||||
break;
|
||||
}
|
||||
first_pointer_low -= 1;
|
||||
second_pointer_low += 1;
|
||||
}
|
||||
if smudge_count == 1 {
|
||||
total += 100*center_lower;
|
||||
break;
|
||||
}
|
||||
smudge_count = 0;
|
||||
let mut first_pointer_up = center_upper-1;
|
||||
let mut second_pointer_up = center_upper;
|
||||
while second_pointer_up < matrices[i].len() && smudge_count < 2 {
|
||||
if matrices[i][first_pointer_up] != matrices[i][second_pointer_up] {
|
||||
for j in 0..matrices[i][first_pointer_up].len() {
|
||||
if matrices[i][first_pointer_up][j] != matrices[i][second_pointer_up][j] {
|
||||
smudge_count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if first_pointer_up == 0 {
|
||||
break;
|
||||
|
||||
}
|
||||
first_pointer_up -= 1;
|
||||
second_pointer_up += 1;
|
||||
}
|
||||
if smudge_count == 1 {
|
||||
total += 100*center_upper;
|
||||
break;
|
||||
}
|
||||
center_lower -= 1;
|
||||
center_upper += 1;
|
||||
}
|
||||
if smudge_count == 1 {
|
||||
continue;
|
||||
}
|
||||
center_lower = matrices[i][0].len()/2;
|
||||
center_upper = matrices[i][0].len()/2+1;
|
||||
while center_lower > 0 && center_upper < matrices[i][0].len() {
|
||||
smudge_count = 0;
|
||||
let mut first_pointer_low = center_lower-1;
|
||||
let mut second_pointer_low = center_lower;
|
||||
while second_pointer_low < matrices[i][0].len() && smudge_count < 2 {
|
||||
for j in 0..matrices[i].len() {
|
||||
if matrices[i][j][first_pointer_low] != matrices[i][j][second_pointer_low] {
|
||||
smudge_count += 1;
|
||||
}
|
||||
}
|
||||
if first_pointer_low == 0 {
|
||||
break;
|
||||
}
|
||||
first_pointer_low -= 1;
|
||||
second_pointer_low += 1;
|
||||
}
|
||||
if smudge_count == 1 {
|
||||
total += center_lower;
|
||||
break;
|
||||
}
|
||||
smudge_count = 0;
|
||||
let mut first_pointer_up = center_upper-1;
|
||||
let mut second_pointer_up = center_upper;
|
||||
while second_pointer_up < matrices[i][0].len() && smudge_count < 2 {
|
||||
for j in 0..matrices[i].len() {
|
||||
if matrices[i][j][first_pointer_up] != matrices[i][j][second_pointer_up] {
|
||||
smudge_count += 1;
|
||||
}
|
||||
}
|
||||
if first_pointer_up == 0 {
|
||||
break;
|
||||
}
|
||||
first_pointer_up -= 1;
|
||||
second_pointer_up += 1;
|
||||
}
|
||||
if smudge_count == 1 {
|
||||
total += center_upper;
|
||||
break;
|
||||
}
|
||||
center_lower -= 1;
|
||||
center_upper += 1;
|
||||
}
|
||||
}
|
||||
println!("{}", total);
|
||||
}
|
Loading…
Reference in New Issue
Block a user