1
0
Fork 0

Compare commits

...

2 Commits

Author SHA1 Message Date
Andrew Coleman 95003d0b0a day14 solution 2023-12-24 16:49:31 -05:00
Andrew Coleman 765e0d90cf remove println 2023-12-24 16:49:25 -05:00
3 changed files with 217 additions and 23 deletions

View File

@ -113,7 +113,6 @@ impl Map {
matches.push(m); matches.push(m);
} }
println!("matches {:?}", matches);
let mut res: Vec<Option<Pair>> = vec![]; let mut res: Vec<Option<Pair>> = vec![];
for &(r1, r2) in matches.iter() { for &(r1, r2) in matches.iter() {
let mut rows_matched = 0; let mut rows_matched = 0;
@ -121,13 +120,6 @@ impl Map {
loop { loop {
let start = r1 - rows_matched; let start = r1 - rows_matched;
let end = r2 + rows_matched; let end = r2 + rows_matched;
println!(
"comparing {} to {} with {} and {}",
start,
end,
board[start].iter().collect::<String>(),
board[end].iter().collect::<String>(),
);
if board[start] == board[end] { if board[start] == board[end] {
rows_matched += 1; rows_matched += 1;
} else { } else {
@ -145,10 +137,6 @@ impl Map {
break; break;
} }
} }
println!(
"{},{} rows matched {} diff_chars {}",
r1, r2, rows_matched, diff_chars
);
if diff_chars == 1 { if diff_chars == 1 {
res.push(Some((r1, r2))); res.push(Some((r1, r2)));
@ -165,29 +153,20 @@ impl Map {
} }
fn get_horiz_smudge(&self) -> Option<usize> { fn get_horiz_smudge(&self) -> Option<usize> {
println!("get horiz smudge"); if let Some((_r1, r2)) = self.find_smudge(&self.rows) {
if let Some((r1, r2)) = self.find_smudge(&self.rows) {
println!("got horiz smudge rows of {},{}", r1, r2);
return Some(100 * r2); return Some(100 * r2);
} }
None None
} }
fn get_vert_smudge(&self) -> Option<usize> { fn get_vert_smudge(&self) -> Option<usize> {
println!("get vert smudge"); if let Some((_c1, c2)) = self.find_smudge(&self.cols) {
if let Some((c1, c2)) = self.find_smudge(&self.cols) {
println!("got vert smudge cols of {},{}", c1, c2);
return Some(c2); return Some(c2);
} }
None None
} }
fn part2(&self) -> usize { fn part2(&self) -> usize {
println!();
for line in self.rows.iter() {
println!("{}", line.iter().collect::<String>());
}
println!("width {} height {}", self.width, self.height);
let hscore = self.get_horiz_smudge(); let hscore = self.get_horiz_smudge();
let vscore = self.get_vert_smudge(); let vscore = self.get_vert_smudge();
if hscore.is_some() && vscore.is_some() { if hscore.is_some() && vscore.is_some() {

214
2023/src/day14.rs Normal file
View File

@ -0,0 +1,214 @@
use aoc_runner_derive::{aoc, aoc_generator};
use std::collections::HashSet;
#[aoc_generator(day14)]
fn parse(input: &str) -> Vec<String> {
input
.lines()
.filter_map(|line| {
if !line.is_empty() {
Some(String::from(line))
} else {
None
}
})
.collect::<Vec<_>>()
}
fn map_to_string(map: &[[char; 100]; 100]) -> String {
let mut s = String::with_capacity(100 * 100);
for row in map.iter() {
let mut has_char = false;
for &c in row.iter() {
if c == ' ' {
break;
}
has_char = true;
s.push(c);
}
if has_char {
s.push('\n');
}
}
s
}
fn score(input: &str) -> usize {
let lines = input
.split("\n")
.filter_map(|t| {
if t.is_empty() {
None
} else {
Some(String::from(t))
}
})
.collect::<Vec<String>>();
let mut score = 0;
let total_lines = lines.len();
for (index, line) in lines.iter().enumerate() {
score += line.matches("O").collect::<Vec<_>>().len() * (total_lines - index);
}
score
}
#[aoc(day14, part1)]
fn part1(input: &[String]) -> usize {
let mut map: [[char; 100]; 100] = [[' '; 100]; 100];
for (row, line) in input.iter().enumerate() {
for (col, c) in line.chars().enumerate() {
map[row][col] = c;
}
}
tilt_north(&mut map);
score(&map_to_string(&map))
}
fn tilt_north(map: &mut [[char; 100]; 100]) -> String {
for col in 0..(map[0]).len() {
let mut r = 0;
for row in (0..(map.len())).rev() {
if map[row][col] == ' ' {
continue;
} else if map[row][col] == '#' {
for rock in 1..=r {
map[row + rock][col] = 'O';
}
r = 0;
} else if map[row][col] == 'O' {
r += 1;
map[row][col] = '.';
}
}
for rock in 1..=r {
map[rock - 1][col] = 'O';
}
}
map_to_string(&map)
}
fn tilt_west(map: &mut [[char; 100]; 100]) -> String {
for row in 0..map.len() {
let mut r = 0;
for col in (0..map[0].len()).rev() {
if map[row][col] == ' ' {
continue;
} else if map[row][col] == '#' {
for rock in 1..=r {
map[row][col + rock] = 'O';
}
r = 0;
} else if map[row][col] == 'O' {
r += 1;
map[row][col] = '.';
}
}
for rock in 1..=r {
map[row][rock - 1] = 'O';
}
}
map_to_string(&map)
}
fn tilt_south(map: &mut [[char; 100]; 100]) -> String {
for col in 0..map[0].len() {
let mut r = 0;
let mut last_row = 100;
for row in 0..map.len() {
if map[row][col] == ' ' {
last_row = row;
break;
} else if map[row][col] == '#' {
for rock in 1..=r {
map[row - rock][col] = 'O';
}
r = 0;
} else if map[row][col] == 'O' {
r += 1;
map[row][col] = '.';
}
}
for rock in 1..=r {
map[last_row - rock][col] = 'O';
}
}
map_to_string(&map)
}
fn tilt_east(map: &mut [[char; 100]; 100]) -> String {
for row in 0..map.len() {
let mut r = 0;
let mut last_col = 100;
for col in 0..map[row].len() {
if map[row][col] == ' ' {
last_col = col;
break;
} else if map[row][col] == '#' {
for rock in 1..=r {
map[row][col - rock] = 'O';
}
r = 0;
} else if map[row][col] == 'O' {
r += 1;
map[row][col] = '.';
}
}
for rock in 1..=r {
map[row][last_col - rock] = 'O';
}
}
map_to_string(&map)
}
#[aoc(day14, part2)]
fn part2(input: &[String]) -> usize {
let mut map: [[char; 100]; 100] = [[' '; 100]; 100];
for (row, line) in input.iter().enumerate() {
for (col, c) in line.chars().enumerate() {
map[row][col] = c;
}
}
let mut patterns: HashSet<String> = HashSet::new();
let mut seq: Vec<String> = vec![];
let mut iters = 0;
loop {
if iters == 1000 {
break;
}
tilt_north(&mut map);
tilt_west(&mut map);
tilt_south(&mut map);
tilt_east(&mut map);
let s = map_to_string(&map);
iters += 1;
seq.push(s.clone());
patterns.insert(s);
}
let score = score(&seq[999]);
score
}
#[cfg(test)]
mod tests {
use super::*;
const SAMPLE_DATA: &'static str = r#"O....#....
O.OO#....#
.....##...
OO.#O....O
.O.....O#.
O.#..O.#.#
..O..#O..O
.......O..
#....###..
#OO..#....
"#;
#[test]
fn sample_data() {
let input = parse(&SAMPLE_DATA);
assert_eq!(part1(&input), 136);
assert_eq!(part2(&input), 64);
}
}

View File

@ -13,5 +13,6 @@ mod day10;
mod day11; mod day11;
mod day12; mod day12;
mod day13; mod day13;
mod day14;
aoc_lib! { year = 2023 } aoc_lib! { year = 2023 }