Advertisement
Guest User

aoc17

a guest
Dec 30th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.22 KB | None | 0 0
  1. use std::io::{BufReader};
  2. use std::io::prelude::*;
  3. use std::fs::File;
  4.  
  5. fn blocks(c: char) -> bool{
  6.     c == '#' || c == '~'
  7. }
  8.  
  9. fn water_below(map :&mut Vec<Vec<char>>, x:usize, y:usize, maxy:usize) {
  10.     println!("water below {} {}",x,y);
  11.     let mut cury = y;
  12.     loop {
  13.         if map[cury][x] == '|' || cury>maxy {
  14.             return;
  15.                 }
  16.         if map[cury][x] == '#' {
  17.           break;
  18.         }
  19.         map[cury][x] = '|';
  20.         cury = cury + 1;
  21.     }
  22.     cury=cury-1;
  23.    
  24.     let mut maxtopy = y;
  25.     let maxcury = cury;
  26.     let mut maxleftx = x;
  27.     let mut maxrightx = x;
  28.     let mut leftx ;
  29.     let mut rightx ;
  30.     //fill
  31.     loop {
  32.         leftx = x;
  33.         while blocks(map[cury+1][leftx-1]) && !blocks(map[cury][leftx-1]) {
  34.             map[cury][leftx-1] = '~';
  35.             leftx = leftx - 1;
  36.         }
  37.  
  38.         rightx = x;
  39.         while blocks(map[cury+1][rightx+1]) && !blocks(map[cury][rightx+1]) {
  40.             map[cury][rightx+1] = '~';
  41.             rightx = rightx + 1;
  42.         }
  43.         if maxleftx>leftx {
  44.             maxleftx = leftx;
  45.         }
  46.         if maxrightx<rightx {
  47.             maxrightx = rightx;
  48.         }
  49.         if blocks(map[cury][rightx+1]) && blocks(map[cury][leftx-1]) {
  50.             map[cury][x] = '~';
  51.             cury=cury-1;
  52.             if maxtopy > cury {
  53.                 maxtopy = cury;
  54.             }
  55.         } else {
  56.             break;
  57.         }
  58.     }
  59.     for i in leftx..rightx+1 {
  60.         map[cury][i] = '|';
  61.     }
  62.     for l in map.iter().skip(maxtopy-2).take(maxcury+4-maxtopy) {
  63.         for c in l.iter().skip(maxleftx-2).take(maxrightx-maxleftx+4) {
  64.             print!("{}",c);
  65.         }
  66.         println!("");
  67.     }
  68.  
  69.     if map[cury][leftx-1] == '.' {
  70.         // overflow left
  71.         water_below(map, leftx-1,cury, maxy)
  72.     }
  73.     if map[cury][rightx+1] == '.' {
  74.         // overflow right
  75.         water_below(map, rightx+1,cury,maxy)
  76.     }
  77. }
  78.  
  79. fn main() {
  80.  
  81.     let mut map = vec![vec!['.';2000];2000];
  82.     let f = File::open("/tmp/input").unwrap();
  83.     let f = BufReader::new(f);
  84.  
  85.     let mut maxy = 0;
  86.     let mut miny = 3000;
  87.     let mut maxx = 0;
  88.     let mut minx = 3000;
  89.     for line in f.lines() {
  90.         let line = line.unwrap();
  91.         let split = line.split(|c| ". =,".contains(c)).collect::<Vec<_>>();
  92.         let direction = *(split.get(0).unwrap()) == "x";
  93.         let base = split.get(1).unwrap().parse::<usize>().ok().unwrap();
  94.         let rstart = split.get(4).unwrap().parse::<usize>().ok().unwrap();
  95.         let rend = split.get(6).unwrap().parse::<usize>().ok().unwrap();
  96.         if direction {
  97.             if maxy < rend {
  98.                 maxy = rend;
  99.             }
  100.             if miny > rstart {
  101.                 miny = rstart;
  102.             }
  103.             if maxx < base {
  104.                 maxx = base;
  105.             }
  106.             if minx > base {
  107.                 minx = base;
  108.             }
  109.             for i in rstart..rend+1 {
  110.                 map[i][base] = '#';
  111.             }
  112.         } else {
  113.             if maxy < base {
  114.                 maxy = base;
  115.             }
  116.             if miny > base {
  117.                 miny = base;
  118.             }
  119.             if maxx < rend {
  120.                 maxx = rend;
  121.             }
  122.             if minx > rstart {
  123.                 minx = rstart;
  124.             }
  125.             for i in rstart..rend+1 {
  126.                 map[base][i] = '#';
  127.             }
  128.         }
  129.     }
  130.     println!("{}",miny);
  131.     println!("{}",maxy);
  132.     map[0][500] = '+';
  133.     water_below(&mut map, 500, miny,maxy);
  134.  
  135.     let mut count = 0;
  136.     let mut count2 = 0;
  137.     for l in map.iter() {
  138.         for c in l.iter() {
  139.             if *c == '~' || *c == '|' {
  140.                 count = count +1;
  141.             }
  142.             if *c == '~' {
  143.                 count2 = count2 +1;
  144.             }
  145.         }
  146.     }
  147.  
  148.     for l in map.iter().skip(miny-1).take(maxy-miny+2) {
  149.         for c in l.iter().skip(minx-1).take(maxx-minx+2) {
  150.             print!("{}",c);
  151.         }
  152.         println!("");
  153.     }
  154.     println!("{}",count);
  155.     println!("{}",count2);
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement