#[path = "../lib.rs"] mod lib; fn parse_xy(s: &str) -> Option<(i64, i64)> { let (_, ss) = s.split_once('=')?; let (xs, ys) = ss.split_once(',')?; let x = xs.parse::().ok()?; let y = ys.parse::().ok()?; return Some((x, y)); } fn parse_line(s: &str) -> Option<(i64, i64, i64, i64)> { let (ps, vs) = s.split_once(' ')?; let (px, py) = parse_xy(ps)?; let (vx, vy) = parse_xy(vs)?; return Some((px, py, vx, vy)); } fn print(bots: &Vec<(i64, i64, i64, i64)>, w: i64, h: i64) { let mut map = vec![vec![0; w as usize]; h as usize]; for (x, y, _, _) in bots.iter() { map[*y as usize][*x as usize] += 1; } for row in map.iter() { for c in row.iter() { if *c == 0 { print!(" "); } else { print!("{}", c); } } println!(""); } println!(""); } fn part2() { // store output to a file and search for many consecutive 1s let w = 101; let h = 103; let mut bots = vec![]; for line in lib::iter_input() { bots.push(parse_line(&line).unwrap()); } for i in 0.. { println!("{}", i); print(&bots, w, h); bots = bots.iter().map(|(px, py, vx, vy)| { let x = (px + vx).rem_euclid(w); let y = (py + vy).rem_euclid(h); return (x, y, *vx, *vy); }).collect(); } } fn main() { let mut quadrants = [0, 0, 0, 0]; let w = 101; let h = 103; let t = 100; for line in lib::iter_input() { let (px, py, vx, vy) = parse_line(&line).unwrap(); let x = (px + vx * t).rem_euclid(w); let y = (py + vy * t).rem_euclid(h); if x < w / 2 && y < h / 2 { quadrants[0] += 1; } else if x < w / 2 && y > h / 2 { quadrants[1] += 1; } else if x > w / 2 && y < h / 2 { quadrants[2] += 1; } else if x > w / 2 && y > h / 2 { quadrants[3] += 1; } } println!("part1: {}", quadrants.iter().fold(1, |a, b| a * b)); part2(); }