From f592ed10f04e543bb131213daed3ba2e051720f1 Mon Sep 17 00:00:00 2001 From: Rob Tsuk Date: Mon, 26 Dec 2022 06:14:06 -0700 Subject: [PATCH] clippy and start on day 24 --- data/day24.txt | 100 +++++++++++------------------------------ src/bin/day22.rs | 6 +-- src/bin/day23.rs | 18 ++++---- src/bin/day24.rs | 115 ++++++++++++++++++++++++++++++++++++++++------- src/bin/day25.rs | 23 +++------- 5 files changed, 142 insertions(+), 120 deletions(-) diff --git a/data/day24.txt b/data/day24.txt index 2e7207e..1859b72 100644 --- a/data/day24.txt +++ b/data/day24.txt @@ -1,73 +1,27 @@ -.#.######...####..##.###.#########.######..######..#..###...#######.####. -#.#....##..#.##...#..##.#........###.#.###.#.###.###....#.###.#..##..##.. -..#########.#.#..##.##....##....##.......#...###....##..#..#.#.#..#.####. -.#..###.#.##...###..####.#.#..#.#.###.##.#.#......#.####..##.#####..#.##. -#..#.#######..####...##..#..##.###.#####...#.###.#....##.####..#.#.#####. -####.#.#..###########..#.###..#.###...###..#....#.####..###...#.##..##.#. -.##...##.##.####......#.#...#.#...###.#..##.###.###.#.########.#####.#.## -#....#.##..#.#..##..##.###.##.##..##..###.#.#...##....#.##..##.#.#..###.. -#.##.#.#.##..#.#..#####.#.#...###.#...#####...##.##.#.#......##...#...#.. -#.....#.##...##.##.#.####.#####.###..#.#.....#.###..#.######.#####.##.### -##......###...#..##.#####..###..#..######..#...#..#..#..#..#..#.###.#.##. -.....####..######..#.#.....###.#.##.#.##.##...##.#.######.##.#.##..#.###. -#.#.##.#.....#..###..#..##.######.#..####.##.##...##...#.#...#.....###### -.#.####..##...#...##.######.#.###.##.#..#...#...#..#.####.#.#####.##...#. -####.##.###...####.....#..#.##..#.#..###..#.#....#.#.#....#.#..#.###....# -.####.#..###...##.#.##........#.#.##.#.####..#..#..##..#..#.#.#.######.## -.###.#..###...######.#..##.#########.#..........####...#.###.##..#..####. -#####....######.##.#....##.####..#.###...#####.###..##..#.#.##..#..#..### -.#.###.#..##.#####.####.#.#.#.#.##..#.####.##.#.#.#..#.#.#..##.#..####.## -####......###.#.#...#.#.....#...##.#.#.......#.##...#..#.###.#.#####..#.. -.#.##....#...#.####.###.#.###.#.#.##.#.#.##.###.#####.....##..#..##.#...# -#....#...##.##.####....##...#...#.##...##.##.##...######.#.##..###.####.. -#..#.#.##..####.#.#..#.#..#....####...####..#..#..###...##.#.##.#..##.... -####..##.#.#.##.#####...####.#.##..##.#.#.#.##....##.#..#..##.##.#...#..# -##.#...#..####..##.#.#.#.#..##..##..###.#...##...#.#.#.#.##...####.##.##. -#######.#...#.####.....#.###.#..#.##.##.#####.....#.#.#..###..#....##.#.. -.###....#.#.##.#...#..##..#.#...#.#.....#.##.####..###....#.#.#.###.##.## -#.#.#..#.##.####...#..#..##....##.#.#...#.##..##.#.#####.###.#####.#..##. -.#####.#####....####...#...#..#..#..####.#........###.##..##.#.##..#.#.## -#..#..#.....#.#.##..##.#...#...###.#...###.#...##.##..##.....##.####.#... -##.#.##.###..#...#######.###.###..#.##.##.#..##.....##....##.##.....##### -..#..##.#.###.....#..#..#.##.###.##..###.#.....##..##.#.#.######..##..... -.##..##.#.##.#.#####.....#.#....#...####.#..#.##.#..#..##.#.##.##..#...#. -#.#.#..##...#.#####..#.###.##.###...##...#..##......##.#.#.#.#.#..#.#...# -..###......####.######...###..#.#....#..#.....#..#.#..#.##..####.##.##... -#...#..####..##....#...#..####.#..#####.######..#.######..####.##...#.#.# -##..#..##.####.#..##.###.....###....#...####...#..##..#.####......##.#### -.##.##.##.##....#..##.....#####.###....###..##..###..#....#.#.##..#.##### -.#.##..#.###..#.#.#...#..##...##.#.#..#..#.#..###...#..#.......#.#..#.#.# -###.......#.##.##.#..#..#.###.#####.##.#..###..#.#...##..#..#....###.#### -.##.#....##.##.#.####.##.#####..#.####..##.##.###..##.#...##.####.#..#..# -..##..###.#.#####.#.#.....#...#.#.#..##..##..##..##..#.###...#.##...#.##. -.####.#.#.##.##.#...##.##..###....#..##.##..#...##.#.###...#.##.#..#.#.## -#.###..#..#..###....#.#...#.#..########.##.######.#.#....##.#.##.##....## -.##.#...#.#.##..#.#.##.###.##.#.....##........##.######...###...######### -..##.##....#.....####....#..#..#....###.##..#.......#..##.#.##.####...##. -##......##.##.###......#.####...#..#.#...#.#.##.#.#....##...##..#.#####.# -##.#.####..####..#.##..#..####.#.#..#.##..####.##..#.##.#.#...##....#.... -###...#..##..#.#.#.##.##.#.#.#######.#.###.#...#.##.#.....##.#....#.#.... -##.#.#######.#.#....#...##.####.....####.#.##.###.#.##...#.#...#..#...### -.###......##.#..##.###.#...#......##..###.#......#.#..#...#..##.#...####. -##.#..#.#.###.#..#..#####...#.##.###.#.#.###...##.##..####.###.#..##.#.#. -#...#.#.###......##.##...####...######..##...####.#..####.#.#..####...### -##.##.#.####.##.....#..###.###...#....#..###.##....#..#...#.##.##.....#.# -.####..#.#.###.....##.#....#.##.#.#..##.....#.####..##.#####.....#####..# -#...###.#####..###.#...##...#.#.##..#####..##.##.##..#...##.#..###.#####. -###.#####..#.####.#.#.#...#.#.#...####.###.#.#..###..#.####.##..##.#..#.. -.#.....##...#.....##.#..###.#......#.##..##...####..##.#.##...#.#..#.###. -.#####.#..###.##.......#############.#.#.....##...#.##.##....###.#.##..## -####.#..#..#..##..#...#.#.#..##....#.########...####.####.#.###.....#.#.. -.##..#.##....#.#...#.#.#...###.#.#..#.#.###.##...#.###..#...#.##.######.. -#...#.####..#......#.....#######..#........###..##.#..##.#.#.#.###..##..# -.#.######.###.###.....#.#.####..##...#.##....#.##.##.####..###.#####.#.#. -...##...##.##..#.###.###.#......###.##.##.#.#.####.#.#..#...#...###...#.. -..#....#....#.#..###.#....#####.#......#....#...#..#........#..##.###.##. -#.#..##.#.####.##..##....##..#.#...#..#.#..#.#.##..#.#.####...#..#####..# -.##.###.#.#.#.#.#.#.##.###.##.#.##.######.##..##.##......####....#....#.. -.######..#####.#..#.#...#....###########.#.##.####.###.#....##.###..####. -..#####..##.#..##.#..#..#..##.#.###.#.#......######.#..#...###....#..#### -###.##...####....##..###......##.#.#...##..#.##...#.##.#.#.#.##.##.##.#.. -#..#.#.##.#.#.##..#..##.###..##.####...........#..####.#.##..#...#....#.. -###...#####.#..#..#.##.##...####....#.#..####.#..##..##.##.###.##.###.##. -#.....##.##...###.#.#..###.#####..#.#..####..#..#...#.#...##.####.#..###. \ No newline at end of file +#.######################################################################################################################## +#<>>><>>^vv<>>.^..><^.^v<<^.<.v<>>^<>>v^><<.<>v<<<><>v>^>v>vv>^vvv<^v<>^>^.v^^^v># +#>v>^^><>v^^.>><^v<>v^^^v><.^v^<^<>>^v>^>>>>^<<><>><.<>..v<>^>vv<<^<^v>v>^.^vv><<>>>>^<^>>>v><^^>^>v^.v<^<<># +#>^^v<>.>v^<<^>>v^>^>>v^^>v^^><^.v^<>vv^^.>^v^^.v^^>^^>..>^<>v>v><<^^>^^^><<# +#>vv..<^.^>v>v^.v^>.^^vv.<><^>^>.v<>.>^>vv>^<.v<.<><><.^><<>^>>^v>^<^>v.^<^^><^>vvvv><# +#>^v<..>>v^v..v^^v>v<^vv^^v..>v^^^.>>.^>>>^^>v.v<.>^^^^vv^vv^^^<>v><<^.<>>><^^vv># +#v>.v<^>.v>^>^>.>^<^>>>>>><>><.vvv<^^v>.>v^>^v.v^<^>><<>^>v><.>v^.<^^>^>v^<<^v.v^.v>><^<^<>>>v>v^.^>>>>^<<^>v><# +#>v^>>v^v<>.vv..vvv^<^^vvv<.<^.^>^.><.^v^^><<<>^.<<<^v>v.^>.<><<^.v^.<.^<# +#<>.^>><<^vv.^^vvv><^><><>>^>v<>^^vv<^><<^.^>^<<^v^<>^<^^>>^^>vv^>>^v>^v<v^.^>>.^>^<^<>^.^v<^>.^^^v><><# +#<^>v<^vvv<>v>.>vvv.>>^.^v^<.v<<^.<>vv>>v.<<<<.^<>>^.<><>^v><><>vv<<>.<^v<<<.v^v>.<.<^v<^># +#<^^.>^^vv^>^^v.v^vv>>vvv^>>^v>^v^<>^v.^.^<<<<<>>^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^v.^<><^<><^>^.^><<><>v^<<# +#>>.<^v<^<..v^>^>>>v.<<^^.v^^v.^.^v^>.v><>v>.^..^^>^>v^^<.>v<^v<>.^^^^<^.>.# +#>><^.v<^^<<>>^v<^<^^^>^<.^<..^^>^^<.^>vv^vv^^.v.^v.^v^.>v<<>^<>^v<>^^<><.>^<^<>v.^>^>^.>^<..v>>vvvvv>>vv<<.<>.# +#^^<><^^^<<>v><.>v>v^..^>^v<>v><^v.^^<^vv>v^><<>^>^^v^<^.<^vv><^><..v>^.>>.^^>v<<^<><^<<># +#<.><><^v>>>v^^<^<^^^^>>v^<.>.^vvvv<v^v>v^>>v^^^<><<>^.^>^^v.^^^<<^^v>> for Direction { - fn into(self) -> Vector { - match self { +impl From for Vector { + fn from(val: Direction) -> Self { + match val { Direction::North => vec2(0, -1), Direction::East => vec2(1, 0), Direction::South => vec2(0, 1), diff --git a/src/bin/day23.rs b/src/bin/day23.rs index 9f3f663..00356ff 100644 --- a/src/bin/day23.rs +++ b/src/bin/day23.rs @@ -51,9 +51,9 @@ impl Direction { } } -impl Into for Direction { - fn into(self) -> Vector { - match self { +impl From for Vector { + fn from(val: Direction) -> Self { + match val { Direction::North => vec2(0, -1), Direction::East => vec2(1, 0), Direction::South => vec2(0, 1), @@ -62,9 +62,9 @@ impl Into for Direction { } } -impl Into for Direction { - fn into(self) -> char { - match self { +impl From for char { + fn from(val: Direction) -> Self { + match val { Direction::North => '^', Direction::East => '>', Direction::South => 'v', @@ -235,7 +235,7 @@ impl World { } } -fn render_elves(elves: &Vec, proposals: &ProposalList) { +fn render_elves(elves: &[Elf], proposals: &ProposalList) { let bbox = Box::from_points(elves.iter().map(|e| e.position)); let elf_map: HashMap<_, _> = elves .iter() @@ -257,7 +257,7 @@ fn render_elves(elves: &Vec, proposals: &ProposalList) { }; s.push(c); } - println!("{}", s); + println!("{s}"); } } @@ -346,7 +346,7 @@ fn main() -> Result<(), Error> { let mut world2 = world.clone(); let p1 = solve_part_1(&mut world, None, false); - println!("part 1 password = {}", p1); + println!("part 1 password = {p1}"); println!("part 2 password = {}", solve_part_2(&mut world2)); diff --git a/src/bin/day24.rs b/src/bin/day24.rs index 1b36775..9a1e7b8 100644 --- a/src/bin/day24.rs +++ b/src/bin/day24.rs @@ -1,6 +1,7 @@ +#![allow(dead_code)] use anyhow::Error; use enum_iterator::Sequence; -use euclid::vec2; +use euclid::{point2, vec2}; use structopt::StructOpt; type Coord = i64; @@ -10,13 +11,13 @@ type Vector = euclid::default::Vector2D; type Rect = euclid::default::Rect; const DATA: &str = include_str!("../../data/day23.txt"); -const SAMPLE: &str = r#"....#.. -..###.# -#...#.# -.#...## -#.###.. -##.#.## -.#..#.."#; +const SAMPLE: &str = r#"#.##### +#.....# +#>....# +#.....# +#...v.# +#.....# +#####.#"#; #[derive(Debug, PartialEq, Eq, Clone, Copy, Sequence)] #[repr(usize)] @@ -33,9 +34,9 @@ impl Direction { } } -impl Into for Direction { - fn into(self) -> Vector { - match self { +impl From for Vector { + fn from(val: Direction) -> Self { + match val { Direction::North => vec2(0, -1), Direction::East => vec2(1, 0), Direction::South => vec2(0, 1), @@ -44,9 +45,9 @@ impl Into for Direction { } } -impl Into for Direction { - fn into(self) -> char { - match self { +impl From for char { + fn from(val: Direction) -> Self { + match val { Direction::North => '^', Direction::East => '>', Direction::South => 'v', @@ -55,7 +56,86 @@ impl Into for Direction { } } -fn parse(_s: &str) -> () {} +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum MapCell { + Blizzard(Direction), + Wall, + Open, +} + +impl From for MapCell { + fn from(c: char) -> Self { + match c { + '.' => MapCell::Open, + '#' => MapCell::Wall, + '^' => MapCell::Blizzard(Direction::North), + '>' => MapCell::Blizzard(Direction::East), + 'v' => MapCell::Blizzard(Direction::South), + '<' => MapCell::Blizzard(Direction::West), + _ => panic!("unknown cell"), + } + } +} + +type MapRow = Vec; + +#[derive(Debug)] +struct Map { + rows: Vec, + entrance: Point, + exit: Point, +} + +impl Map { + fn new(rows: Vec) -> Self { + let entrance = rows[0] + .iter() + .enumerate() + .find(|(_index, cell)| **cell == MapCell::Open) + .expect("entrance") + .0; + let last_row = rows.len() - 1; + let exit = rows[last_row] + .iter() + .enumerate() + .find(|(_index, cell)| **cell == MapCell::Open) + .expect("exit") + .0; + Self { + rows, + entrance: point2(entrance as Coord, 0), + exit: point2(exit as Coord, last_row as Coord), + } + } + + fn cell_at(&self, p: &Point) -> MapCell { + if p.x < 0 || p.y < 0 { + return MapCell::Wall; + } + + let p_u = p.to_usize(); + + if p_u.y >= self.rows.len() { + return MapCell::Wall; + } + + let row = &self.rows[p_u.y]; + if p_u.x >= row.len() { + return MapCell::Wall; + } + + row[p_u.x] + } +} + +fn parse(s: &str) -> Map { + let rows: Vec<_> = s + .lines() + .map(|s| s.chars().map(MapCell::from).collect::>()) + .collect(); + println!("rows = {rows:?}"); + Map::new(rows) +} fn solve_part_1() -> usize { todo!(); @@ -79,7 +159,7 @@ fn main() -> Result<(), Error> { let _ = parse(if opt.puzzle_input { DATA } else { SAMPLE }); let p1 = solve_part_1(); - println!("part 1 = {}", p1); + println!("part 1 = {p1}"); println!("part 2 = {}", solve_part_2()); @@ -92,7 +172,8 @@ mod test { #[test] fn test_parse() { - let _ = parse(SAMPLE); + let map = parse(SAMPLE); + dbg!(&map); todo!(); } diff --git a/src/bin/day25.rs b/src/bin/day25.rs index 958c981..3fef7a5 100644 --- a/src/bin/day25.rs +++ b/src/bin/day25.rs @@ -55,20 +55,7 @@ fn parse_snafu(s: &str) -> isize { value } -fn to_snafu_string(mut v: isize) -> String { - let mut snafu_digits = Vec::new(); - while v > 0 { - let amount_to_encode = v % 5; - snafu_digits.push(to_snafu_digit(amount_to_encode)); - if amount_to_encode >= 3 { - v += 5 - } - v /= 5; - } - snafu_digits.iter().rev().collect::() -} - -fn to_snafu_string2(v: isize) -> String { +fn to_snafu_string(v: isize) -> String { let snafu_digits: Vec = std::iter::repeat(()) .scan(v, |current_value, _| { let mut v = *current_value; @@ -93,10 +80,10 @@ fn parse(s: &str) -> Vec { s.lines().map(str::to_string).collect() } -fn solve_part_1(s: &Vec) -> String { +fn solve_part_1(s: &[String]) -> String { let values: Vec = s.iter().map(String::as_str).map(parse_snafu).collect(); let sum: isize = values.iter().sum(); - to_snafu_string2(sum) + to_snafu_string(sum) } #[derive(Debug, StructOpt)] @@ -113,7 +100,7 @@ fn main() -> Result<(), Error> { let value_list = parse(if opt.puzzle_input { DATA } else { SAMPLE }); let p1 = solve_part_1(&value_list); - println!("part 1 = {}", p1); + println!("part 1 = {p1}"); Ok(()) } @@ -138,6 +125,6 @@ mod test { let sum: isize = values.iter().sum(); assert_eq!(sum, 4890); - assert_eq!(to_snafu_string2(sum).as_str(), "2=-1=0"); + assert_eq!(to_snafu_string(sum).as_str(), "2=-1=0"); } }