diff --git a/2021/day-13/Cargo.toml b/2021/day-13/Cargo.toml new file mode 100644 index 0000000..7f53c1c --- /dev/null +++ b/2021/day-13/Cargo.toml @@ -0,0 +1,9 @@ +[package] +authors = ["Joseph Chamochumbi "] +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"} diff --git a/2021/day-13/src/main.rs b/2021/day-13/src/main.rs new file mode 100644 index 0000000..762627c --- /dev/null +++ b/2021/day-13/src/main.rs @@ -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::>(); + + let mut dots = spec[0] + .split("\n") + .map(|row| { + let coords = row + .split(",") + .map(|c| parse_num::(c)) + .collect::>(); + + Point { + x: coords[0], + y: coords[1], + } + }) + .collect::>(); + + 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::>(); + + let dir = inst[0]; + let at = parse_num::(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(str: &str) -> T { + match str.trim().parse::() { + Ok(n) => n, + _ => panic!("Error parsing"), + } +} + +fn string_vec(vec: &Vec, separator: &str) -> String { + vec.iter() + .map(|x| x.to_string()) + .collect::>() + .join(separator) +} diff --git a/2021/deno/day-13.ts b/2021/deno/day-13.ts new file mode 100644 index 0000000..f7a50f1 --- /dev/null +++ b/2021/deno/day-13.ts @@ -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 = 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);