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
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465


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); }