Compare commits
5 Commits
0eaa4648f0
...
b039579f00
Author | SHA1 | Date |
---|---|---|
Bauke | b039579f00 | |
Bauke | 7647cd8487 | |
Bauke | d92fef6411 | |
Bauke | f4cb00ba69 | |
Bauke | 32008c4408 |
|
@ -1,13 +1,9 @@
|
|||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
/target/
|
||||
debug/
|
||||
target/
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||
Cargo.lock
|
||||
# Code coverage results
|
||||
coverage/
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
# Advent of Code input data
|
||||
/data/
|
||||
# Advent of Code data
|
||||
aoc/
|
||||
|
|
|
@ -0,0 +1,366 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "addr2line"
|
||||
version = "0.17.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b9ecd88a8c8378ca913a680cd98f0f13ac67383d35993f86c90a70e3f137816b"
|
||||
dependencies = [
|
||||
"gimli",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "adler"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
||||
|
||||
[[package]]
|
||||
name = "advent-of-code"
|
||||
version = "0.0.0"
|
||||
dependencies = [
|
||||
"color-eyre",
|
||||
"itertools",
|
||||
"pathfinding",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||
|
||||
[[package]]
|
||||
name = "backtrace"
|
||||
version = "0.3.66"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cab84319d616cfb654d03394f38ab7e6f0919e181b1b57e1fd15e7fb4077d9a7"
|
||||
dependencies = [
|
||||
"addr2line",
|
||||
"cc",
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"miniz_oxide",
|
||||
"object",
|
||||
"rustc-demangle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.0.73"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "color-eyre"
|
||||
version = "0.6.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5a667583cca8c4f8436db8de46ea8233c42a7d9ae424a82d338f2e4675229204"
|
||||
dependencies = [
|
||||
"backtrace",
|
||||
"color-spantrace",
|
||||
"eyre",
|
||||
"indenter",
|
||||
"once_cell",
|
||||
"owo-colors",
|
||||
"tracing-error",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "color-spantrace"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1ba75b3d9449ecdccb27ecbc479fdc0b87fa2dd43d2f8298f9bf0e59aacc8dce"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
"owo-colors",
|
||||
"tracing-core",
|
||||
"tracing-error",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
|
||||
|
||||
[[package]]
|
||||
name = "eyre"
|
||||
version = "0.6.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4c2b6b5a29c02cdc822728b7d7b8ae1bab3e3b05d44522770ddd49722eeac7eb"
|
||||
dependencies = [
|
||||
"indenter",
|
||||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fixedbitset"
|
||||
version = "0.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
|
||||
|
||||
[[package]]
|
||||
name = "gimli"
|
||||
version = "0.26.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "22030e2c5a68ec659fde1e949a745124b48e6fa8b045b7ed5bd1fe4ccc5c4e5d"
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.12.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
|
||||
|
||||
[[package]]
|
||||
name = "indenter"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683"
|
||||
|
||||
[[package]]
|
||||
name = "indexmap"
|
||||
version = "1.9.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"hashbrown",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "integer-sqrt"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "276ec31bcb4a9ee45f58bec6f9ec700ae4cf4f4f8f2fa7e06cb406bd5ffdd770"
|
||||
dependencies = [
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.10.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.134"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "329c933548736bc49fd575ee68c89e8be4d260064184389a5b77517cddd99ffb"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
|
||||
|
||||
[[package]]
|
||||
name = "miniz_oxide"
|
||||
version = "0.5.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "96590ba8f175222643a85693f33d26e9c8a015f599c216509b1a6894af675d34"
|
||||
dependencies = [
|
||||
"adler",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "object"
|
||||
version = "0.29.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "21158b2c33aa6d4561f1c0a6ea283ca92bc54802a93b263e910746d679a7eb53"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e82dad04139b71a90c080c8463fe0dc7902db5192d939bd0950f074d014339e1"
|
||||
|
||||
[[package]]
|
||||
name = "owo-colors"
|
||||
version = "3.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f"
|
||||
|
||||
[[package]]
|
||||
name = "pathfinding"
|
||||
version = "3.0.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "84da3eab6c7638931f4876ebb03455be74db8eab1e344cd5a90daba8b3ad2f22"
|
||||
dependencies = [
|
||||
"fixedbitset",
|
||||
"indexmap",
|
||||
"integer-sqrt",
|
||||
"itertools",
|
||||
"num-traits",
|
||||
"rustc-hash",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pin-project-lite"
|
||||
version = "0.2.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.46"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "94e2ef8dbfc347b10c094890f778ee2e36ca9bb4262e86dc99cd217e35f3470b"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc-demangle"
|
||||
version = "0.1.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342"
|
||||
|
||||
[[package]]
|
||||
name = "rustc-hash"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
|
||||
|
||||
[[package]]
|
||||
name = "sharded-slab"
|
||||
version = "0.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.101"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e90cde112c4b9690b8cbe810cba9ddd8bc1d7472e2cae317b69e9438c1cba7d2"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.37"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "10deb33631e3c9018b9baf9dcbbc4f737320d2b576bac10f6aefa048fa407e3e"
|
||||
dependencies = [
|
||||
"thiserror-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror-impl"
|
||||
version = "1.0.37"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "982d17546b47146b28f7c22e3d08465f6b8903d0ea13c1660d9d84a6e7adcdbb"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thread_local"
|
||||
version = "1.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5516c27b78311c50bf42c071425c560ac799b11c30b31f87e3081965fe5e0180"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing"
|
||||
version = "0.1.36"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2fce9567bd60a67d08a16488756721ba392f24f29006402881e43b19aac64307"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"pin-project-lite",
|
||||
"tracing-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-core"
|
||||
version = "0.1.29"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5aeea4303076558a00714b823f9ad67d58a3bbda1df83d8827d21193156e22f7"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
"valuable",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-error"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d686ec1c0f384b1277f097b2f279a2ecc11afe8c133c1aabf036a27cb4cd206e"
|
||||
dependencies = [
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-subscriber"
|
||||
version = "0.3.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "60db860322da191b40952ad9affe65ea23e7dd6a5c442c2c42865810c6ab8e6b"
|
||||
dependencies = [
|
||||
"sharded-slab",
|
||||
"thread_local",
|
||||
"tracing-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dcc811dc4066ac62f84f11307873c4850cb653bfa9b1719cee2bd2204a4bc5dd"
|
||||
|
||||
[[package]]
|
||||
name = "valuable"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
|
15
Cargo.toml
15
Cargo.toml
|
@ -1,15 +1,20 @@
|
|||
# https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[package]
|
||||
name = "advent-of-code-2021"
|
||||
version = "0.1.0"
|
||||
name = "advent-of-code"
|
||||
description = "Advent of Code solutions in Rust."
|
||||
repository = "https://git.bauke.xyz/Bauke/advent-of-code"
|
||||
license="Unlicense"
|
||||
version = "0.0.0"
|
||||
authors = ["Bauke <me@bauke.xyz>"]
|
||||
edition = "2021"
|
||||
publish = false
|
||||
|
||||
[[bin]]
|
||||
name = "advent-of-code-2021"
|
||||
name = "advent-of-code"
|
||||
path = "source/main.rs"
|
||||
|
||||
[dependencies]
|
||||
color-eyre = "0.5.11"
|
||||
itertools = "0.10.1"
|
||||
color-eyre = "0.6.2"
|
||||
itertools = "0.10.5"
|
||||
pathfinding = "3.0.13"
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
This is free and unencumbered software released into the public domain.
|
||||
|
||||
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
|
||||
|
||||
In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
For more information, please refer to <https://unlicense.org/>
|
|
@ -0,0 +1,39 @@
|
|||
[tasks.fmt]
|
||||
command = "cargo"
|
||||
args = ["fmt", "${@}"]
|
||||
|
||||
[tasks.check]
|
||||
command = "cargo"
|
||||
args = ["check", "${@}"]
|
||||
|
||||
[tasks.clippy]
|
||||
command = "cargo"
|
||||
args = ["clippy", "${@}"]
|
||||
|
||||
[tasks.test]
|
||||
command = "cargo"
|
||||
args = ["test", "${@}"]
|
||||
|
||||
[tasks.doc]
|
||||
command = "cargo"
|
||||
args = ["doc", "${@}"]
|
||||
|
||||
[tasks.build]
|
||||
command = "cargo"
|
||||
args = ["build", "${@}"]
|
||||
|
||||
[tasks.complete-check]
|
||||
dependencies = ["fmt", "check", "clippy", "test", "doc", "build"]
|
||||
|
||||
[tasks.code-coverage]
|
||||
workspace = false
|
||||
install_crate = "cargo-tarpaulin"
|
||||
command = "cargo"
|
||||
args = [
|
||||
"tarpaulin",
|
||||
"--exclude-files=target/*",
|
||||
"--out=html",
|
||||
"--output-dir=coverage",
|
||||
"--skip-clean",
|
||||
"--target-dir=target/tarpaulin"
|
||||
]
|
|
@ -0,0 +1,7 @@
|
|||
# Advent of Code 🎄
|
||||
|
||||
> **Advent of Code solutions in Rust.**
|
||||
|
||||
## License
|
||||
|
||||
Distributed under the [Unlicense](https://spdx.org/licenses/Unlicense.html) license, see [LICENSE](https://git.bauke.xyz/Bauke/advent-of-code/src/branch/main/LICENSE) for more information.
|
|
@ -0,0 +1,69 @@
|
|||
use std::{collections::HashSet, fmt::Display};
|
||||
|
||||
#[derive(Debug, Hash, PartialEq, Eq)]
|
||||
pub struct Point(pub isize, pub isize);
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Fold {
|
||||
Horizontal(isize),
|
||||
Vertical(isize),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Canvas {
|
||||
pub width: isize,
|
||||
pub height: isize,
|
||||
pub points: HashSet<Point>,
|
||||
}
|
||||
|
||||
impl Canvas {
|
||||
pub fn fold(self, fold: &Fold) -> Self {
|
||||
let (width, height) = match fold {
|
||||
Fold::Horizontal(amount) => (self.width, *amount),
|
||||
Fold::Vertical(amount) => (*amount, self.height),
|
||||
};
|
||||
|
||||
let mut points = HashSet::new();
|
||||
for Point(x, y) in self.points {
|
||||
if y > height {
|
||||
points.insert(Point(x, height - (y - height)));
|
||||
continue;
|
||||
}
|
||||
|
||||
if x > width {
|
||||
points.insert(Point(width - (x - width), y));
|
||||
continue;
|
||||
}
|
||||
|
||||
points.insert(Point(x, y));
|
||||
}
|
||||
|
||||
Self {
|
||||
width,
|
||||
height,
|
||||
points,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Canvas {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
for y in 0..self.height {
|
||||
for x in 0..self.width {
|
||||
if x % (self.width / 8) == 0 {
|
||||
write!(f, " ")?;
|
||||
}
|
||||
|
||||
if self.points.contains(&Point(x, y)) {
|
||||
write!(f, "█")?;
|
||||
} else {
|
||||
write!(f, " ")?;
|
||||
}
|
||||
}
|
||||
|
||||
writeln!(f)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
use std::collections::HashSet;
|
||||
|
||||
use color_eyre::{eyre::eyre, Result};
|
||||
use itertools::Itertools;
|
||||
|
||||
mod canvas;
|
||||
|
||||
use canvas::{Canvas, Fold, Point};
|
||||
|
||||
pub fn solve() -> Result<()> {
|
||||
let input_data = include_str!("../../data/day_13.txt").trim();
|
||||
println!("Day 13 Part 1: {}", part_1(input_data)?);
|
||||
println!("Day 13 Part 2:\n{}", part_2(input_data)?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse(input: &str) -> Result<(Canvas, Vec<Fold>)> {
|
||||
let mut canvas = Canvas {
|
||||
width: 0,
|
||||
height: 0,
|
||||
points: HashSet::new(),
|
||||
};
|
||||
|
||||
let mut in_folds_section = false;
|
||||
let mut folds = vec![];
|
||||
|
||||
for line in input.lines() {
|
||||
if line == "" {
|
||||
in_folds_section = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if in_folds_section {
|
||||
let mut split = line.split("=");
|
||||
let is_x_axis = split
|
||||
.next()
|
||||
.map(|s| s.ends_with("x"))
|
||||
.ok_or_else(|| eyre!("Invalid line: {}", line))?;
|
||||
|
||||
let amount = split
|
||||
.next()
|
||||
.map(str::parse)
|
||||
.ok_or_else(|| eyre!("Expected number: {}", line))??;
|
||||
|
||||
if is_x_axis {
|
||||
folds.push(Fold::Vertical(amount));
|
||||
} else {
|
||||
folds.push(Fold::Horizontal(amount));
|
||||
}
|
||||
} else {
|
||||
let (x, y) = line
|
||||
.split(",")
|
||||
.tuples()
|
||||
.next()
|
||||
.ok_or_else(|| eyre!("Invalid line: {}", line))?;
|
||||
|
||||
let x = x.parse()?;
|
||||
let y = y.parse()?;
|
||||
if canvas.width < x {
|
||||
canvas.width = x;
|
||||
}
|
||||
|
||||
if canvas.height < y {
|
||||
canvas.height = y;
|
||||
}
|
||||
|
||||
canvas.points.insert(Point(x, y));
|
||||
}
|
||||
}
|
||||
|
||||
Ok((canvas, folds))
|
||||
}
|
||||
|
||||
fn part_1(input: &str) -> Result<usize> {
|
||||
let (mut canvas, folds) = parse(input)?;
|
||||
canvas = canvas.fold(&folds[0]);
|
||||
Ok(canvas.points.into_iter().count())
|
||||
}
|
||||
|
||||
fn part_2(input: &str) -> Result<String> {
|
||||
let (mut canvas, folds) = parse(input)?;
|
||||
|
||||
for fold in folds {
|
||||
canvas = canvas.fold(&fold);
|
||||
}
|
||||
|
||||
Ok(format!("{}", canvas))
|
||||
}
|
|
@ -0,0 +1,97 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use color_eyre::{eyre::eyre, Result};
|
||||
use itertools::Itertools;
|
||||
|
||||
type PairMap = HashMap<(char, char), char>;
|
||||
type PairCounts = HashMap<(char, char), isize>;
|
||||
|
||||
pub fn solve() -> Result<()> {
|
||||
let input_data = include_str!("../../data/day_14.txt").trim();
|
||||
println!("Day 14 Part 1: {}", part_1(input_data)?);
|
||||
println!("Day 14 Part 2: {}", part_2(input_data)?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse(input: &str) -> Result<(String, PairMap)> {
|
||||
let mut lines = input.lines();
|
||||
let template = lines
|
||||
.next()
|
||||
.ok_or_else(|| eyre!("Invalid input: {}", input))?
|
||||
.to_string();
|
||||
|
||||
let mut pairs = HashMap::new();
|
||||
|
||||
for line in lines.skip(1) {
|
||||
let (a, b, c) = line
|
||||
.replace(" -> ", "")
|
||||
.chars()
|
||||
.collect_tuple()
|
||||
.ok_or_else(|| eyre!("Invalid line: {}", line))?;
|
||||
|
||||
pairs.insert((a, b), c);
|
||||
}
|
||||
|
||||
Ok((template, pairs))
|
||||
}
|
||||
|
||||
fn apply(counts: &PairCounts, pairs: &PairMap) -> PairCounts {
|
||||
let mut to_add = PairCounts::new();
|
||||
|
||||
for (pair, count) in counts {
|
||||
if let Some(character) = pairs.get(pair) {
|
||||
*to_add.entry((pair.0, *character)).or_default() += count;
|
||||
*to_add.entry((*character, pair.1)).or_default() += count;
|
||||
}
|
||||
}
|
||||
|
||||
to_add
|
||||
}
|
||||
|
||||
fn count_totals(counts: &PairCounts) -> HashMap<char, isize> {
|
||||
let mut totals = HashMap::new();
|
||||
|
||||
for ((_, b), count) in counts {
|
||||
// If I count the first character in the pair the end result ends up being
|
||||
// off by one...?
|
||||
*totals.entry(*b).or_default() += count;
|
||||
}
|
||||
|
||||
totals
|
||||
}
|
||||
|
||||
fn run(input: &str, steps: isize) -> Result<isize> {
|
||||
let (template, pairs) = parse(input)?;
|
||||
|
||||
let mut counts = PairCounts::new();
|
||||
for (a, b) in template.chars().tuple_windows() {
|
||||
*counts.entry((a, b)).or_default() += 1;
|
||||
}
|
||||
|
||||
for _ in 0..steps {
|
||||
counts = apply(&counts, &pairs);
|
||||
}
|
||||
|
||||
let totals = count_totals(&counts)
|
||||
.into_iter()
|
||||
.sorted_by(|(_, a), (_, b)| a.cmp(b));
|
||||
|
||||
let (_, min) = totals
|
||||
.clone()
|
||||
.next()
|
||||
.ok_or_else(|| eyre!("No minimum found"))?;
|
||||
let (_, max) = totals
|
||||
.clone()
|
||||
.last()
|
||||
.ok_or_else(|| eyre!("No maximum found"))?;
|
||||
|
||||
Ok(max - min)
|
||||
}
|
||||
|
||||
fn part_1(input: &str) -> Result<isize> {
|
||||
run(input, 10)
|
||||
}
|
||||
|
||||
fn part_2(input: &str) -> Result<isize> {
|
||||
run(input, 40)
|
||||
}
|
|
@ -0,0 +1,105 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use color_eyre::{eyre::eyre, Result};
|
||||
use pathfinding::prelude::{absdiff, astar};
|
||||
|
||||
pub fn solve() -> Result<()> {
|
||||
let input_data = include_str!("../../data/day_15.txt").trim();
|
||||
println!("Day 15 Part 1: {}", part_1(input_data)?);
|
||||
println!("Day 15 Part 2: {}", part_2(input_data)?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
type Grid = HashMap<Coordinate, isize>;
|
||||
|
||||
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
||||
struct Coordinate(isize, isize);
|
||||
|
||||
impl Coordinate {
|
||||
fn distance(&self, target: &Coordinate) -> isize {
|
||||
absdiff(self.0, target.0) + absdiff(self.1, target.1)
|
||||
}
|
||||
|
||||
fn successors(&self, grid: &Grid) -> Vec<(Coordinate, isize)> {
|
||||
let &Coordinate(x, y) = self;
|
||||
let mut successors = vec![];
|
||||
|
||||
for coordinate in [
|
||||
Coordinate(x, y - 1), // Up
|
||||
Coordinate(x - 1, y), // Left
|
||||
Coordinate(x, y + 1), // Down
|
||||
Coordinate(x + 1, y), // Right
|
||||
] {
|
||||
if let Some(value) = grid.get(&coordinate) {
|
||||
successors.push((coordinate, *value));
|
||||
}
|
||||
}
|
||||
|
||||
successors
|
||||
}
|
||||
}
|
||||
|
||||
fn parse(input: &str) -> Result<(Grid, Coordinate)> {
|
||||
let mut grid = Grid::new();
|
||||
let mut end = Coordinate(0, 0);
|
||||
|
||||
for (y, line) in input.lines().enumerate() {
|
||||
let y = (y + 1).try_into()?;
|
||||
|
||||
for (x, character) in line.char_indices() {
|
||||
let x = (x + 1).try_into()?;
|
||||
grid.insert(Coordinate(x, y), character.to_string().parse()?);
|
||||
|
||||
if end.0 < x && end.1 < y {
|
||||
end = Coordinate(x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok((grid, end))
|
||||
}
|
||||
|
||||
fn enlarge_grid(grid: Grid, end: Coordinate) -> (Grid, Coordinate) {
|
||||
let Coordinate(width, height) = end;
|
||||
let mut larger_grid = grid.clone();
|
||||
|
||||
for (Coordinate(x, y), risk) in grid {
|
||||
for step_x in 0..5 {
|
||||
for step_y in 0..5 {
|
||||
let risk = match (risk + step_x + step_y) % 9 {
|
||||
0 => 9,
|
||||
n => n,
|
||||
};
|
||||
let x = x + (width * step_x);
|
||||
let y = y + (height * step_y);
|
||||
larger_grid.insert(Coordinate(x, y), risk);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
(larger_grid, Coordinate(width * 5, height * 5))
|
||||
}
|
||||
|
||||
fn run(grid: Grid, end: Coordinate) -> Result<isize> {
|
||||
Ok(
|
||||
astar(
|
||||
&Coordinate(1, 1),
|
||||
|p| p.successors(&grid),
|
||||
|p| p.distance(&end),
|
||||
|p| p == &end,
|
||||
)
|
||||
.ok_or_else(|| eyre!("No path found"))?
|
||||
.1,
|
||||
)
|
||||
}
|
||||
|
||||
fn part_1(input: &str) -> Result<isize> {
|
||||
let (grid, end) = parse(input)?;
|
||||
run(grid, end)
|
||||
}
|
||||
|
||||
fn part_2(input: &str) -> Result<isize> {
|
||||
let (grid, end) = parse(input)?;
|
||||
let (grid, end) = enlarge_grid(grid, end);
|
||||
run(grid, end)
|
||||
}
|
|
@ -12,6 +12,9 @@ mod day_07;
|
|||
mod day_08;
|
||||
mod day_09;
|
||||
mod day_10;
|
||||
mod day_13;
|
||||
mod day_14;
|
||||
mod day_15;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
color_eyre::install()?;
|
||||
|
@ -29,6 +32,9 @@ fn main() -> Result<()> {
|
|||
day_08::solve,
|
||||
day_09::solve,
|
||||
day_10::solve,
|
||||
day_13::solve,
|
||||
day_14::solve,
|
||||
day_15::solve,
|
||||
];
|
||||
|
||||
for day in days {
|
||||
|
|
Loading…
Reference in New Issue