Skip to content

Commit

Permalink
More work on past days
Browse files Browse the repository at this point in the history
  • Loading branch information
rtsuk committed Dec 26, 2022
1 parent f592ed1 commit c91e147
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/bin/day17.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ fn main() -> Result<(), Error> {
let mut starting_y = 0;
let mut block_set: BlockSet = HashSet::new();
let mut jet_index = 0;
for i in 0..2023 {
for i in 0..=opt.limit {
let mut shape = Shape::shape_for(i);
let v = vec2(2, starting_y + 3);
shape = shape.translate(v);
Expand Down Expand Up @@ -247,6 +247,8 @@ fn main() -> Result<(), Error> {

println!("bbox = {bbox:?}");

println!("height = {}", bbox.max.y + 1);

// 2568 is too low
// 2894 is too low
// 3171 is too low
Expand Down
55 changes: 52 additions & 3 deletions src/bin/day24.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#![allow(dead_code)]
use anyhow::Error;
use enum_iterator::Sequence;
use euclid::{point2, vec2};
use euclid::{point2, size2, vec2};
use structopt::StructOpt;

type Coord = i64;
type Point = euclid::default::Point2D<Coord>;
type UPoint = euclid::default::Point2D<Coord>;
type Box = euclid::default::Box2D<Coord>;
type Vector = euclid::default::Vector2D<Coord>;
type Rect = euclid::default::Rect<Coord>;
Expand Down Expand Up @@ -77,10 +78,20 @@ impl From<char> for MapCell {
}
}

fn blizzards_from_row((y, cells): (usize, &MapRow)) -> Vec<Blizzard> {
cells
.iter()
.enumerate()
.map(|(x, cells)| (point2(x, y).to_i64(), cells))
.filter_map(Blizzard::from_cell)
.collect()
}

type MapRow = Vec<MapCell>;

#[derive(Debug)]
struct Map {
bounds: Rect,
rows: Vec<MapRow>,
entrance: Point,
exit: Point,
Expand All @@ -101,7 +112,12 @@ impl Map {
.find(|(_index, cell)| **cell == MapCell::Open)
.expect("exit")
.0;
let bounds = Rect::new(
point2(1, 1),
size2(rows[0].len() - 2, rows.len() - 2).to_i64(),
);
Self {
bounds,
rows,
entrance: point2(entrance as Coord, 0),
exit: point2(exit as Coord, last_row as Coord),
Expand All @@ -126,6 +142,31 @@ impl Map {

row[p_u.x]
}

fn blizzard_starts(&self) -> Vec<Blizzard> {
self.rows
.iter()
.enumerate()
.flat_map(blizzards_from_row)
.collect()
}
}

struct Blizzard {
position: Point,
direction: Direction,
}

impl Blizzard {
fn from_cell((position, cell): (UPoint, &MapCell)) -> Option<Blizzard> {
match *cell {
MapCell::Blizzard(direction) => Some(Blizzard {
position,
direction,
}),
_ => None,
}
}
}

fn parse(s: &str) -> Map {
Expand Down Expand Up @@ -173,8 +214,16 @@ mod test {
#[test]
fn test_parse() {
let map = parse(SAMPLE);
dbg!(&map);
todo!();
assert_eq!(map.bounds.size, size2(5,5));
assert_eq!(map.bounds.origin, point2(1,1));

let blizzards = map.blizzard_starts();
assert_eq!(blizzards.len(), 2);

assert_eq!(blizzards[0].position, point2(1,2));
assert_eq!(blizzards[0].direction, Direction::East);
assert_eq!(blizzards[1].position, point2(4,4));
assert_eq!(blizzards[1].direction, Direction::South);
}

#[test]
Expand Down

0 comments on commit c91e147

Please sign in to comment.