Compare commits
	
		
			No commits in common. "5000ae73445f4f101a1e718d55e7d9c6f83cb0d9" and "3027ffbdd7e002929a860b67110e085162a0f521" have entirely different histories.
		
	
	
		
			5000ae7344
			...
			3027ffbdd7
		
	
		
					 6 changed files with 33 additions and 164 deletions
				
			
		| 
						 | 
					@ -1 +0,0 @@
 | 
				
			||||||
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7
 | 
					 | 
				
			||||||
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							| 
						 | 
					@ -19,6 +19,7 @@ pub fn solve() {
 | 
				
			||||||
    let result: usize = patterns
 | 
					    let result: usize = patterns
 | 
				
			||||||
        .iter()
 | 
					        .iter()
 | 
				
			||||||
        .map(|block| {
 | 
					        .map(|block| {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // check columns
 | 
					            // check columns
 | 
				
			||||||
            let mut ks: Vec<usize> = vec![];
 | 
					            let mut ks: Vec<usize> = vec![];
 | 
				
			||||||
            for k in 1..block[0].len() {
 | 
					            for k in 1..block[0].len() {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,4 +1,4 @@
 | 
				
			||||||
use std::{collections::HashMap, fs};
 | 
					use std::{fs, collections::HashMap};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn solve() {
 | 
					pub fn solve() {
 | 
				
			||||||
    let path = "res/14/input";
 | 
					    let path = "res/14/input";
 | 
				
			||||||
| 
						 | 
					@ -6,8 +6,7 @@ pub fn solve() {
 | 
				
			||||||
    let contents = fs::read_to_string(path).expect("I/O error, wrong path?");
 | 
					    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 contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let rocks_init: Vec<Vec<char>> = contents
 | 
					    let rocks_init: Vec<Vec<char>> = contents.lines()
 | 
				
			||||||
        .lines()
 | 
					 | 
				
			||||||
        .map(|line| line.chars().collect::<Vec<_>>())
 | 
					        .map(|line| line.chars().collect::<Vec<_>>())
 | 
				
			||||||
        .collect();
 | 
					        .collect();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -19,7 +18,7 @@ pub fn solve() {
 | 
				
			||||||
                let mut new_x = x;
 | 
					                let mut new_x = x;
 | 
				
			||||||
                while new_x > 0 {
 | 
					                while new_x > 0 {
 | 
				
			||||||
                    new_x -= 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;
 | 
					                        new_x += 1;
 | 
				
			||||||
                        break;
 | 
					                        break;
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
| 
						 | 
					@ -33,6 +32,7 @@ pub fn solve() {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    println!("Result 1: {result}");
 | 
					    println!("Result 1: {result}");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let mut counter = 1;
 | 
					    let mut counter = 1;
 | 
				
			||||||
    let mut cache: HashMap<Vec<Vec<char>>, usize> = HashMap::new();
 | 
					    let mut cache: HashMap<Vec<Vec<char>>, usize> = HashMap::new();
 | 
				
			||||||
    let mut cache_used = false;
 | 
					    let mut cache_used = false;
 | 
				
			||||||
| 
						 | 
					@ -42,25 +42,23 @@ pub fn solve() {
 | 
				
			||||||
                for y in 0..rocks[0].len() {
 | 
					                for y in 0..rocks[0].len() {
 | 
				
			||||||
                    if rocks[x][y] == 'O' {
 | 
					                    if rocks[x][y] == 'O' {
 | 
				
			||||||
                        match counter % 4 {
 | 
					                        match counter % 4 {
 | 
				
			||||||
                            0 => {
 | 
					                            0 => { // north
 | 
				
			||||||
                                // north
 | 
					 | 
				
			||||||
                                let mut new_x = x;
 | 
					                                let mut new_x = x;
 | 
				
			||||||
                                while new_x > 0 {
 | 
					                                while new_x > 0 {
 | 
				
			||||||
                                    new_x -= 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;
 | 
					                                        new_x += 1;
 | 
				
			||||||
                                        break;
 | 
					                                        break;
 | 
				
			||||||
                                    }
 | 
					                                    }
 | 
				
			||||||
                                }
 | 
					                                }
 | 
				
			||||||
                                rocks[x][y] = '.';
 | 
					                                rocks[x][y] = '.';
 | 
				
			||||||
                                rocks[new_x][y] = 'O';
 | 
					                                rocks[new_x][y] = 'O';
 | 
				
			||||||
                            }
 | 
					                            },
 | 
				
			||||||
                            _ => {
 | 
					                            _ => { // west
 | 
				
			||||||
                                // west
 | 
					 | 
				
			||||||
                                let mut new_y = y;
 | 
					                                let mut new_y = y;
 | 
				
			||||||
                                while new_y > 0 {
 | 
					                                while new_y > 0 {
 | 
				
			||||||
                                    new_y -= 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;
 | 
					                                        new_y += 1;
 | 
				
			||||||
                                        break;
 | 
					                                        break;
 | 
				
			||||||
                                    }
 | 
					                                    }
 | 
				
			||||||
| 
						 | 
					@ -69,6 +67,7 @@ pub fn solve() {
 | 
				
			||||||
                                rocks[x][new_y] = 'O';
 | 
					                                rocks[x][new_y] = 'O';
 | 
				
			||||||
                            }
 | 
					                            }
 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
 | 
					                        
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
| 
						 | 
					@ -77,25 +76,23 @@ pub fn solve() {
 | 
				
			||||||
                for y in (0..rocks[0].len()).rev() {
 | 
					                for y in (0..rocks[0].len()).rev() {
 | 
				
			||||||
                    if rocks[x][y] == 'O' {
 | 
					                    if rocks[x][y] == 'O' {
 | 
				
			||||||
                        match counter % 4 {
 | 
					                        match counter % 4 {
 | 
				
			||||||
                            3 => {
 | 
					                            3 => { // east
 | 
				
			||||||
                                // east
 | 
					 | 
				
			||||||
                                let mut new_y = y;
 | 
					                                let mut new_y = y;
 | 
				
			||||||
                                while new_y < rocks[0].len() - 1 {
 | 
					                                while new_y < rocks[0].len()-1  {
 | 
				
			||||||
                                    new_y += 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;
 | 
					                                        new_y -= 1;
 | 
				
			||||||
                                        break;
 | 
					                                        break;
 | 
				
			||||||
                                    }
 | 
					                                    }
 | 
				
			||||||
                                }
 | 
					                                }
 | 
				
			||||||
                                rocks[x][y] = '.';
 | 
					                                rocks[x][y] = '.';
 | 
				
			||||||
                                rocks[x][new_y] = 'O';
 | 
					                                rocks[x][new_y] = 'O';
 | 
				
			||||||
                            }
 | 
					                            },
 | 
				
			||||||
                            _ => {
 | 
					                            _ => { // south
 | 
				
			||||||
                                // south
 | 
					 | 
				
			||||||
                                let mut new_x = x;
 | 
					                                let mut new_x = x;
 | 
				
			||||||
                                while new_x < rocks.len() - 1 {
 | 
					                                while new_x < rocks.len()-1 {
 | 
				
			||||||
                                    new_x += 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;
 | 
					                                        new_x -= 1;
 | 
				
			||||||
                                        break;
 | 
					                                        break;
 | 
				
			||||||
                                    }
 | 
					                                    }
 | 
				
			||||||
| 
						 | 
					@ -104,32 +101,33 @@ pub fn solve() {
 | 
				
			||||||
                                rocks[new_x][y] = 'O';
 | 
					                                rocks[new_x][y] = 'O';
 | 
				
			||||||
                            }
 | 
					                            }
 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
 | 
					                        
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        counter += 1;
 | 
					        counter += 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if counter % 4 == 0 && !cache_used {
 | 
					        if counter % 4 == 0 && !cache_used{
 | 
				
			||||||
            let cycle_start = cache.get(&rocks);
 | 
					            let cycle_start = cache.get(&rocks);
 | 
				
			||||||
            match cycle_start {
 | 
					            match cycle_start {
 | 
				
			||||||
                None => {
 | 
					                None => { cache.insert(rocks.clone(), counter); },
 | 
				
			||||||
                    cache.insert(rocks.clone(), counter);
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                Some(x) => { 
 | 
					                Some(x) => { 
 | 
				
			||||||
                    let cycle = counter - x;
 | 
					                    let cycle = counter - x;
 | 
				
			||||||
                    counter += (((4000000000 - counter) / cycle) as usize) * cycle;
 | 
					                    counter += (((4000000000-counter)/cycle) as usize) * cycle;
 | 
				
			||||||
                    cache_used = true;
 | 
					                    cache_used = true;
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    result = rocks
 | 
					    result = rocks.iter().enumerate()
 | 
				
			||||||
        .iter()
 | 
					        .map(|(i_row, row)| {
 | 
				
			||||||
        .enumerate()
 | 
					            row.iter().filter(|c| **c == 'O').count() * (rocks.len() - i_row)
 | 
				
			||||||
        .map(|(i_row, row)| row.iter().filter(|c| **c == 'O').count() * (rocks.len() - i_row))
 | 
					        })
 | 
				
			||||||
        .sum();
 | 
					        .sum();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    println!("Result 2: {result}");
 | 
					    println!("Result 2: {result}");
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,59 +0,0 @@
 | 
				
			||||||
use std::{collections::HashMap, fs};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
pub fn solve() {
 | 
					 | 
				
			||||||
    let path = "res/15/input";
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    let mut 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?"));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    contents.retain(|c| !c.is_whitespace());
 | 
					 | 
				
			||||||
    let result: usize = contents
 | 
					 | 
				
			||||||
        .split(',')
 | 
					 | 
				
			||||||
        .map(|p| p.chars().fold(0, |x, y| ((x + y as usize) * 17) % 256))
 | 
					 | 
				
			||||||
        .sum();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    println!("Result 1: {result}");
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    let mut boxes: HashMap<usize, Vec<(&str, usize)>> = HashMap::new();
 | 
					 | 
				
			||||||
    for p in contents.split(',') {
 | 
					 | 
				
			||||||
        let (t, n) = match p.contains('=') {
 | 
					 | 
				
			||||||
            true => {
 | 
					 | 
				
			||||||
                let (t, n) = p.split_once('=').unwrap();
 | 
					 | 
				
			||||||
                (t, n.parse::<usize>().unwrap())
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            false => {
 | 
					 | 
				
			||||||
                let (t, _) = p.split_once('-').unwrap();
 | 
					 | 
				
			||||||
                (t, 0usize)
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
        let i = t.chars().fold(0, |x, y| ((x + y as usize) * 17) % 256);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        let v = boxes.entry(i).or_insert(vec![]);
 | 
					 | 
				
			||||||
        if n == 0 {
 | 
					 | 
				
			||||||
            if v.iter().any(|(p, _)| *p == t) {
 | 
					 | 
				
			||||||
                v.remove(v.iter().position(|(p, _)| *p == t).unwrap());
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
            if v.iter().any(|(p, _)| *p == t) {
 | 
					 | 
				
			||||||
                let pos = v.iter().position(|(p, _)| *p == t).unwrap();
 | 
					 | 
				
			||||||
                v[pos] = (t, n);
 | 
					 | 
				
			||||||
            } else {
 | 
					 | 
				
			||||||
                v.push((t, n))
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    dbg!(&boxes);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    let result: usize = boxes
 | 
					 | 
				
			||||||
        .iter()
 | 
					 | 
				
			||||||
        .map(|(k, v)| {
 | 
					 | 
				
			||||||
            v.iter()
 | 
					 | 
				
			||||||
                .enumerate()
 | 
					 | 
				
			||||||
                .map(|(i, (_, n))| (k + 1) * (i + 1) * n)
 | 
					 | 
				
			||||||
                .sum::<usize>()
 | 
					 | 
				
			||||||
        })
 | 
					 | 
				
			||||||
        .sum();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    println!("Result 2: {result}");
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
							
								
								
									
										75
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										75
									
								
								src/main.rs
									
									
									
									
									
								
							| 
						 | 
					@ -1,81 +1,12 @@
 | 
				
			||||||
pub mod days;
 | 
					pub mod days;
 | 
				
			||||||
#[macro_use]
 | 
					#[macro_use] extern crate scan_fmt;
 | 
				
			||||||
extern crate scan_fmt;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
use std::time::Instant;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn main() {
 | 
					fn main() {
 | 
				
			||||||
    days::d15::solve()
 | 
					    days::d14::solve()
 | 
				
			||||||
    //_all_days()
 | 
					    //_all_days()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn _all_days() {
 | 
					fn _all_days() {
 | 
				
			||||||
    let start = Instant::now();
 | 
					    println!("Day 1");
 | 
				
			||||||
    println!("\nDay 1");
 | 
					 | 
				
			||||||
    days::d01::solve();
 | 
					    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);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    println!("\nDay 15");
 | 
					 | 
				
			||||||
    days::d15::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