~cdv/aoc-2018-rs

2917fdab8bfffb5be34a9b127432285d48b36a0f — Christopher Vittal 1 year, 3 months ago 375f42e
Run clippy, fix lints
M src/bin/day02.rs => src/bin/day02.rs +1 -1
@@ 27,7 27,7 @@ fn count(s: &[u8]) -> (bool, bool) {
        assert!(c >= b'a' && c <= b'z');
        freqs[(c - b'a') as usize] += 1;
    }
    freqs.into_iter().fold((false, false), |(two, three), &c| {
    freqs.iter().fold((false, false), |(two, three), &c| {
        if c == 2 {
            (true, three)
        } else if c == 3 {

M src/bin/day03.rs => src/bin/day03.rs +2 -9
@@ 36,21 36,14 @@ impl FromStr for Claim {
fn main() {
    let v: Vec<Claim> = aoc::file::to_single_parsed(INPUT);
    let mut m = vec![[0; ARSIZ]; ARSIZ];
    for &Claim {
        no: _,
        x,
        y,
        xl,
        yl,
    } in &v
    {
    for &Claim { x, y, xl, yl, .. } in &v {
        for i in 0..xl {
            for j in 0..yl {
                m[x + i][y + j] += 1;
            }
        }
    }
    let sum: usize = m.iter().map(|r| r.iter().filter(|&x| x > &1).count()).sum();
    let sum: usize = m.iter().map(|r| r.iter().filter(|&&x| x > 1).count()).sum();
    println!("  1: {}", sum);
    println!("  2: {}", find_claim(&m, &v));
}

M src/bin/day04.rs => src/bin/day04.rs +2 -4
@@ 41,7 41,7 @@ fn main() {
    let data: Vec<Vec<i16>> = aoc::file::to_split_parsed(INPUT);
    let mut data = data.into_iter().map(|b| b[..].into());
    let mut state: State = Default::default();
    let mut map = FxHashMap::default();
    let mut map = FxHashMap::<i16, Vec<(u8, u8)>>::default();
    loop {
        match data.next() {
            Some(GuardEvent {


@@ 71,13 71,11 @@ fn main() {
            None => break,
        }
        if state.f.is_some() && state.w.is_some() {
            let e = map.entry(state.g).or_insert(vec![]);
            let e = map.entry(state.g).or_default();
            if let (Some(f), Some(w)) = (state.f, state.w) {
                e.push((f, w));
                state.f = None;
                state.w = None;
            } else {
                panic!();
            }
        }
    }

M src/bin/day07.rs => src/bin/day07.rs +7 -12
@@ 29,12 29,10 @@ impl FromStr for Edge {
fn main() {
    let input: Vec<Edge> = aoc::file::to_single_parsed(INPUT);
    let mut ids = FxHashSet::default();
    let mut deps = FxHashMap::default();
    let mut deps = FxHashMap::<_, FxHashSet<_>>::default();

    for Edge { needed, needs } in input {
        deps.entry(needs)
            .or_insert(FxHashSet::default())
            .insert(needed);
        deps.entry(needs).or_default().insert(needed);
        ids.insert(needs);
        ids.insert(needed);
    }


@@ 48,8 46,7 @@ fn main() {
    {
        let mut rest = rest.clone();
        while !rest.is_empty() {
            let mut it = (0..rest.len()).rev();
            while let Some(i) = it.next() {
            for i in (0..rest.len()).rev() {
                if deps.get(&rest[i]).map_or(true, |s| done.is_superset(s)) {
                    ans.push(rest[i]);
                    done.insert(rest[i]);


@@ 72,12 69,10 @@ fn main() {
            time = *wda.iter().min().filter(|&&t| t > time).unwrap_or(&time);
            let mut it = (0..rest.len()).rev();
            while let Some(i) = it.next() {
                if {
                    if let Some(m) = deps.get(&rest[i]) {
                        m.iter().all(|d| ida.get(d).map_or(false, |&t| t <= time))
                    } else {
                        true
                    }
                if if let Some(m) = deps.get(&rest[i]) {
                    m.iter().all(|d| ida.get(d).map_or(false, |&t| t <= time))
                } else {
                    true
                } {
                    for b in wda.iter_mut() {
                        if *b <= time {

M src/bin/day09.rs => src/bin/day09.rs +1 -1
@@ 37,7 37,7 @@ impl Game<'_> {
            }
        }
        self.players
            .into_iter()
            .iter()
            .max()
            .ok_or("empty container")
            .map(|r| *r)

M src/bin/day10.rs => src/bin/day10.rs +3 -3
@@ 22,8 22,8 @@ fn main() {
    let mut by_pos = FxHashMap::default();
    for i in 1.. {
        by_pos.clear();
        let mut xp = (1000000i32, -1000000i32);
        let mut yp = (1000000i32, -1000000i32);
        let mut xp = (std::i32::MAX, std::i32::MIN);
        let mut yp = (std::i32::MAX, std::i32::MIN);

        for &mut (ref mut pos, ref vel) in &mut input {
            *pos += *vel;


@@ 43,7 43,7 @@ fn main() {
        for y in yp.0..=yp.1 {
            print!("\t");
            for x in xp.0..=xp.1 {
                if let Some(_) = by_pos.remove(&Point(x, y)) {
                if by_pos.remove(&Point(x, y)).is_some() {
                    print!("#");
                } else {
                    print!(" ");

M src/bin/day12.rs => src/bin/day12.rs +1 -1
@@ 66,7 66,7 @@ fn generation(line: Vec<u8>, zero: isize) -> (Vec<u8>, isize) {
    let next = line.windows(5).map(rule);
    let last = last(&line);
    let last = if let Some(p) = last.iter().rposition(|&b| b == b'#') {
        &last[0..p + 1]
        &last[0..=p]
    } else {
        &[]
    };

M src/bin/day16.rs => src/bin/day16.rs +9 -17
@@ 86,17 86,14 @@ fn solve_opcodes(mut map: Vec<Either<OpCode, FxHashSet<OpCode>>>) -> [OpCode; 16
                Either::Left(op) => *op,
                _ => continue,
            };
            for j in 0..map.len() {
            for (j, m) in map.iter_mut().enumerate() {
                if j == i {
                    continue;
                }
                match &mut map[j] {
                    Either::Right(m) => {
                        m.remove(&op);
                    }
                    _ => {}
                if let Either::Right(ref mut m) = m {
                    m.remove(&op);
                }
                let op = if let Either::Right(m) = &map[j] {
                let op = if let Either::Right(m) = &m {
                    if m.len() == 1 {
                        *m.iter().next().unwrap()
                    } else {


@@ 105,7 102,7 @@ fn solve_opcodes(mut map: Vec<Either<OpCode, FxHashSet<OpCode>>>) -> [OpCode; 16
                } else {
                    continue;
                };
                map[j] = Either::Left(op);
                *m = Either::Left(op);
            }
        }
    }


@@ 192,7 189,7 @@ impl Device {
            if n == 0 {
                break;
            }
            if l.trim().len() == 0 {
            if l.trim().is_empty() {
                continue;
            }
            let mut ds = DFILT.find_iter(&l).filter_map(|m| m.as_str().parse().ok());


@@ 244,14 241,9 @@ impl Test {
        }
        if candidates.len() == 1 {
            map[self.oper[0]] = Either::Left(candidates[0]);
        } else {
            match &mut map[self.oper[0]] {
                Either::Right(m) => {
                    for &op in &candidates {
                        m.insert(op);
                    }
                }
                _ => {}
        } else if let Either::Right(ref mut m) = map[self.oper[0]] {
            for &op in &candidates {
                m.insert(op);
            }
        }


M src/bin/day17.rs => src/bin/day17.rs +1 -5
@@ 19,11 19,7 @@ impl std::str::FromStr for ClayRange {
            static ref PARSE: Regex = Regex::new(r"^(x|y)=(\d+), (x|y)=(\d+)\.\.(\d+)$").unwrap();
        }
        let caps = PARSE.captures(s).expect("string did not match");
        let is_vert = if caps.get(1).unwrap().as_str() == "x" {
            true
        } else {
            false
        };
        let is_vert = caps.get(1).unwrap().as_str() == "x";
        let pos = caps.get(2).unwrap().as_str().parse().unwrap();
        let range = RangeInclusive::new(
            caps.get(4).unwrap().as_str().parse().unwrap(),

M src/file.rs => src/file.rs +3 -3
@@ 52,8 52,8 @@ where
        .map(|v| {
            let v = v.expect("error in read");
            match v.parse() {
                Err(_) => panic!("parse error for input: {}", v),
                Ok(vp) => vp,
                _ => panic!("parse error for input: {}", v),
            }
        })
        .collect()


@@ 70,8 70,8 @@ where
        .map(|s| {
            s.split_whitespace()
                .map(|v| match v.parse() {
                    Err(_) => panic!("parse error for input: {}", v),
                    Ok(vp) => vp,
                    _ => panic!("parse error for input: {}", v),
                })
                .collect()
        })


@@ 89,8 89,8 @@ where
            s.split(f)
                .filter(|s| !s.is_empty())
                .map(|v| match v.parse() {
                    Err(_) => panic!("parse error for input: {}", v),
                    Ok(vp) => vp,
                    _ => panic!("parse error for input: {}", v),
                })
                .collect()
        })

M src/tree.rs => src/tree.rs +2 -2
@@ 113,7 113,7 @@ impl<T> Iterator for BfsIntoIter<T> {
        if let Some(Node { data, sub_forest }) = match self.cur.next() {
            None => {
                if let Some(v) = self.rest.pop_front() {
                    assert!(v.len() > 0, "length should always be greater than 0");
                    assert!(!v.is_empty(), "length should always be greater than 0");
                    self.cur = v.into_iter();
                    self.cur.next()
                } else {


@@ 123,7 123,7 @@ impl<T> Iterator for BfsIntoIter<T> {
            something => something,
        } {
            self.next = Some(data);
            if sub_forest.len() > 0 {
            if !sub_forest.is_empty() {
                self.rest.push_back(sub_forest);
            }
        }

M src/unionfind.rs => src/unionfind.rs +4 -0
@@ 28,4 28,8 @@ impl UnionFind {
    pub fn len(&self) -> usize {
        self.0.len()
    }

    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
}