-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Day 13 2021, both stars ⭐ :star_2: Rust and TypeScript
- Loading branch information
Showing
3 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
authors = ["Joseph Chamochumbi <[email protected]>"] | ||
edition = "2018" | ||
name = "day-13" | ||
version = "0.1.0" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
[dependencies] | ||
aoc = {git = "https://github.com/icyJoseph/advent-of-code.git"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
use aoc; | ||
|
||
use std::collections::HashSet; | ||
|
||
#[derive(Hash, PartialEq, Eq)] | ||
struct Point { | ||
x: usize, | ||
y: usize, | ||
} | ||
|
||
impl Point { | ||
fn fold(&mut self, dir: &str, at: usize) { | ||
match dir { | ||
"x" => { | ||
self.x = if self.x > at { 2 * at - self.x } else { self.x }; | ||
} | ||
"y" => { | ||
self.y = if self.y > at { 2 * at - self.y } else { self.y }; | ||
} | ||
_ => {} | ||
} | ||
} | ||
} | ||
|
||
fn solve(raw: String) -> () { | ||
let spec = raw.trim().split("\n\n").collect::<Vec<&str>>(); | ||
|
||
let mut dots = spec[0] | ||
.split("\n") | ||
.map(|row| { | ||
let coords = row | ||
.split(",") | ||
.map(|c| parse_num::<usize>(c)) | ||
.collect::<Vec<usize>>(); | ||
|
||
Point { | ||
x: coords[0], | ||
y: coords[1], | ||
} | ||
}) | ||
.collect::<Vec<Point>>(); | ||
|
||
let instructions = spec[1].split("\n"); | ||
|
||
let mut part_one = true; | ||
|
||
for entry in instructions { | ||
let spec = entry.replace("fold along ", ""); | ||
let inst = spec.split("=").collect::<Vec<&str>>(); | ||
|
||
let dir = inst[0]; | ||
let at = parse_num::<usize>(inst[1]); | ||
|
||
dots.iter_mut().for_each(|p: &mut Point| p.fold(dir, at)); | ||
|
||
if part_one { | ||
let mut visible: HashSet<&Point> = HashSet::new(); | ||
|
||
dots.iter().for_each(|p| { | ||
visible.insert(p); | ||
}); | ||
|
||
println!("Part Two: {}", visible.len()); | ||
|
||
part_one = false | ||
} | ||
} | ||
|
||
let width = dots.iter().max_by(|p, q| p.x.cmp(&q.x)).unwrap().x; | ||
|
||
let height = dots.iter().max_by(|p, q| p.y.cmp(&q.y)).unwrap().y; | ||
|
||
let mut board = vec![vec![' '; width + 1]; height + 1]; | ||
|
||
dots.iter().for_each(|p| board[p.y][p.x] = '#'); | ||
|
||
println!("Part Two:"); | ||
|
||
for row in board { | ||
println!("{}", string_vec(&row, "")); | ||
} | ||
} | ||
|
||
fn main() { | ||
let input = aoc::get_input(2021, 13); | ||
|
||
solve(input); | ||
} | ||
|
||
// Utilities | ||
fn parse_num<T: std::str::FromStr>(str: &str) -> T { | ||
match str.trim().parse::<T>() { | ||
Ok(n) => n, | ||
_ => panic!("Error parsing"), | ||
} | ||
} | ||
|
||
fn string_vec<T: std::string::ToString>(vec: &Vec<T>, separator: &str) -> String { | ||
vec.iter() | ||
.map(|x| x.to_string()) | ||
.collect::<Vec<String>>() | ||
.join(separator) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const input = await Deno.readTextFile("./input/day-13.in"); | ||
// const input = await Deno.readTextFile("./input/example.in"); | ||
|
||
const [dots, instructions] = input.split("\n\n"); | ||
|
||
const coords = dots | ||
.split("\n") | ||
.map((row) => { | ||
const [x, y] = row.split(","); | ||
return { x: Number(x), y: Number(y) }; | ||
}) | ||
.flat(1); | ||
|
||
const foldV = (xf: number, grid: { x: number; y: number }[]) => { | ||
return grid.map(({ x, y }) => ({ | ||
y, | ||
x: x > xf ? 2 * xf - x : x | ||
})); | ||
}; | ||
|
||
const foldH = (yf: number, grid: { x: number; y: number }[]) => { | ||
return grid.map(({ x, y }) => ({ | ||
x, | ||
y: y > yf ? 2 * yf - y : y | ||
})); | ||
}; | ||
|
||
/** | ||
* Part Two | ||
*/ | ||
|
||
const print = (grid: { x: number; y: number }[]) => { | ||
const width = 1 + Math.max(...grid.map(({ x }) => x)); | ||
const height = 1 + Math.max(...grid.map(({ y }) => y)); | ||
|
||
const board = Array.from({ length: height }, () => | ||
Array.from({ length: width }, () => " ") | ||
); | ||
|
||
grid.forEach(({ x, y }) => { | ||
board[y][x] = "#"; | ||
}); | ||
|
||
for (const row of board) { | ||
console.log(row.join("")); | ||
} | ||
}; | ||
|
||
let it = 0; | ||
let grid = coords.slice(0); | ||
|
||
for (const inst of instructions.split("\n")) { | ||
const [dir, where] = inst.replace("fold along ", "").split("="); | ||
|
||
const fn = dir === "x" ? foldV : foldH; | ||
|
||
grid = fn(Number(where), grid); | ||
|
||
if (it === 0) { | ||
const visible: Set<string> = new Set(); | ||
grid.forEach(({ x, y }) => visible.add(`${x},${y}`)); | ||
|
||
/** | ||
* Part One | ||
*/ | ||
console.log("Part One:", visible.size); | ||
} | ||
|
||
it += 1; | ||
} | ||
|
||
console.log("Part Two:"); | ||
print(grid); |