Skip to content

Commit

Permalink
2023: use using enum to simplify some code
Browse files Browse the repository at this point in the history
  • Loading branch information
yut23 committed Feb 16, 2024
1 parent ff6bd86 commit 6b89ff7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 38 deletions.
37 changes: 19 additions & 18 deletions 2023/src/day10.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,47 +35,48 @@ enum class Pipe : char {
};

std::optional<AbsDirection> get_out_dir(Pipe pipe, AbsDirection in_dir) {
using enum AbsDirection;
switch (pipe) {
case Pipe::straight_vert:
if (in_dir == AbsDirection::north || in_dir == AbsDirection::south) {
if (in_dir == north || in_dir == south) {
return in_dir;
}
break;
case Pipe::straight_horz:
if (in_dir == AbsDirection::west || in_dir == AbsDirection::east) {
if (in_dir == west || in_dir == east) {
return in_dir;
}
break;
case Pipe::bend_ne:
if (in_dir == AbsDirection::south) {
return AbsDirection::east;
if (in_dir == south) {
return east;
}
if (in_dir == AbsDirection::west) {
return AbsDirection::north;
if (in_dir == west) {
return north;
}
break;
case Pipe::bend_nw:
if (in_dir == AbsDirection::south) {
return AbsDirection::west;
if (in_dir == south) {
return west;
}
if (in_dir == AbsDirection::east) {
return AbsDirection::north;
if (in_dir == east) {
return north;
}
break;
case Pipe::bend_sw:
if (in_dir == AbsDirection::north) {
return AbsDirection::west;
if (in_dir == north) {
return west;
}
if (in_dir == AbsDirection::east) {
return AbsDirection::south;
if (in_dir == east) {
return south;
}
break;
case Pipe::bend_se:
if (in_dir == AbsDirection::north) {
return AbsDirection::east;
if (in_dir == north) {
return east;
}
if (in_dir == AbsDirection::west) {
return AbsDirection::south;
if (in_dir == west) {
return south;
}
break;
case Pipe::start:
Expand Down
41 changes: 21 additions & 20 deletions 2023/src/day16.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,49 +37,50 @@ struct Tile {
};

std::vector<AbsDirection> Tile::get_out_dir(AbsDirection in_dir) const {
using enum AbsDirection;
AbsDirection out_dir = in_dir;
switch (type) {
case TileType::empty:
break;
case TileType::vert_splitter:
if (in_dir == AbsDirection::east || in_dir == AbsDirection::west) {
return {AbsDirection::north, AbsDirection::south};
if (in_dir == east || in_dir == west) {
return {north, south};
}
break;
case TileType::horz_splitter:
if (in_dir == AbsDirection::north || in_dir == AbsDirection::south) {
return {AbsDirection::east, AbsDirection::west};
if (in_dir == north || in_dir == south) {
return {east, west};
}
break;
case TileType::forward_mirror:
switch (in_dir) {
case AbsDirection::north:
out_dir = AbsDirection::east;
case north:
out_dir = east;
break;
case AbsDirection::south:
out_dir = AbsDirection::west;
case south:
out_dir = west;
break;
case AbsDirection::east:
out_dir = AbsDirection::north;
case east:
out_dir = north;
break;
case AbsDirection::west:
out_dir = AbsDirection::south;
case west:
out_dir = south;
break;
}
break;
case TileType::backward_mirror:
switch (in_dir) {
case AbsDirection::north:
out_dir = AbsDirection::west;
case north:
out_dir = west;
break;
case AbsDirection::south:
out_dir = AbsDirection::east;
case south:
out_dir = east;
break;
case AbsDirection::east:
out_dir = AbsDirection::south;
case east:
out_dir = south;
break;
case AbsDirection::west:
out_dir = AbsDirection::north;
case west:
out_dir = north;
break;
}
break;
Expand Down

0 comments on commit 6b89ff7

Please sign in to comment.