aoc2021-rust

Advent of Code 2021 Solutions in Rust
git clone https://git.sinitax.com/sinitax/aoc2021-rust
Log | Files | Refs | README | sfeed.txt

main.rs (2494B)


      1use std::collections::HashMap;
      2
      3#[derive(Hash,PartialEq,Eq,Clone)]
      4struct Pos {
      5    x: usize,
      6    y: usize
      7}
      8
      9fn parse_input(input: &str) -> (HashMap<Pos,char>,usize,usize) {
     10    let mut map = HashMap::new();
     11    let mut y: usize = 0;
     12    for line in input.lines() {
     13        if line.is_empty() { continue; }
     14        for (x,c) in line.chars().enumerate() {
     15            if c == '>' || c == 'v' {
     16                map.insert(Pos { x, y }, c);
     17            }
     18        }
     19        y += 1;
     20    }
     21    let width = map.iter().map(|(p,_)| p.x).max().unwrap() + 1;
     22    return (map,width,y);
     23}
     24
     25fn printmap(map: &HashMap<Pos,char>, width: usize, height: usize) {
     26    if aoc::get_debug() == 0 { return; }
     27    for y in 0..height {
     28        for x in 0..width {
     29            let pos = Pos { x, y };
     30            let res = map.get(&pos);
     31            if res.is_some() {
     32                aoc::debug!("{}", *res.unwrap());
     33            } else {
     34                aoc::debug!(".");
     35            }
     36        }
     37        aoc::debugln!("");
     38    }
     39    aoc::debugln!("");
     40}
     41
     42fn walk(map: &HashMap<Pos,char>, width: usize, height: usize)
     43        -> (HashMap<Pos,char>,usize) {
     44    let mut nmap: HashMap<Pos,char> = HashMap::new();
     45    let mut moved: usize = 0;
     46
     47    for (p,c) in map.iter() {
     48        if *c != '>' { continue; }
     49
     50        let np = Pos { x: (p.x + width + 1) % width, y: p.y };
     51        if map.get(&np).is_none() {
     52            moved += 1;
     53            nmap.insert(np, '>');
     54        } else {
     55            nmap.insert(p.clone(), '>');
     56        }
     57    }
     58
     59    for (p,c) in map.iter() {
     60        if *c != 'v' { continue; }
     61
     62        let np = Pos { x: p.x, y: (p.y + height + 1) % height };
     63        let res = map.get(&np);
     64        if (res.is_none() || *res.unwrap() != 'v') && nmap.get(&np).is_none() {
     65            moved += 1;
     66            nmap.insert(np, 'v');
     67        } else {
     68            nmap.insert(p.clone(), 'v');
     69        }
     70    }
     71
     72    return (nmap,moved);
     73}
     74
     75fn part1(aoc: &mut aoc::Info) {
     76    let (mut map, width, height) = parse_input(&aoc.input);
     77
     78    let mut answer: usize = 1;
     79    loop {
     80        printmap(&map, width, height);
     81        let (nmap,cnt) = walk(&map, width, height);
     82        aoc::debugln!("{} {}", answer, cnt);
     83        if cnt == 0 { break; }
     84        map = nmap;
     85        answer += 1;
     86    }
     87
     88    aoc.answer = Some(format!("{}", answer));
     89    aoc.solution = Some("492");
     90}
     91
     92fn part2(aoc: &mut aoc::Info) {
     93    aoc.answer = Some(format!(""));
     94    aoc.solution = Some("");
     95}
     96
     97fn main() {
     98    aoc::run(part1, part2);
     99}
    100