···125125 (self.width() - 1, self.height() - 1)
126126 }
127127128128+ /// Get if a given position is in this grid's bounds
129129+ pub fn in_bounds(&self, pos: &Position) -> bool {
130130+ pos.x >= 0 && pos.y >= 0 && pos.x < self.width() as isize && pos.y < self.height() as isize
131131+ }
132132+128133 /// Get a value from the grid at the given position.
129134 ///
130135 /// # Examples
+2-2
utils/src/pos.rs
···3030#[macro_export]
3131macro_rules! upos {
3232 ($x:expr, $y:expr) => {
3333- $crate::pos::ipos!($x as isize, $y as isize)
3333+ $crate::ipos!($x as isize, $y as isize)
3434 };
3535}
3636···6060 }
61616262 /// Create a new position at 0, 0
6363- pub fn zero() -> Self {
6363+ pub const fn zero() -> Self {
6464 Self { x: 0, y: 0 }
6565 }
6666
+65-7
years/2024/src/day_7.rs
···11-22-use advent_core::{Day, day_stuff, ex_for_day};
11+use advent_core::{day_stuff, ex_for_day, Day};
3243pub struct Day7;
5465impl Day for Day7 {
66+ day_stuff!(7, "3749", "11387", Vec<(i64, Vec<i64>)>);
7788- day_stuff!(7, "", "");
88+ fn part_1(input: Self::Input) -> Option<String> {
99+ let ans = input
1010+ .into_iter()
1111+ .filter_map(|(target, operands)| {
1212+ let ps: u64 = 1 << (operands.len() - 1);
1313+ for p in 0..=ps {
1414+ let res = operands.iter().enumerate().fold(0, |acc, (i, e)| {
1515+ if i == 0 {
1616+ acc + *e
1717+ } else if p & (1 << (i - 1)) != 0 {
1818+ acc * *e
1919+ } else {
2020+ acc + *e
2121+ }
2222+ });
2323+ if res == target {
2424+ return Some(target);
2525+ }
2626+ }
2727+ None
2828+ })
2929+ .sum::<i64>();
9301010- fn part_1(_input: Self::Input) -> Option<String> {
1111- None
3131+ Some(ans.to_string())
1232 }
13331414- fn part_2(_input: Self::Input) -> Option<String> {
1515- None
3434+ fn part_2(input: Self::Input) -> Option<String> {
3535+ let ans = input
3636+ .into_iter()
3737+ .filter_map(|(target, operands)| {
3838+ let ps: u64 = 3_u64.pow((operands.len() - 1) as u32);
3939+ for p in 0..=ps {
4040+ let res = operands.iter().enumerate().fold(0, |acc, (i, e)| {
4141+ if i == 0 {
4242+ acc + *e
4343+ } else if p / 3_u64.pow((i - 1) as u32) % 3 == 1 {
4444+ acc * *e
4545+ } else if p / 3_u64.pow((i - 1) as u32) % 3 == 2 {
4646+ // Integer logartihm to prevent calls to to_string?
4747+ format!("{acc}{e}").parse().unwrap()
4848+ } else {
4949+ acc + *e
5050+ }
5151+ });
5252+ if res == target {
5353+ return Some(target);
5454+ }
5555+ }
5656+ None
5757+ })
5858+ .sum::<i64>();
5959+6060+ Some(ans.to_string())
6161+ }
6262+6363+ fn parse_input(input: &str) -> Self::Input {
6464+ input
6565+ .lines()
6666+ .map(|l| {
6767+ let (eq, rest) = l.split_once(": ").unwrap();
6868+ (
6969+ eq.parse().unwrap(),
7070+ rest.split(" ").map(|x| x.parse().unwrap()).collect(),
7171+ )
7272+ })
7373+ .collect()
1674 }
1775}