Print all days
This commit is contained in:
parent
3027ffbdd7
commit
c2e2d2930a
3 changed files with 100 additions and 34 deletions
|
@ -19,7 +19,6 @@ pub fn solve() {
|
|||
let result: usize = patterns
|
||||
.iter()
|
||||
.map(|block| {
|
||||
|
||||
// check columns
|
||||
let mut ks: Vec<usize> = vec![];
|
||||
for k in 1..block[0].len() {
|
||||
|
@ -134,7 +133,7 @@ pub fn solve() {
|
|||
if !ks.is_empty() {
|
||||
return ks[ks.len() / 2] * 100;
|
||||
}
|
||||
|
||||
|
||||
0
|
||||
})
|
||||
.sum();
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use std::{fs, collections::HashMap};
|
||||
use std::{collections::HashMap, fs};
|
||||
|
||||
pub fn solve() {
|
||||
let path = "res/14/input";
|
||||
|
@ -6,7 +6,8 @@ pub fn solve() {
|
|||
let contents = fs::read_to_string(path).expect("I/O error, wrong path?");
|
||||
//let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?"));
|
||||
|
||||
let rocks_init: Vec<Vec<char>> = contents.lines()
|
||||
let rocks_init: Vec<Vec<char>> = contents
|
||||
.lines()
|
||||
.map(|line| line.chars().collect::<Vec<_>>())
|
||||
.collect();
|
||||
|
||||
|
@ -18,7 +19,7 @@ pub fn solve() {
|
|||
let mut new_x = x;
|
||||
while new_x > 0 {
|
||||
new_x -= 1;
|
||||
if rocks[new_x][y] == '#' || rocks[new_x][y] == 'O' {
|
||||
if rocks[new_x][y] == '#' || rocks[new_x][y] == 'O' {
|
||||
new_x += 1;
|
||||
break;
|
||||
}
|
||||
|
@ -32,7 +33,6 @@ pub fn solve() {
|
|||
|
||||
println!("Result 1: {result}");
|
||||
|
||||
|
||||
let mut counter = 1;
|
||||
let mut cache: HashMap<Vec<Vec<char>>, usize> = HashMap::new();
|
||||
let mut cache_used = false;
|
||||
|
@ -42,23 +42,25 @@ pub fn solve() {
|
|||
for y in 0..rocks[0].len() {
|
||||
if rocks[x][y] == 'O' {
|
||||
match counter % 4 {
|
||||
0 => { // north
|
||||
0 => {
|
||||
// north
|
||||
let mut new_x = x;
|
||||
while new_x > 0 {
|
||||
new_x -= 1;
|
||||
if rocks[new_x][y] == '#' || rocks[new_x][y] == 'O' {
|
||||
if rocks[new_x][y] == '#' || rocks[new_x][y] == 'O' {
|
||||
new_x += 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
rocks[x][y] = '.';
|
||||
rocks[new_x][y] = 'O';
|
||||
},
|
||||
_ => { // west
|
||||
}
|
||||
_ => {
|
||||
// west
|
||||
let mut new_y = y;
|
||||
while new_y > 0 {
|
||||
new_y -= 1;
|
||||
if rocks[x][new_y] == '#' || rocks[x][new_y] == 'O' {
|
||||
if rocks[x][new_y] == '#' || rocks[x][new_y] == 'O' {
|
||||
new_y += 1;
|
||||
break;
|
||||
}
|
||||
|
@ -67,7 +69,6 @@ pub fn solve() {
|
|||
rocks[x][new_y] = 'O';
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -76,23 +77,25 @@ pub fn solve() {
|
|||
for y in (0..rocks[0].len()).rev() {
|
||||
if rocks[x][y] == 'O' {
|
||||
match counter % 4 {
|
||||
3 => { // east
|
||||
3 => {
|
||||
// east
|
||||
let mut new_y = y;
|
||||
while new_y < rocks[0].len()-1 {
|
||||
while new_y < rocks[0].len() - 1 {
|
||||
new_y += 1;
|
||||
if rocks[x][new_y] == '#' || rocks[x][new_y] == 'O' {
|
||||
if rocks[x][new_y] == '#' || rocks[x][new_y] == 'O' {
|
||||
new_y -= 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
rocks[x][y] = '.';
|
||||
rocks[x][new_y] = 'O';
|
||||
},
|
||||
_ => { // south
|
||||
}
|
||||
_ => {
|
||||
// south
|
||||
let mut new_x = x;
|
||||
while new_x < rocks.len()-1 {
|
||||
while new_x < rocks.len() - 1 {
|
||||
new_x += 1;
|
||||
if rocks[new_x][y] == '#' || rocks[new_x][y] == 'O' {
|
||||
if rocks[new_x][y] == '#' || rocks[new_x][y] == 'O' {
|
||||
new_x -= 1;
|
||||
break;
|
||||
}
|
||||
|
@ -101,33 +104,32 @@ pub fn solve() {
|
|||
rocks[new_x][y] = 'O';
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
counter += 1;
|
||||
|
||||
if counter % 4 == 0 && !cache_used{
|
||||
if counter % 4 == 0 && !cache_used {
|
||||
let cycle_start = cache.get(&rocks);
|
||||
match cycle_start {
|
||||
None => { cache.insert(rocks.clone(), counter); },
|
||||
Some(x) => {
|
||||
None => {
|
||||
cache.insert(rocks.clone(), counter);
|
||||
}
|
||||
Some(x) => {
|
||||
let cycle = counter - x;
|
||||
counter += (((4000000000-counter)/cycle) as usize) * cycle;
|
||||
counter += (((4000000000 - counter) / cycle) as usize) * cycle;
|
||||
cache_used = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result = rocks.iter().enumerate()
|
||||
.map(|(i_row, row)| {
|
||||
row.iter().filter(|c| **c == 'O').count() * (rocks.len() - i_row)
|
||||
})
|
||||
result = rocks
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i_row, row)| row.iter().filter(|c| **c == 'O').count() * (rocks.len() - i_row))
|
||||
.sum();
|
||||
|
||||
println!("Result 2: {result}");
|
||||
|
|
73
src/main.rs
73
src/main.rs
|
@ -1,12 +1,77 @@
|
|||
pub mod days;
|
||||
#[macro_use] extern crate scan_fmt;
|
||||
#[macro_use]
|
||||
extern crate scan_fmt;
|
||||
|
||||
use std::time::Instant;
|
||||
|
||||
fn main() {
|
||||
days::d14::solve()
|
||||
//_all_days()
|
||||
//days::d14::solve()
|
||||
_all_days()
|
||||
}
|
||||
|
||||
fn _all_days() {
|
||||
println!("Day 1");
|
||||
let start = Instant::now();
|
||||
println!("\nDay 1");
|
||||
days::d01::solve();
|
||||
print_elapsed(&start);
|
||||
|
||||
println!("\nDay 2");
|
||||
days::d02::solve();
|
||||
print_elapsed(&start);
|
||||
|
||||
println!("\nDay 3");
|
||||
days::d03::solve();
|
||||
print_elapsed(&start);
|
||||
|
||||
println!("\nDay 4");
|
||||
days::d04::solve();
|
||||
print_elapsed(&start);
|
||||
|
||||
println!("\nDay 5");
|
||||
days::d05::solve();
|
||||
print_elapsed(&start);
|
||||
|
||||
println!("\nDay 6");
|
||||
days::d06::solve();
|
||||
print_elapsed(&start);
|
||||
|
||||
println!("\nDay 7");
|
||||
days::d07::solve();
|
||||
print_elapsed(&start);
|
||||
|
||||
println!("\nDay 8");
|
||||
days::d08::solve();
|
||||
print_elapsed(&start);
|
||||
|
||||
println!("\nDay 9");
|
||||
days::d09::solve();
|
||||
print_elapsed(&start);
|
||||
|
||||
println!("\nDay 10");
|
||||
days::d10::solve();
|
||||
print_elapsed(&start);
|
||||
|
||||
println!("\nDay 11");
|
||||
days::d11::solve();
|
||||
print_elapsed(&start);
|
||||
|
||||
println!("\nDay 12");
|
||||
days::d12::solve();
|
||||
print_elapsed(&start);
|
||||
|
||||
println!("\nDay 13");
|
||||
days::d13::solve();
|
||||
print_elapsed(&start);
|
||||
|
||||
println!("\nDay 14");
|
||||
days::d14::solve();
|
||||
print_elapsed(&start);
|
||||
}
|
||||
|
||||
fn print_elapsed(start: &Instant) {
|
||||
println!(
|
||||
" Elapsed: {}.{}ms",
|
||||
start.elapsed().as_millis(),
|
||||
start.elapsed().as_micros() % 1000
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue