Day 2: Fulminating Farmland

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

  • mcmodknower@programming.dev
    link
    fedilink
    English
    arrow-up
    5
    ·
    4 days ago

    Rust

    part 1 is finding the distance to the next char in a string, part 2 can be done with some simple math (result is about 6*input.len()).

    Might update part 2 later with an even simpler version.

    Solution
    use std::{collections::HashMap, fs};
    
    fn main() {
        let contents = fs::read_to_string("../../inputs/2026/2")
    	.expect("Could not read input file");
        // let contents = "rybryb";
    
        let mut pos = 0;
        let w = "roygbvroygbv";
        let l = w.len()/2;
        let mut counts: HashMap<char,i32> = HashMap::new();
    
        for c in contents.lines().next().unwrap().chars() {
    	let off = pos%l+1;
    	// println!("{} {}", c, off);
    	let p = w[off..].find(c).unwrap();
    	pos += p+1;
    
    	counts.entry(c).and_modify(|e| *e += 1).or_insert(1);
        }
    
        println!("p1: {}", pos);
    
        let mut pos = 0;
        let mut d = 6;
        for c in "rvbgyo".chars() {
    	let a = counts.get(&c).unwrap_or(&0);
    	if *a == 0 { d-=1; continue; }
    	pos += d;
    	d = 5;
    	pos += (a-1)*6;
        }
        println!("p2: {}", pos);
    }