Compare commits
1 commit
6c64f744f7
...
36154b6780
Author | SHA1 | Date | |
---|---|---|---|
Antoine Martin | 36154b6780 |
|
@ -18,7 +18,7 @@ pub fn run() -> Result<String> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn manhattan_distance(a: &Point, b: &Point) -> u64 {
|
fn manhattan_distance(a: &Point, b: &Point) -> u64 {
|
||||||
(a.x - b.x).abs() as u64 + (a.y - b.y).abs() as u64
|
(a.x - b.x).unsigned_abs() + (a.y - b.y).unsigned_abs()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part1(first_wire: &Wire, second_wire: &Wire) -> Result<u64> {
|
fn part1(first_wire: &Wire, second_wire: &Wire) -> Result<u64> {
|
||||||
|
|
|
@ -233,12 +233,12 @@ impl Planet {
|
||||||
|
|
||||||
fn potential_energy(&self) -> u64 {
|
fn potential_energy(&self) -> u64 {
|
||||||
let pos = &self.position;
|
let pos = &self.position;
|
||||||
pos.x.abs() as u64 + pos.y.abs() as u64 + pos.z.abs() as u64
|
pos.x.unsigned_abs() + pos.y.unsigned_abs() + pos.z.unsigned_abs()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn kinetic_energy(&self) -> u64 {
|
fn kinetic_energy(&self) -> u64 {
|
||||||
let vel = &self.velocity;
|
let vel = &self.velocity;
|
||||||
vel.x.abs() as u64 + vel.y.abs() as u64 + vel.z.abs() as u64
|
vel.x.unsigned_abs() + vel.y.unsigned_abs() + vel.z.unsigned_abs()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn total_energy(&self) -> u64 {
|
fn total_energy(&self) -> u64 {
|
||||||
|
|
|
@ -226,7 +226,7 @@ impl Intcode {
|
||||||
let val1 = op1.get(&mut self.memory, self.relative_base)?;
|
let val1 = op1.get(&mut self.memory, self.relative_base)?;
|
||||||
let val2 = op2.get(&mut self.memory, self.relative_base)?;
|
let val2 = op2.get(&mut self.memory, self.relative_base)?;
|
||||||
|
|
||||||
let res = if val1 < val2 { 1 } else { 0 };
|
let res = i64::from(val1 < val2);
|
||||||
dst.set(res, &mut self.memory, self.relative_base)?;
|
dst.set(res, &mut self.memory, self.relative_base)?;
|
||||||
|
|
||||||
self.ip += 4;
|
self.ip += 4;
|
||||||
|
@ -235,7 +235,7 @@ impl Intcode {
|
||||||
let val1 = op1.get(&mut self.memory, self.relative_base)?;
|
let val1 = op1.get(&mut self.memory, self.relative_base)?;
|
||||||
let val2 = op2.get(&mut self.memory, self.relative_base)?;
|
let val2 = op2.get(&mut self.memory, self.relative_base)?;
|
||||||
|
|
||||||
let res = if val1 == val2 { 1 } else { 0 };
|
let res = i64::from(val1 == val2);
|
||||||
dst.set(res, &mut self.memory, self.relative_base)?;
|
dst.set(res, &mut self.memory, self.relative_base)?;
|
||||||
|
|
||||||
self.ip += 4;
|
self.ip += 4;
|
||||||
|
@ -263,7 +263,7 @@ impl Intcode {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_day02_output(&self) -> Option<i64> {
|
pub fn get_day02_output(&self) -> Option<i64> {
|
||||||
self.memory.get(0).copied()
|
self.memory.first().copied()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_last_output(&self) -> Option<i64> {
|
pub fn get_last_output(&self) -> Option<i64> {
|
||||||
|
|
|
@ -111,9 +111,8 @@ impl Layout {
|
||||||
count += self
|
count += self
|
||||||
.grid
|
.grid
|
||||||
.get(i)
|
.get(i)
|
||||||
.map(|line| line.get(j))
|
.and_then(|line| line.get(j))
|
||||||
.flatten()
|
.map(|&cell| u8::from(cell == value))
|
||||||
.map(|&cell| if cell == value { 1 } else { 0 })
|
|
||||||
.unwrap_or(0);
|
.unwrap_or(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,7 +130,7 @@ impl Layout {
|
||||||
|
|
||||||
let (i, j) = (i.wrapping_add(di as usize), j.wrapping_add(dj as usize));
|
let (i, j) = (i.wrapping_add(di as usize), j.wrapping_add(dj as usize));
|
||||||
|
|
||||||
let cell = self.grid.get(i).map(|line| line.get(j)).flatten();
|
let cell = self.grid.get(i).and_then(|line| line.get(j));
|
||||||
|
|
||||||
match cell {
|
match cell {
|
||||||
// keep going, the next seat is farther away
|
// keep going, the next seat is farther away
|
||||||
|
|
|
@ -22,8 +22,7 @@ fn part1(input: &str) -> Result<u64> {
|
||||||
|
|
||||||
Ok(tickets
|
Ok(tickets
|
||||||
.iter()
|
.iter()
|
||||||
.map(|t| t.invalid_values(&fields_vec))
|
.flat_map(|t| t.invalid_values(&fields_vec))
|
||||||
.flatten()
|
|
||||||
.sum())
|
.sum())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -113,7 +113,7 @@ fn mul(input: &str) -> IResult<&str, Expr> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn num(input: &str) -> IResult<&str, Expr> {
|
fn num(input: &str) -> IResult<&str, Expr> {
|
||||||
map_res(take_while1(|c: char| c.is_digit(10)), |res: &str| {
|
map_res(take_while1(|c: char| c.is_ascii_digit()), |res: &str| {
|
||||||
res.parse().map(Expr::Num)
|
res.parse().map(Expr::Num)
|
||||||
})(input)
|
})(input)
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,13 +30,7 @@ fn compute_gamma(binary_numbers: &[&str], size: usize) -> u64 {
|
||||||
let mut gamma = 0;
|
let mut gamma = 0;
|
||||||
|
|
||||||
for pos in 0..size {
|
for pos in 0..size {
|
||||||
let digit = if count_ones(binary_numbers, pos) > (binary_numbers.len() / 2) {
|
let digit = u64::from(count_ones(binary_numbers, pos) > (binary_numbers.len() / 2));
|
||||||
// majority of ones
|
|
||||||
1
|
|
||||||
} else {
|
|
||||||
// majority of zeroes
|
|
||||||
0
|
|
||||||
};
|
|
||||||
gamma = (gamma << 1) | digit;
|
gamma = (gamma << 1) | digit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -120,7 +120,7 @@ impl Grid {
|
||||||
fn unmarked_numbers(&self) -> impl Iterator<Item = u8> + '_ {
|
fn unmarked_numbers(&self) -> impl Iterator<Item = u8> + '_ {
|
||||||
self.number_to_pos
|
self.number_to_pos
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|(num, &(x, y))| (!self.access_grid(x, y)).then(|| *num))
|
.filter_map(|(num, &(x, y))| (!self.access_grid(x, y)).then_some(*num))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn access_grid(&self, x: usize, y: usize) -> bool {
|
fn access_grid(&self, x: usize, y: usize) -> bool {
|
||||||
|
|
|
@ -73,7 +73,7 @@ impl std::str::FromStr for Rules {
|
||||||
let (pair, res) = l.split_once(" -> ").context("couldn't parse rule")?;
|
let (pair, res) = l.split_once(" -> ").context("couldn't parse rule")?;
|
||||||
Ok((
|
Ok((
|
||||||
(
|
(
|
||||||
*pair.as_bytes().get(0).context("couldn't parse rule")?,
|
*pair.as_bytes().first().context("couldn't parse rule")?,
|
||||||
*pair.as_bytes().get(1).context("couldn't parse rule")?,
|
*pair.as_bytes().get(1).context("couldn't parse rule")?,
|
||||||
),
|
),
|
||||||
res.bytes().next().context("couldn't parse rule")?,
|
res.bytes().next().context("couldn't parse rule")?,
|
||||||
|
|
|
@ -64,31 +64,19 @@ impl Packet {
|
||||||
debug_assert_eq!(op.sub_packets.len(), 2);
|
debug_assert_eq!(op.sub_packets.len(), 2);
|
||||||
let pack1 = &op.sub_packets[0];
|
let pack1 = &op.sub_packets[0];
|
||||||
let pack2 = &op.sub_packets[1];
|
let pack2 = &op.sub_packets[1];
|
||||||
if pack1.value() > pack2.value() {
|
u64::from(pack1.value() > pack2.value())
|
||||||
1
|
|
||||||
} else {
|
|
||||||
0
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
OperatorType::LessThan => {
|
OperatorType::LessThan => {
|
||||||
debug_assert_eq!(op.sub_packets.len(), 2);
|
debug_assert_eq!(op.sub_packets.len(), 2);
|
||||||
let pack1 = &op.sub_packets[0];
|
let pack1 = &op.sub_packets[0];
|
||||||
let pack2 = &op.sub_packets[1];
|
let pack2 = &op.sub_packets[1];
|
||||||
if pack1.value() < pack2.value() {
|
u64::from(pack1.value() < pack2.value())
|
||||||
1
|
|
||||||
} else {
|
|
||||||
0
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
OperatorType::EqualTo => {
|
OperatorType::EqualTo => {
|
||||||
debug_assert_eq!(op.sub_packets.len(), 2);
|
debug_assert_eq!(op.sub_packets.len(), 2);
|
||||||
let pack1 = &op.sub_packets[0];
|
let pack1 = &op.sub_packets[0];
|
||||||
let pack2 = &op.sub_packets[1];
|
let pack2 = &op.sub_packets[1];
|
||||||
if pack1.value() == pack2.value() {
|
u64::from(pack1.value() == pack2.value())
|
||||||
1
|
|
||||||
} else {
|
|
||||||
0
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,9 @@
|
||||||
let
|
let
|
||||||
overlays = [ (import rust-overlay) ];
|
overlays = [ (import rust-overlay) ];
|
||||||
pkgs = import nixpkgs { inherit system overlays; };
|
pkgs = import nixpkgs { inherit system overlays; };
|
||||||
myRust = pkgs.rust-bin.stable.latest.default;
|
myRust = pkgs.rust-bin.stable.latest.default.override {
|
||||||
|
extensions = ["rust-src" "rust-analysis"];
|
||||||
|
};
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
devShell = pkgs.mkShell {
|
devShell = pkgs.mkShell {
|
||||||
|
|
Loading…
Reference in a new issue