diff --git a/aoc2019/src/day03.rs b/aoc2019/src/day03.rs index 38727f4..40a1f9a 100644 --- a/aoc2019/src/day03.rs +++ b/aoc2019/src/day03.rs @@ -18,7 +18,7 @@ pub fn run() -> Result { } fn manhattan_distance(a: &Point, b: &Point) -> u64 { - (a.x - b.x).unsigned_abs() + (a.y - b.y).unsigned_abs() + (a.x - b.x).abs() as u64 + (a.y - b.y).abs() as u64 } fn part1(first_wire: &Wire, second_wire: &Wire) -> Result { diff --git a/aoc2019/src/day12.rs b/aoc2019/src/day12.rs index a178241..fa3aa55 100644 --- a/aoc2019/src/day12.rs +++ b/aoc2019/src/day12.rs @@ -233,12 +233,12 @@ impl Planet { fn potential_energy(&self) -> u64 { let pos = &self.position; - pos.x.unsigned_abs() + pos.y.unsigned_abs() + pos.z.unsigned_abs() + pos.x.abs() as u64 + pos.y.abs() as u64 + pos.z.abs() as u64 } fn kinetic_energy(&self) -> u64 { let vel = &self.velocity; - vel.x.unsigned_abs() + vel.y.unsigned_abs() + vel.z.unsigned_abs() + vel.x.abs() as u64 + vel.y.abs() as u64 + vel.z.abs() as u64 } fn total_energy(&self) -> u64 { diff --git a/aoc2019/src/intcode/mod.rs b/aoc2019/src/intcode/mod.rs index 10ec3e1..a93ba8b 100644 --- a/aoc2019/src/intcode/mod.rs +++ b/aoc2019/src/intcode/mod.rs @@ -226,7 +226,7 @@ impl Intcode { let val1 = op1.get(&mut self.memory, self.relative_base)?; let val2 = op2.get(&mut self.memory, self.relative_base)?; - let res = i64::from(val1 < val2); + let res = if val1 < val2 { 1 } else { 0 }; dst.set(res, &mut self.memory, self.relative_base)?; self.ip += 4; @@ -235,7 +235,7 @@ impl Intcode { let val1 = op1.get(&mut self.memory, self.relative_base)?; let val2 = op2.get(&mut self.memory, self.relative_base)?; - let res = i64::from(val1 == val2); + let res = if val1 == val2 { 1 } else { 0 }; dst.set(res, &mut self.memory, self.relative_base)?; self.ip += 4; @@ -263,7 +263,7 @@ impl Intcode { } pub fn get_day02_output(&self) -> Option { - self.memory.first().copied() + self.memory.get(0).copied() } pub fn get_last_output(&self) -> Option { diff --git a/aoc2020/src/day11.rs b/aoc2020/src/day11.rs index 9d068a5..8a05a0a 100644 --- a/aoc2020/src/day11.rs +++ b/aoc2020/src/day11.rs @@ -111,8 +111,9 @@ impl Layout { count += self .grid .get(i) - .and_then(|line| line.get(j)) - .map(|&cell| u8::from(cell == value)) + .map(|line| line.get(j)) + .flatten() + .map(|&cell| if cell == value { 1 } else { 0 }) .unwrap_or(0); } @@ -130,7 +131,7 @@ impl Layout { let (i, j) = (i.wrapping_add(di as usize), j.wrapping_add(dj as usize)); - let cell = self.grid.get(i).and_then(|line| line.get(j)); + let cell = self.grid.get(i).map(|line| line.get(j)).flatten(); match cell { // keep going, the next seat is farther away diff --git a/aoc2020/src/day16.rs b/aoc2020/src/day16.rs index 4f30ecb..69f30d2 100644 --- a/aoc2020/src/day16.rs +++ b/aoc2020/src/day16.rs @@ -22,7 +22,8 @@ fn part1(input: &str) -> Result { Ok(tickets .iter() - .flat_map(|t| t.invalid_values(&fields_vec)) + .map(|t| t.invalid_values(&fields_vec)) + .flatten() .sum()) } diff --git a/aoc2020/src/day18.rs b/aoc2020/src/day18.rs index 287ae2e..4df8254 100644 --- a/aoc2020/src/day18.rs +++ b/aoc2020/src/day18.rs @@ -113,7 +113,7 @@ fn mul(input: &str) -> IResult<&str, Expr> { } fn num(input: &str) -> IResult<&str, Expr> { - map_res(take_while1(|c: char| c.is_ascii_digit()), |res: &str| { + map_res(take_while1(|c: char| c.is_digit(10)), |res: &str| { res.parse().map(Expr::Num) })(input) } diff --git a/aoc2021/src/day03.rs b/aoc2021/src/day03.rs index a96113c..919e327 100644 --- a/aoc2021/src/day03.rs +++ b/aoc2021/src/day03.rs @@ -30,7 +30,13 @@ fn compute_gamma(binary_numbers: &[&str], size: usize) -> u64 { let mut gamma = 0; for pos in 0..size { - let digit = u64::from(count_ones(binary_numbers, pos) > (binary_numbers.len() / 2)); + let digit = if count_ones(binary_numbers, pos) > (binary_numbers.len() / 2) { + // majority of ones + 1 + } else { + // majority of zeroes + 0 + }; gamma = (gamma << 1) | digit; } diff --git a/aoc2021/src/day04.rs b/aoc2021/src/day04.rs index d18cff5..15a1250 100644 --- a/aoc2021/src/day04.rs +++ b/aoc2021/src/day04.rs @@ -120,7 +120,7 @@ impl Grid { fn unmarked_numbers(&self) -> impl Iterator + '_ { self.number_to_pos .iter() - .filter_map(|(num, &(x, y))| (!self.access_grid(x, y)).then_some(*num)) + .filter_map(|(num, &(x, y))| (!self.access_grid(x, y)).then(|| *num)) } fn access_grid(&self, x: usize, y: usize) -> bool { diff --git a/aoc2021/src/day14.rs b/aoc2021/src/day14.rs index feaf38e..a91b2a6 100644 --- a/aoc2021/src/day14.rs +++ b/aoc2021/src/day14.rs @@ -73,7 +73,7 @@ impl std::str::FromStr for Rules { let (pair, res) = l.split_once(" -> ").context("couldn't parse rule")?; Ok(( ( - *pair.as_bytes().first().context("couldn't parse rule")?, + *pair.as_bytes().get(0).context("couldn't parse rule")?, *pair.as_bytes().get(1).context("couldn't parse rule")?, ), res.bytes().next().context("couldn't parse rule")?, diff --git a/aoc2021/src/day16.rs b/aoc2021/src/day16.rs index c744926..d02fd94 100644 --- a/aoc2021/src/day16.rs +++ b/aoc2021/src/day16.rs @@ -64,19 +64,31 @@ impl Packet { debug_assert_eq!(op.sub_packets.len(), 2); let pack1 = &op.sub_packets[0]; let pack2 = &op.sub_packets[1]; - u64::from(pack1.value() > pack2.value()) + if pack1.value() > pack2.value() { + 1 + } else { + 0 + } } OperatorType::LessThan => { debug_assert_eq!(op.sub_packets.len(), 2); let pack1 = &op.sub_packets[0]; let pack2 = &op.sub_packets[1]; - u64::from(pack1.value() < pack2.value()) + if pack1.value() < pack2.value() { + 1 + } else { + 0 + } } OperatorType::EqualTo => { debug_assert_eq!(op.sub_packets.len(), 2); let pack1 = &op.sub_packets[0]; let pack2 = &op.sub_packets[1]; - u64::from(pack1.value() == pack2.value()) + if pack1.value() == pack2.value() { + 1 + } else { + 0 + } } }, } diff --git a/flake.lock b/flake.lock index 2affeb8..f8b7e1c 100644 --- a/flake.lock +++ b/flake.lock @@ -2,11 +2,11 @@ "nodes": { "flake-utils": { "locked": { - "lastModified": 1667395993, - "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=", + "lastModified": 1637014545, + "narHash": "sha256-26IZAc5yzlD9FlDT54io1oqG/bBoyka+FJk5guaX4x4=", "owner": "numtide", "repo": "flake-utils", - "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f", + "rev": "bba5dcc8e0b20ab664967ad83d24d64cb64ec4f4", "type": "github" }, "original": { @@ -17,11 +17,11 @@ }, "flake-utils_2": { "locked": { - "lastModified": 1659877975, - "narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=", + "lastModified": 1637014545, + "narHash": "sha256-26IZAc5yzlD9FlDT54io1oqG/bBoyka+FJk5guaX4x4=", "owner": "numtide", "repo": "flake-utils", - "rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0", + "rev": "bba5dcc8e0b20ab664967ad83d24d64cb64ec4f4", "type": "github" }, "original": { @@ -48,11 +48,11 @@ }, "nixpkgs_2": { "locked": { - "lastModified": 1665296151, - "narHash": "sha256-uOB0oxqxN9K7XGF1hcnY+PQnlQJ+3bP2vCn/+Ru/bbc=", + "lastModified": 1637453606, + "narHash": "sha256-Gy6cwUswft9xqsjWxFYEnx/63/qzaFUwatcbV5GF/GQ=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "14ccaaedd95a488dd7ae142757884d8e125b3363", + "rev": "8afc4e543663ca0a6a4f496262cd05233737e732", "type": "github" }, "original": { @@ -75,11 +75,11 @@ "nixpkgs": "nixpkgs_2" }, "locked": { - "lastModified": 1670207212, - "narHash": "sha256-uuKbbv0L+QoXiqO7METP9BihY0F7hJqGdKn7xDVfyFw=", + "lastModified": 1638584238, + "narHash": "sha256-s9ABdhsYMTz0Qp1EoORN1O8PlJ68390iGv0+KUwaktg=", "owner": "oxalica", "repo": "rust-overlay", - "rev": "18823e511bc85ed27bfabe33cccecb389f9aa92d", + "rev": "4a918c124e43188e8a1d2492a731befe47effa71", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index cd0a5af..ffaa247 100644 --- a/flake.nix +++ b/flake.nix @@ -10,9 +10,7 @@ let overlays = [ (import rust-overlay) ]; pkgs = import nixpkgs { inherit system overlays; }; - myRust = pkgs.rust-bin.stable.latest.default.override { - extensions = ["rust-src" "rust-analysis"]; - }; + myRust = pkgs.rust-bin.stable.latest.default; in { devShell = pkgs.mkShell {