Skip to content

Commit

Permalink
clippy and start on day 24
Browse files Browse the repository at this point in the history
  • Loading branch information
rtsuk committed Dec 26, 2022
1 parent 0deacea commit f592ed1
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 120 deletions.
100 changes: 27 additions & 73 deletions data/day24.txt
Original file line number Diff line number Diff line change
@@ -1,73 +1,27 @@
.#.######...####..##.###.#########.######..######..#..###...#######.####.
#.#....##..#.##...#..##.#........###.#.###.#.###.###....#.###.#..##..##..
..#########.#.#..##.##....##....##.......#...###....##..#..#.#.#..#.####.
.#..###.#.##...###..####.#.#..#.#.###.##.#.#......#.####..##.#####..#.##.
#..#.#######..####...##..#..##.###.#####...#.###.#....##.####..#.#.#####.
####.#.#..###########..#.###..#.###...###..#....#.####..###...#.##..##.#.
.##...##.##.####......#.#...#.#...###.#..##.###.###.#.########.#####.#.##
#....#.##..#.#..##..##.###.##.##..##..###.#.#...##....#.##..##.#.#..###..
#.##.#.#.##..#.#..#####.#.#...###.#...#####...##.##.#.#......##...#...#..
#.....#.##...##.##.#.####.#####.###..#.#.....#.###..#.######.#####.##.###
##......###...#..##.#####..###..#..######..#...#..#..#..#..#..#.###.#.##.
.....####..######..#.#.....###.#.##.#.##.##...##.#.######.##.#.##..#.###.
#.#.##.#.....#..###..#..##.######.#..####.##.##...##...#.#...#.....######
.#.####..##...#...##.######.#.###.##.#..#...#...#..#.####.#.#####.##...#.
####.##.###...####.....#..#.##..#.#..###..#.#....#.#.#....#.#..#.###....#
.####.#..###...##.#.##........#.#.##.#.####..#..#..##..#..#.#.#.######.##
.###.#..###...######.#..##.#########.#..........####...#.###.##..#..####.
#####....######.##.#....##.####..#.###...#####.###..##..#.#.##..#..#..###
.#.###.#..##.#####.####.#.#.#.#.##..#.####.##.#.#.#..#.#.#..##.#..####.##
####......###.#.#...#.#.....#...##.#.#.......#.##...#..#.###.#.#####..#..
.#.##....#...#.####.###.#.###.#.#.##.#.#.##.###.#####.....##..#..##.#...#
#....#...##.##.####....##...#...#.##...##.##.##...######.#.##..###.####..
#..#.#.##..####.#.#..#.#..#....####...####..#..#..###...##.#.##.#..##....
####..##.#.#.##.#####...####.#.##..##.#.#.#.##....##.#..#..##.##.#...#..#
##.#...#..####..##.#.#.#.#..##..##..###.#...##...#.#.#.#.##...####.##.##.
#######.#...#.####.....#.###.#..#.##.##.#####.....#.#.#..###..#....##.#..
.###....#.#.##.#...#..##..#.#...#.#.....#.##.####..###....#.#.#.###.##.##
#.#.#..#.##.####...#..#..##....##.#.#...#.##..##.#.#####.###.#####.#..##.
.#####.#####....####...#...#..#..#..####.#........###.##..##.#.##..#.#.##
#..#..#.....#.#.##..##.#...#...###.#...###.#...##.##..##.....##.####.#...
##.#.##.###..#...#######.###.###..#.##.##.#..##.....##....##.##.....#####
..#..##.#.###.....#..#..#.##.###.##..###.#.....##..##.#.#.######..##.....
.##..##.#.##.#.#####.....#.#....#...####.#..#.##.#..#..##.#.##.##..#...#.
#.#.#..##...#.#####..#.###.##.###...##...#..##......##.#.#.#.#.#..#.#...#
..###......####.######...###..#.#....#..#.....#..#.#..#.##..####.##.##...
#...#..####..##....#...#..####.#..#####.######..#.######..####.##...#.#.#
##..#..##.####.#..##.###.....###....#...####...#..##..#.####......##.####
.##.##.##.##....#..##.....#####.###....###..##..###..#....#.#.##..#.#####
.#.##..#.###..#.#.#...#..##...##.#.#..#..#.#..###...#..#.......#.#..#.#.#
###.......#.##.##.#..#..#.###.#####.##.#..###..#.#...##..#..#....###.####
.##.#....##.##.#.####.##.#####..#.####..##.##.###..##.#...##.####.#..#..#
..##..###.#.#####.#.#.....#...#.#.#..##..##..##..##..#.###...#.##...#.##.
.####.#.#.##.##.#...##.##..###....#..##.##..#...##.#.###...#.##.#..#.#.##
#.###..#..#..###....#.#...#.#..########.##.######.#.#....##.#.##.##....##
.##.#...#.#.##..#.#.##.###.##.#.....##........##.######...###...#########
..##.##....#.....####....#..#..#....###.##..#.......#..##.#.##.####...##.
##......##.##.###......#.####...#..#.#...#.#.##.#.#....##...##..#.#####.#
##.#.####..####..#.##..#..####.#.#..#.##..####.##..#.##.#.#...##....#....
###...#..##..#.#.#.##.##.#.#.#######.#.###.#...#.##.#.....##.#....#.#....
##.#.#######.#.#....#...##.####.....####.#.##.###.#.##...#.#...#..#...###
.###......##.#..##.###.#...#......##..###.#......#.#..#...#..##.#...####.
##.#..#.#.###.#..#..#####...#.##.###.#.#.###...##.##..####.###.#..##.#.#.
#...#.#.###......##.##...####...######..##...####.#..####.#.#..####...###
##.##.#.####.##.....#..###.###...#....#..###.##....#..#...#.##.##.....#.#
.####..#.#.###.....##.#....#.##.#.#..##.....#.####..##.#####.....#####..#
#...###.#####..###.#...##...#.#.##..#####..##.##.##..#...##.#..###.#####.
###.#####..#.####.#.#.#...#.#.#...####.###.#.#..###..#.####.##..##.#..#..
.#.....##...#.....##.#..###.#......#.##..##...####..##.#.##...#.#..#.###.
.#####.#..###.##.......#############.#.#.....##...#.##.##....###.#.##..##
####.#..#..#..##..#...#.#.#..##....#.########...####.####.#.###.....#.#..
.##..#.##....#.#...#.#.#...###.#.#..#.#.###.##...#.###..#...#.##.######..
#...#.####..#......#.....#######..#........###..##.#..##.#.#.#.###..##..#
.#.######.###.###.....#.#.####..##...#.##....#.##.##.####..###.#####.#.#.
...##...##.##..#.###.###.#......###.##.##.#.#.####.#.#..#...#...###...#..
..#....#....#.#..###.#....#####.#......#....#...#..#........#..##.###.##.
#.#..##.#.####.##..##....##..#.#...#..#.#..#.#.##..#.#.####...#..#####..#
.##.###.#.#.#.#.#.#.##.###.##.#.##.######.##..##.##......####....#....#..
.######..#####.#..#.#...#....###########.#.##.####.###.#....##.###..####.
..#####..##.#..##.#..#..#..##.#.###.#.#......######.#..#...###....#..####
###.##...####....##..###......##.#.#...##..#.##...#.##.#.#.#.##.##.##.#..
#..#.#.##.#.#.##..#..##.###..##.####...........#..####.#.##..#...#....#..
###...#####.#..#..#.##.##...####....#.#..####.#..##..##.##.###.##.###.##.
#.....##.##...###.#.#..###.#####..#.#..####..#..#...#.#...##.####.#..###.
#.########################################################################################################################
#<>>><>>^vv<>>.^..><^.^v<<^.<.v<>>^<>><v>v^><<.<>v<<<v<.><><>v>^>v>vv<v.<<>>^vv<v<v<v<^v><v<v<^<v<v^v>v<^v<>^>^.v^^^v<v>>#
#>v>^^><>v^^.>><<v>^v<>v^^^v><.^v^<^<>>^v>^>>>>^<<><>><.<>..v<>^>vv<<^<^v>v>^.^vv><<>>>>^<^>>>v><^<vv.<.^^v>^>^>v^.v<^<<>#
#>^<v<<<<v>^v<><v^>.>v^<<^>>v^>^>>v^^>v^^><v<vvv><^.<vv.>v^<vv><>vv^^.>^v^^.v^^><vv>^^>..>^<>v>v><<^^>^^^><v^<<<<<><<<v<>#
#>vv..<^.^>v>v^.v^>.^^vv.<><v^<><^>^>.<v>v<>.><v>^<v<vv>>vv>^<<v<<<>.v<.<><><.^><<<v^.vv<<^<<>>^>>^v>^<^>v.^<^^><^>vvvv><#
#>^<v>v<..>>v^v..v^^v>v<v^^<><^vv^^v..>v<v>^^^<v>.>>.^>>>^^><v^.v>v.v<.>^^^^vv^vv^^^<>v><<^.<>>><^^vv<v>><vvvv.^<vv^<><v>#
#<v<>v>.v<^>.v>^>^>.>^<^>>>>>><>><<vv<^>.vvv<^^v>.>v^>^v.v^<^>><<>^>v><.>v^.<^^>^>v^<<^v.v^.v>><^<^<>>>v>v^.^>>>>^<<^>v><#
#>v^>>v^v<>.vv..vvv^<^^vvv<.<^.^>^.><.^v^^><v.<v<><<<<v<v^v<<^<v<.<^v^>><vv^<v<<>^.<v<v><v<^^v<.><<<^v>v.^>.<><<^.v^.<.^<#
#<>.^>><<^vv.^^vvv><^><<v^<<^.v^>><>>^>v<>^^vv<^><<^.^>^<<^v^<>^<^^>>^^>vv^>>^v>^v<<v^v>v^.^>>.^>^<^<>^.^v<^>.^^^v><><v><#
#<vvv<><^>v<^vvv<>v>.<v^<<<v>>vv<v^v.v<^<^>v.>>^.^v^<.v<<^.<<vv.<>>vv>>v.<<<<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^v^v<<^^<<^.^<v>v>.vv^>v<v^><v^<.<v<<^v<<v<<^>^<.>>^.v^^vvv.^^<<v<>#
#>v<v<^v>v^^>^>>^<^.v>>><<^^>^<>v><<^<^v^vv<vv>^^<v<<v^<vv>><>.^vv<^vv..<<^v>>^v<v.<.>^^^^v^v<.v.<><>vv>^v<^>v^>.>v>v.^><#
#.><<v>^v^v^^v>v>v^><v<vv^<>>>><^v.<v^<>>>^v>v.^<>v>v^^v<^v>>>>^v><>><<.<>.>vv<<^vv^vv^<.^^>^.^.v^^.^^>^>^^><<<v<>>>^..v<#
#<vv<<.<>vv^>^.<^vv^><^v^vv.<.>^v>v^^^><^<^>v<>^>v^<>v^<v^>vv.>^^vv^>><^^><vv<^vv>^>v>^v.^<><^<><^><v<>^.<v^^>^><<><>v^<<#
#>>.<^v<^<.<vv<<^vv^.^v<>.v^>^>><v>>v.<<^^.v^^v.<vv^^<vvv^>^<vv<v.>.^v^>.v><>v>.^..^<vv.v><v^<>^>^>v^^<.>v<^v<>.^^^^<^.>.#
#>><^.v<^^<<v.<><>>^v<^<^^^>^<.^<..^^>^^<.^>vv^vv^^.v.^v.^v^.>v<<>^<>^v<>^^<><.>^<^<>v.^>^>^.<vv^>>^<..v>>vvvvv>>vv<<.<>.#
#<v^^^<^^^<^<<>^^<><^^^<<>v><v^<<v<^^><.>v>v^..^>^v<>v><^v.^^<^vv>v^><<>^>^^v^<^.<^vv><^><..v>^.<vv<>>>.^^<v>>v<<^<><^<<>#
#<.><><^v>>>v^^<^<^^^^>>v^<<v>.>.^vvvv<<v<.>v^v>v^>>v^^^<><<>^.^>^^<v<<v<<<.v>v.^^^<<^^v<v<>><v^^v<^vv^v<^vv>><v^<<<v^v<.#
########################################################################################################################.#
6 changes: 3 additions & 3 deletions src/bin/day22.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@ impl Direction {
}
}

impl Into<Vector> for Direction {
fn into(self) -> Vector {
match self {
impl From<Direction> for Vector {
fn from(val: Direction) -> Self {
match val {
Direction::North => vec2(0, -1),
Direction::East => vec2(1, 0),
Direction::South => vec2(0, 1),
Expand Down
18 changes: 9 additions & 9 deletions src/bin/day23.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ impl Direction {
}
}

impl Into<Vector> for Direction {
fn into(self) -> Vector {
match self {
impl From<Direction> for Vector {
fn from(val: Direction) -> Self {
match val {
Direction::North => vec2(0, -1),
Direction::East => vec2(1, 0),
Direction::South => vec2(0, 1),
Expand All @@ -62,9 +62,9 @@ impl Into<Vector> for Direction {
}
}

impl Into<char> for Direction {
fn into(self) -> char {
match self {
impl From<Direction> for char {
fn from(val: Direction) -> Self {
match val {
Direction::North => '^',
Direction::East => '>',
Direction::South => 'v',
Expand Down Expand Up @@ -235,7 +235,7 @@ impl World {
}
}

fn render_elves(elves: &Vec<Elf>, 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()
Expand All @@ -257,7 +257,7 @@ fn render_elves(elves: &Vec<Elf>, proposals: &ProposalList) {
};
s.push(c);
}
println!("{}", s);
println!("{s}");
}
}

Expand Down Expand Up @@ -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));

Expand Down
115 changes: 98 additions & 17 deletions src/bin/day24.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,13 +11,13 @@ type Vector = euclid::default::Vector2D<Coord>;
type Rect = euclid::default::Rect<Coord>;

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)]
Expand All @@ -33,9 +34,9 @@ impl Direction {
}
}

impl Into<Vector> for Direction {
fn into(self) -> Vector {
match self {
impl From<Direction> for Vector {
fn from(val: Direction) -> Self {
match val {
Direction::North => vec2(0, -1),
Direction::East => vec2(1, 0),
Direction::South => vec2(0, 1),
Expand All @@ -44,9 +45,9 @@ impl Into<Vector> for Direction {
}
}

impl Into<char> for Direction {
fn into(self) -> char {
match self {
impl From<Direction> for char {
fn from(val: Direction) -> Self {
match val {
Direction::North => '^',
Direction::East => '>',
Direction::South => 'v',
Expand All @@ -55,7 +56,86 @@ impl Into<char> for Direction {
}
}

fn parse(_s: &str) -> () {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum MapCell {
Blizzard(Direction),
Wall,
Open,
}

impl From<char> 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<MapCell>;

#[derive(Debug)]
struct Map {
rows: Vec<MapRow>,
entrance: Point,
exit: Point,
}

impl Map {
fn new(rows: Vec<MapRow>) -> 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::<Vec<_>>())
.collect();
println!("rows = {rows:?}");
Map::new(rows)
}

fn solve_part_1() -> usize {
todo!();
Expand All @@ -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());

Expand All @@ -92,7 +172,8 @@ mod test {

#[test]
fn test_parse() {
let _ = parse(SAMPLE);
let map = parse(SAMPLE);
dbg!(&map);
todo!();
}

Expand Down
23 changes: 5 additions & 18 deletions src/bin/day25.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>()
}

fn to_snafu_string2(v: isize) -> String {
fn to_snafu_string(v: isize) -> String {
let snafu_digits: Vec<char> = std::iter::repeat(())
.scan(v, |current_value, _| {
let mut v = *current_value;
Expand All @@ -93,10 +80,10 @@ fn parse(s: &str) -> Vec<String> {
s.lines().map(str::to_string).collect()
}

fn solve_part_1(s: &Vec<String>) -> String {
fn solve_part_1(s: &[String]) -> String {
let values: Vec<isize> = 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)]
Expand All @@ -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(())
}
Expand All @@ -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");
}
}

0 comments on commit f592ed1

Please sign in to comment.