Initial commit

This commit is contained in:
Stanislav Nikolov 2022-08-05 20:45:35 +03:00
commit 610d0d2475
63 changed files with 9633 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/target
/.idea/
/.vscode/

194
Cargo.lock generated Normal file
View File

@ -0,0 +1,194 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aho-corasick"
version = "0.7.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
dependencies = [
"memchr",
]
[[package]]
name = "aoc-2021"
version = "0.1.0"
dependencies = [
"bitvec",
"regex",
"serde",
"serde_yaml",
]
[[package]]
name = "autocfg"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]]
name = "bitvec"
version = "0.22.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5237f00a8c86130a0cc317830e558b966dd7850d48a953d998c813f01a41b527"
dependencies = [
"funty",
"radium",
"tap",
"wyz",
]
[[package]]
name = "funty"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1847abb9cb65d566acd5942e94aea9c8f547ad02c98e1649326fc0e8910b8b1e"
[[package]]
name = "hashbrown"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
[[package]]
name = "indexmap"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc633605454125dec4b66843673f01c7df2b89479b32e0ed634e43a91cff62a5"
dependencies = [
"autocfg",
"hashbrown",
]
[[package]]
name = "linked-hash-map"
version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3"
[[package]]
name = "memchr"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
[[package]]
name = "proc-macro2"
version = "1.0.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f84e92c0f7c9d58328b85a78557813e4bd845130db68d7184635344399423b1"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05"
dependencies = [
"proc-macro2",
]
[[package]]
name = "radium"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "643f8f41a8ebc4c5dc4515c82bb8abd397b527fc20fd681b7c011c2aee5d44fb"
[[package]]
name = "regex"
version = "1.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
[[package]]
name = "ryu"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f"
[[package]]
name = "serde"
version = "1.0.132"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b9875c23cf305cd1fd7eb77234cbb705f21ea6a72c637a5c6db5fe4b8e7f008"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.132"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ecc0db5cb2556c0e558887d9bbdcf6ac4471e83ff66cf696e5419024d1606276"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_yaml"
version = "0.8.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4a521f2940385c165a24ee286aa8599633d162077a54bdcae2a6fd5a7bfa7a0"
dependencies = [
"indexmap",
"ryu",
"serde",
"yaml-rust",
]
[[package]]
name = "syn"
version = "1.0.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8daf5dd0bb60cbd4137b1b587d2fc0ae729bc07cf01cd70b36a1ed5ade3b9d59"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "tap"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
[[package]]
name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "wyz"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "129e027ad65ce1453680623c3fb5163cbf7107bfe1aa32257e7d0e63f9ced188"
dependencies = [
"tap",
]
[[package]]
name = "yaml-rust"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85"
dependencies = [
"linked-hash-map",
]

12
Cargo.toml Normal file
View File

@ -0,0 +1,12 @@
[package]
name = "aoc-2021"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bitvec = "0.22.3"
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.8"
regex = "1"

19
LICENSE Normal file
View File

@ -0,0 +1,19 @@
Copyright (c) 2022 Stanislav Nikolov <hello@snikolov.me>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
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 OR COPYRIGHT HOLDERS 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.

19
src/lib.rs Normal file
View File

@ -0,0 +1,19 @@
pub mod t01_sonar_sweep;
pub mod t02_dive;
pub mod t03_binary_diagnostic;
pub mod t04_giant_squid;
pub mod t05_hydrothermal_venture;
pub mod t06_lanternfish;
pub mod t07_the_treachery_of_whales;
pub mod t08_seven_segment_search;
pub mod t09_smoke_basin;
pub mod t10_syntax_scoring;
pub mod t11_dumbo_octopus;
pub mod t12_passage_passing;
pub mod t13_transparent_origami;
pub mod t14_extended_polymerisation;
pub mod t15_chiton;
pub mod t16_packet_decoder;
pub mod t17_trick_shot;
pub mod t18_snailfish;
pub mod types;

81
src/main.rs Normal file
View File

@ -0,0 +1,81 @@
use aoc_2021::{
t01_sonar_sweep::{sonar_sweep_1, sonar_sweep_2},
t02_dive::{dive_1, dive_2},
t03_binary_diagnostic::{binary_diagnostic_1, binary_diagnostic_2},
t04_giant_squid::{giant_squid_1, giant_squid_2},
t05_hydrothermal_venture::{hydrothermal_venture_1, hydrothermal_venture_2},
t06_lanternfish::{lanternfish_1, lanternfish_2},
t07_the_treachery_of_whales::{the_treachery_of_whales_1, the_treachery_of_whales_2},
t08_seven_segment_search::{seven_segment_search_1, seven_segment_search_2},
t09_smoke_basin::{smoke_basin_1, smoke_basin_2},
t10_syntax_scoring::{syntax_scoring_1, syntax_scoring_2},
t11_dumbo_octopus::{dumbo_octopus_1, dumbo_octopus_2},
t12_passage_passing::{passage_passing_1, passage_passing_2},
t13_transparent_origami::{transparent_origami_1, transparent_origami_2},
t14_extended_polymerisation::{extended_polymerisation_1, extended_polymerisation_2},
t15_chiton::{chiton_1, chiton_2},
t16_packet_decoder::{packet_decoder_1, packet_decoder_2},
t17_trick_shot::{trick_shot_1, trick_shot_2},
t18_snailfish::{snailfish_1, snailfish_2},
};
use std::env;
fn main() {
let mut args = env::args();
args.next();
let task = args
.next()
.unwrap_or_else(|| panic!("Task not given!"))
.parse()
.unwrap();
let subtask = args
.next()
.unwrap_or_else(|| panic!("Subtask not given!"))
.parse()
.unwrap();
let test_mode = args
.next()
.unwrap_or_else(|| panic!("Test mode not given!"))
.parse()
.unwrap();
match (task, subtask) {
(1, 1) => sonar_sweep_1(test_mode),
(1, 2) => sonar_sweep_2(test_mode),
(2, 1) => dive_1(test_mode),
(2, 2) => dive_2(test_mode),
(3, 1) => binary_diagnostic_1(test_mode),
(3, 2) => binary_diagnostic_2(test_mode),
(4, 1) => giant_squid_1(test_mode),
(4, 2) => giant_squid_2(test_mode),
(5, 1) => hydrothermal_venture_1(test_mode),
(5, 2) => hydrothermal_venture_2(test_mode),
(6, 1) => lanternfish_1(test_mode),
(6, 2) => lanternfish_2(test_mode),
(7, 1) => the_treachery_of_whales_1(test_mode),
(7, 2) => the_treachery_of_whales_2(test_mode),
(8, 1) => seven_segment_search_1(test_mode),
(8, 2) => seven_segment_search_2(test_mode),
(9, 1) => smoke_basin_1(test_mode),
(9, 2) => smoke_basin_2(test_mode),
(10, 1) => syntax_scoring_1(test_mode),
(10, 2) => syntax_scoring_2(test_mode),
(11, 1) => dumbo_octopus_1(test_mode),
(11, 2) => dumbo_octopus_2(test_mode),
(12, 1) => passage_passing_1(test_mode),
(12, 2) => passage_passing_2(test_mode),
(13, 1) => transparent_origami_1(test_mode),
(13, 2) => transparent_origami_2(test_mode),
(14, 1) => extended_polymerisation_1(test_mode),
(14, 2) => extended_polymerisation_2(test_mode),
(15, 1) => chiton_1(test_mode),
(15, 2) => chiton_2(test_mode),
(16, 1) => packet_decoder_1(test_mode),
(16, 2) => packet_decoder_2(test_mode),
(17, 1) => trick_shot_1(test_mode),
(17, 2) => trick_shot_2(test_mode),
(18, 1) => snailfish_1(test_mode),
(18, 2) => snailfish_2(test_mode),
_ => panic!("Unknown task combination!"),
}
}

View File

@ -0,0 +1,36 @@
use std::io::{BufRead, BufReader};
use crate::types::TestMode;
fn parse_input(test_mode: TestMode) -> Vec<u64> {
let f = test_mode.input_file(file!());
BufReader::new(f)
.lines()
.map(|line| line.unwrap().parse().unwrap())
.collect()
}
pub fn sonar_sweep_1(test_mode: TestMode) {
let nums = parse_input(test_mode);
let mut count = 0;
for i in 1..nums.len() {
if nums[i] > nums[i - 1] {
count += 1;
}
}
println!("{}", count)
}
pub fn sonar_sweep_2(test_mode: TestMode) {
let nums = parse_input(test_mode);
const WINDOW: usize = 3;
let mut count = 0u64;
for i in WINDOW..nums.len() {
if nums[i - WINDOW + 1..i + 1].iter().sum::<u64>() > nums[i - WINDOW..i].iter().sum() {
count += 1;
}
}
println!("{}", count)
}

View File

@ -0,0 +1,10 @@
199
200
208
210
200
207
240
269
260
263

2000
src/t01_sonar_sweep/test.in Normal file

File diff suppressed because it is too large Load Diff

79
src/t02_dive/mod.rs Normal file
View File

@ -0,0 +1,79 @@
use std::{
io::{BufRead, BufReader},
str::FromStr,
};
use crate::types::TestMode;
struct DirectionChange {
direction: Direction,
units: u32,
}
enum Direction {
Forward,
Up,
Down,
}
impl FromStr for Direction {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"forward" => Ok(Direction::Forward),
"up" => Ok(Direction::Up),
"down" => Ok(Direction::Down),
_ => Err(format!("Invalid value: {:?}", s)),
}
}
}
fn parse_input(test_mode: TestMode) -> Vec<DirectionChange> {
let f = test_mode.input_file(file!());
BufReader::new(f)
.lines()
.map(|line| {
let line = line.unwrap();
let mut split = line.split_ascii_whitespace();
DirectionChange {
direction: split.next().unwrap().parse().unwrap(),
units: split.next().unwrap().parse().unwrap(),
}
})
.collect()
}
pub fn dive_1(test_mode: TestMode) {
let directions = parse_input(test_mode);
let mut x = 0;
let mut y = 0;
for d in directions {
match d.direction {
Direction::Forward => x += d.units,
Direction::Up => y -= d.units,
Direction::Down => y += d.units,
}
}
println!("({},{}) -> {}", x, y, x * y)
}
pub fn dive_2(test_mode: TestMode) {
let directions = parse_input(test_mode);
let mut x = 0;
let mut y = 0;
let mut aim = 0;
for d in directions {
match d.direction {
Direction::Forward => {
x += d.units;
y += d.units * aim;
}
Direction::Up => aim -= d.units,
Direction::Down => aim += d.units,
}
}
println!("({},{}) -> {}", x, y, x * y)
}

6
src/t02_dive/prod.in Normal file
View File

@ -0,0 +1,6 @@
forward 5
down 5
forward 8
up 3
down 8
forward 2

1000
src/t02_dive/test.in Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,83 @@
use std::io::{BufRead, BufReader};
use crate::types::TestMode;
fn parse_input(test_mode: TestMode) -> Vec<String> {
let f = test_mode.input_file(file!());
BufReader::new(f)
.lines()
.map(|line| line.unwrap())
.collect()
}
fn num_symbols_at_pos(input: &[String], symbol: char, position: usize) -> usize {
input
.iter()
.filter(|s| s.chars().nth(position).unwrap() == symbol)
.count()
}
pub fn binary_diagnostic_1(test_mode: TestMode) {
let nums = parse_input(test_mode);
let mut gamma = 0;
let mut epsilon = 0;
for b in 0..nums[0].len() {
let zero_count = num_symbols_at_pos(&nums, '0', b);
let one_count = num_symbols_at_pos(&nums, '1', b);
let most_common = if one_count >= zero_count { 1 } else { 0 };
let least_common = if one_count < zero_count { 1 } else { 0 };
gamma = (gamma << 1) | most_common;
epsilon = (epsilon << 1) | least_common;
}
println!("{}, {} -> {}", gamma, epsilon, gamma * epsilon);
}
pub fn binary_diagnostic_2(test_mode: TestMode) {
let nums = parse_input(test_mode);
let mut oxygen_generator_ratings = nums.clone();
for b in 0..nums[0].len() {
if oxygen_generator_ratings.len() == 1 {
break;
};
let zero_count = num_symbols_at_pos(&oxygen_generator_ratings, '0', b);
let one_count = num_symbols_at_pos(&oxygen_generator_ratings, '1', b);
let most_common = if one_count >= zero_count { '1' } else { '0' };
oxygen_generator_ratings = oxygen_generator_ratings
.into_iter()
.filter(|s| s.chars().nth(b).unwrap() == most_common)
.collect();
}
let mut co2_scrubber_ratings = nums.clone();
for b in 0..nums[0].len() {
if co2_scrubber_ratings.len() == 1 {
break;
}
let zero_count = num_symbols_at_pos(&co2_scrubber_ratings, '0', b);
let one_count = num_symbols_at_pos(&co2_scrubber_ratings, '1', b);
let least_common = if one_count < zero_count { '1' } else { '0' };
co2_scrubber_ratings = co2_scrubber_ratings
.into_iter()
.filter(|s| s.chars().nth(b).unwrap() == least_common)
.collect();
}
let oxy_gen_rating = usize::from_str_radix(&oxygen_generator_ratings[0], 2).unwrap();
let co2_scrub_rating = usize::from_str_radix(&co2_scrubber_ratings[0], 2).unwrap();
println!(
"{}, {} -> {}",
oxy_gen_rating,
co2_scrub_rating,
oxy_gen_rating * co2_scrub_rating
);
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,12 @@
00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010

170
src/t04_giant_squid/mod.rs Normal file
View File

@ -0,0 +1,170 @@
use std::{
fs::File,
io::{BufRead, BufReader},
process::exit,
};
use crate::types::TestMode;
#[derive(Debug)]
struct Input {
numbers: Vec<u16>,
boards: Vec<Board>,
}
const BOARD_DIMENSION: usize = 5;
#[derive(Clone, Debug)]
struct Board {
cells: [[u16; BOARD_DIMENSION]; BOARD_DIMENSION],
marked_cell_map: [[bool; BOARD_DIMENSION]; BOARD_DIMENSION],
}
impl Board {
fn new() -> Self {
Self {
cells: [[0; BOARD_DIMENSION]; BOARD_DIMENSION],
marked_cell_map: [[false; BOARD_DIMENSION]; BOARD_DIMENSION],
}
}
fn from_bufreader(br: &mut BufReader<File>) -> Option<Self> {
let mut buf = String::new();
if let Ok(n) = br.read_line(&mut buf) {
if n == 0 {
return None;
}
}
let mut ret = Self::new();
for j in 0..BOARD_DIMENSION {
buf.clear();
br.read_line(&mut buf).unwrap();
let line_nums: Vec<u16> = buf
.trim_end()
.split_ascii_whitespace()
.map(|n| n.parse().unwrap())
.collect();
ret.cells[j] = line_nums[0..BOARD_DIMENSION].try_into().unwrap();
}
Some(ret)
}
fn mark_number(&mut self, number: u16) {
for i in 0..BOARD_DIMENSION {
for j in 0..BOARD_DIMENSION {
if self.cells[i][j] == number {
self.marked_cell_map[i][j] = true;
}
}
}
}
fn is_winning(&self) -> bool {
for col in 0..BOARD_DIMENSION {
let mut bingo = true;
for row in 0..BOARD_DIMENSION {
if !self.marked_cell_map[row][col] {
bingo = false;
break;
}
}
if bingo {
return true;
}
}
for row in 0..BOARD_DIMENSION {
let mut bingo = true;
for col in 0..BOARD_DIMENSION {
if !self.marked_cell_map[row][col] {
bingo = false;
break;
}
}
if bingo {
return true;
}
}
false
}
fn score(&self, last_number: u16) -> u64 {
let mut score = 0u64;
for row in 0..BOARD_DIMENSION {
for col in 0..BOARD_DIMENSION {
if !self.marked_cell_map[row][col] {
score += self.cells[row][col] as u64;
}
}
}
score * (last_number as u64)
}
}
fn parse_input(test_mode: TestMode) -> Input {
let f = test_mode.input_file(file!());
let mut buf = String::new();
let mut br = BufReader::new(f);
br.read_line(&mut buf).unwrap();
let numbers: Vec<u16> = buf
.split_terminator(',')
.into_iter()
.map(|s| s.trim_end().parse().unwrap())
.collect();
let mut boards = vec![];
loop {
let b = Board::from_bufreader(&mut br);
if let Some(b) = b {
boards.push(b);
} else {
break;
}
}
Input { numbers, boards }
}
pub fn giant_squid_1(test_mode: TestMode) {
let mut input = parse_input(test_mode);
for n in input.numbers {
for b in &mut input.boards {
b.mark_number(n);
if b.is_winning() {
println!("{}", b.score(n));
exit(0);
}
}
}
}
pub fn giant_squid_2(test_mode: TestMode) {
let mut input = parse_input(test_mode);
let mut last_to_win_board = None;
let mut last_to_win_number = None;
for n in input.numbers {
let mut new_boards = Vec::<Board>::new();
for b in &mut input.boards {
b.mark_number(n);
if b.is_winning() {
last_to_win_board = Some(b.clone());
last_to_win_number = Some(n);
} else {
new_boards.push(b.clone());
}
}
input.boards = new_boards;
}
println!(
"{}",
last_to_win_board
.unwrap()
.score(last_to_win_number.unwrap())
);
}

601
src/t04_giant_squid/prod.in Normal file
View File

@ -0,0 +1,601 @@
74,79,46,2,19,27,31,90,21,83,94,77,0,29,38,72,42,23,6,62,45,95,41,55,93,69,39,17,12,1,20,53,49,71,61,13,88,25,87,26,50,58,28,51,89,64,3,80,36,65,57,92,52,86,98,78,9,33,44,63,16,34,97,60,40,66,75,4,7,84,22,43,11,85,91,32,48,14,18,76,8,47,24,81,35,30,82,67,37,70,15,5,73,59,54,68,56,96,99,10
61 96 92 39 0
35 25 50 22 60
3 88 69 48 62
75 24 97 51 67
87 74 94 77 83
1 70 59 40 55
42 88 10 17 80
27 24 82 45 23
5 19 48 51 11
75 72 97 74 7
58 40 78 83 74
4 94 17 63 62
55 61 5 27 69
99 84 89 81 59
64 28 91 49 97
92 88 51 12 22
0 5 65 32 77
80 40 3 10 90
91 47 58 57 14
86 71 94 36 75
71 24 16 66 29
8 47 93 68 36
42 67 69 55 15
75 6 34 60 70
95 92 14 0 81
52 49 37 41 67
9 8 2 13 17
92 89 38 16 53
63 46 60 4 87
57 96 77 85 39
84 98 52 95 89
81 67 99 85 50
88 11 76 49 8
4 30 51 78 20
70 64 74 40 79
45 65 87 79 14
11 26 98 70 28
46 85 54 55 48
97 59 62 57 16
30 40 95 7 18
97 25 38 1 26
20 86 7 68 39
2 55 29 33 65
46 14 72 47 18
60 48 41 9 50
71 81 15 49 50
72 28 51 11 35
20 7 36 84 65
93 33 14 47 45
89 0 75 60 16
98 90 47 94 55
69 41 81 1 43
73 95 65 15 80
85 99 60 92 0
13 33 82 51 22
47 58 82 67 30
88 23 64 4 39
94 52 61 1 75
3 8 34 87 49
13 38 60 54 35
91 62 88 29 33
84 27 6 18 11
47 87 58 42 34
69 46 75 40 43
63 97 53 49 66
80 57 73 65 44
95 55 27 46 10
82 24 90 97 75
33 41 31 84 9
5 48 18 49 12
92 63 91 14 13
32 12 66 87 79
44 60 7 96 84
58 41 42 3 27
16 59 43 77 11
80 36 53 56 62
26 8 4 79 51
22 91 69 78 2
59 13 23 81 93
30 16 49 33 65
52 88 12 67 85
74 78 75 72 79
81 26 82 5 0
23 56 41 3 32
31 69 15 66 87
22 71 80 0 63
94 31 13 60 42
41 77 90 92 91
64 95 5 23 73
85 15 3 88 10
72 75 88 52 38
17 86 54 79 87
66 61 51 3 26
68 47 89 11 41
50 33 92 7 81
82 80 9 65 34
3 49 42 36 76
95 94 61 32 43
72 67 56 45 54
77 48 14 6 25
44 75 99 62 11
43 73 2 87 83
96 63 85 14 30
32 70 18 29 55
1 88 15 27 24
2 38 46 61 7
45 19 97 31 54
88 40 14 81 87
69 39 32 16 21
22 5 0 29 92
78 57 85 4 70
82 43 12 69 79
60 34 15 63 45
90 77 93 31 47
27 49 25 71 19
49 10 40 51 45
9 44 86 26 27
93 98 22 63 95
88 66 33 74 57
81 24 28 91 72
14 83 60 54 57
18 15 41 4 47
39 98 62 33 5
30 70 6 91 90
86 21 28 84 81
91 46 49 9 32
85 33 87 83 76
17 14 37 94 6
31 13 92 89 78
15 66 47 74 63
55 6 83 19 96
71 22 88 99 50
89 84 26 45 38
57 77 87 93 25
44 49 16 64 34
79 76 46 19 51
85 90 58 29 3
34 2 81 62 99
84 60 78 91 96
4 27 43 47 98
66 2 38 39 37
35 25 51 10 82
91 62 1 12 93
83 29 47 32 56
74 19 50 95 49
59 57 35 50 51
27 38 62 76 3
52 49 83 75 4
64 16 93 7 91
40 17 65 41 97
18 37 45 44 4
72 7 28 0 75
9 2 95 90 38
24 79 93 22 88
94 70 57 6 20
11 61 65 50 23
74 51 80 91 22
5 32 27 57 14
59 86 70 17 10
21 62 20 18 67
98 9 88 79 78
99 56 91 41 67
17 39 65 16 38
75 84 11 21 61
22 81 52 55 87
45 36 74 47 19
15 22 88 85 32
38 63 54 16 13
29 7 48 90 43
68 3 24 17 30
72 77 68 75 57
43 74 32 61 34
37 2 47 25 85
56 12 95 98 0
80 36 39 22 11
77 58 24 57 99
70 16 33 41 94
54 61 20 90 30
29 17 55 0 83
13 37 42 49 38
86 58 13 11 6
73 26 25 0 67
56 44 87 5 49
4 91 51 66 22
28 8 1 15 57
61 24 50 25 66
92 42 98 55 96
46 79 22 33 91
97 0 69 90 54
17 38 34 39 52
68 28 67 45 87
8 80 52 41 54
34 47 4 78 59
10 29 32 11 26
17 33 7 93 35
10 15 33 46 14
6 56 52 16 92
47 36 17 8 69
77 45 73 84 9
55 60 80 44 64
58 18 25 11 83
75 7 53 42 68
48 52 6 0 43
80 97 16 60 1
29 67 15 5 17
77 55 54 24 66
58 2 4 39 12
57 86 69 91 8
67 84 65 13 20
87 59 40 34 27
39 7 40 77 91
13 76 32 92 56
34 17 81 27 66
37 80 83 85 15
43 36 30 26 63
11 50 72 85 34
3 92 58 53 7
98 10 49 97 12
26 42 14 24 56
28 20 59 54 4
55 56 29 80 96
63 68 44 22 12
65 4 95 6 26
21 35 14 87 8
17 92 86 30 53
9 42 20 37 19
65 46 11 54 92
52 4 56 80 99
41 55 43 90 17
60 87 13 50 3
3 29 4 41 95
14 9 11 23 10
7 63 68 58 66
13 46 67 86 51
28 36 0 73 84
45 1 19 74 36
58 64 30 86 83
99 42 70 97 54
17 75 56 80 81
93 41 90 10 88
24 25 0 94 22
70 1 50 10 14
89 77 76 63 46
33 72 81 28 60
68 40 12 31 20
79 33 30 55 71
31 91 54 0 82
10 78 9 49 14
85 72 5 3 24
86 38 97 46 61
20 84 97 52 79
45 73 11 18 58
63 86 21 9 87
48 90 13 77 49
44 85 56 71 55
16 1 54 13 83
38 32 69 28 43
5 50 57 95 47
34 76 45 74 89
46 91 71 39 17
82 45 14 28 57
27 21 17 29 51
95 32 31 80 91
89 74 67 76 79
6 0 4 43 94
52 66 44 74 95
85 51 79 76 54
89 34 59 10 27
45 6 69 98 48
88 19 3 65 94
61 9 67 72 71
93 48 64 52 11
74 85 12 13 23
41 4 94 16 57
63 88 28 89 40
68 23 54 56 44
13 77 26 2 46
28 81 15 16 62
82 51 71 86 72
99 0 52 41 32
99 38 7 87 9
69 96 22 57 24
64 81 29 67 14
48 52 6 88 92
90 44 51 40 8
41 1 23 24 73
10 4 66 60 22
17 9 69 53 63
42 34 99 86 56
75 82 81 18 79
58 64 12 59 30
21 94 28 77 53
88 90 97 62 83
35 70 27 98 26
65 34 25 73 75
81 7 90 91 74
23 34 67 31 50
60 87 5 40 77
69 93 27 49 53
39 62 68 16 89
82 13 28 65 35
5 42 90 12 51
15 85 64 86 25
87 22 88 37 98
39 10 46 56 49
62 25 93 75 34
42 89 27 36 18
32 54 59 26 6
51 19 47 85 95
33 39 73 29 79
15 27 0 79 69
13 73 25 19 43
30 8 46 34 58
4 86 66 74 18
83 33 92 11 47
45 25 22 14 4
83 3 65 17 85
91 26 5 19 87
66 89 29 49 64
52 20 58 93 53
30 64 52 14 34
63 16 97 9 15
2 72 65 45 17
47 98 77 23 0
50 20 38 60 26
46 67 84 66 55
7 32 31 75 19
71 85 37 12 52
39 27 8 81 44
89 47 42 16 58
74 99 81 86 89
92 20 7 58 30
63 96 25 45 2
97 50 94 33 87
38 6 51 21 62
52 27 20 32 19
17 80 70 92 96
49 44 62 60 94
40 28 86 4 7
38 91 3 77 29
8 28 89 99 6
46 54 34 95 3
88 60 29 91 10
42 13 62 94 76
56 52 72 85 59
85 50 42 5 91
67 7 21 6 56
14 8 70 10 78
77 80 57 29 96
17 23 73 16 38
59 61 47 43 13
7 93 11 72 83
0 96 67 27 2
42 5 41 65 94
40 34 33 50 3
25 79 52 11 94
73 14 7 99 19
92 40 2 28 45
55 34 87 24 96
36 16 66 78 35
11 27 90 50 55
68 84 63 57 89
35 14 29 77 24
92 81 7 1 85
99 64 20 2 49
20 66 85 88 57
49 17 78 1 80
18 24 11 31 65
30 34 45 99 19
69 40 94 2 58
49 2 55 54 61
48 19 34 5 83
80 52 67 24 96
51 91 20 45 68
87 79 59 9 3
47 12 71 88 74
28 5 79 58 26
93 67 62 86 23
66 13 96 46 17
94 59 19 54 15
21 89 98 54 53
49 44 79 10 93
64 24 25 9 56
57 70 55 65 23
14 36 31 13 4
62 60 30 89 94
88 19 59 41 75
25 45 74 17 47
5 16 76 33 58
53 68 65 39 67
55 2 76 32 26
37 25 5 27 24
61 88 33 45 46
20 96 51 42 49
66 3 15 11 36
60 21 80 9 96
91 39 24 28 13
52 11 34 41 82
66 85 72 38 76
69 25 67 64 81
67 75 42 79 74
36 26 85 30 25
50 19 3 33 28
12 95 54 71 91
0 17 87 92 40
51 85 12 86 40
28 36 35 50 97
55 16 20 14 73
7 5 4 68 22
47 3 67 93 2
48 33 92 35 31
73 40 71 75 62
19 54 49 20 38
23 37 9 11 10
80 63 39 52 56
59 70 61 65 62
42 73 99 39 66
67 8 93 30 97
53 37 51 55 11
48 26 94 44 63
99 5 21 8 13
0 35 25 19 6
93 83 40 98 43
84 18 66 50 62
86 94 32 52 11
55 15 85 39 4
95 83 27 46 45
19 47 61 9 66
82 32 72 77 16
50 96 14 60 35
66 13 84 74 97
85 67 20 43 34
95 0 3 58 38
48 69 93 28 7
91 98 56 94 35
11 15 73 51 77
13 7 22 53 10
2 40 98 79 50
71 83 49 45 56
0 1 68 99 24
34 84 37 31 93
55 7 18 15 65
80 40 29 44 36
51 26 99 59 2
57 45 67 1 41
79 90 56 76 58
78 70 20 26 48
87 82 46 59 98
51 81 91 52 44
21 86 68 64 7
12 26 73 30 87
99 58 45 25 38
95 97 27 22 37
98 72 10 6 79
4 61 20 85 67
9 26 5 68 2
97 4 31 11 69
75 64 0 6 17
25 95 89 59 38
16 99 27 53 10
89 71 42 70 90
12 38 8 63 23
95 77 0 29 43
81 93 56 2 34
46 44 55 13 41
72 21 50 1 81
67 44 88 90 82
98 19 30 48 85
66 20 79 13 28
29 62 38 74 89
1 62 20 28 0
59 52 11 6 74
32 16 50 34 76
79 91 31 24 56
26 37 87 53 57
47 79 55 45 9
63 2 1 60 75
18 39 97 7 44
33 29 91 31 23
50 80 32 49 71
41 52 85 2 83
28 27 49 14 44
20 1 34 19 17
62 59 68 86 82
89 31 37 95 80
67 70 59 17 91
3 60 12 6 93
99 44 34 9 21
31 26 61 20 25
23 15 43 53 42
52 19 16 91 35
65 29 4 2 48
90 44 77 38 60
49 62 53 47 74
61 15 30 28 70
14 97 34 88 55
50 28 80 36 64
93 40 60 90 22
29 77 1 26 56
33 9 4 67 68

View File

@ -0,0 +1,19 @@
7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1
22 13 17 11 0
8 2 23 4 24
21 9 14 16 7
6 10 3 18 5
1 12 20 15 19
3 15 0 2 22
9 18 13 17 5
19 8 7 25 23
20 11 10 24 4
14 21 16 12 6
14 21 17 24 4
10 16 15 9 19
18 8 23 26 20
22 11 13 6 5
2 0 12 3 7

View File

@ -0,0 +1,143 @@
use std::{
cmp::Ordering,
collections::{HashMap, HashSet},
fmt::Display,
io::{BufRead, BufReader},
};
use crate::types::TestMode;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
struct Point {
x: i64,
y: i64,
}
impl Display for Point {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("({}, {})", self.x, self.y))
.unwrap();
Ok(())
}
}
#[derive(Debug, PartialEq, Eq, Hash)]
struct Line {
p1: Point,
p2: Point,
}
impl Display for Line {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("[{} -> {}]", self.p1, self.p2))
.unwrap();
Ok(())
}
}
impl Line {
fn points(&self) -> HashSet<Point> {
let mut res = HashSet::new();
let dx = Self::sign(self.p2.x - self.p1.x);
let dy = Self::sign(self.p2.y - self.p1.y);
let mut x = self.p1.x;
let mut y = self.p1.y;
loop {
let p = Point { x, y };
res.insert(p);
if x == self.p2.x && y == self.p2.y {
break;
}
x += dx;
y += dy;
}
res
}
fn sign(expr: i64) -> i64 {
match expr.cmp(&0) {
Ordering::Less => -1,
Ordering::Equal => 0,
Ordering::Greater => 1,
}
}
}
struct Input {
lines: Vec<Line>,
}
fn parse_input(test_mode: TestMode) -> Input {
let f = test_mode.input_file(file!());
let br = BufReader::new(f);
let lines = br
.lines()
.map(|line| {
let line = line.unwrap();
let mut s = line.split_terminator(" -> ");
let mut m = s.next().unwrap().split_terminator(',');
let p1 = Point {
x: m.next().unwrap().parse().unwrap(),
y: m.next().unwrap().parse().unwrap(),
};
let mut m = s.next().unwrap().split_terminator(',');
let p2 = Point {
x: m.next().unwrap().parse().unwrap(),
y: m.next().unwrap().parse().unwrap(),
};
Line { p1, p2 }
})
.collect();
Input { lines }
}
pub fn hydrothermal_venture_1(test_mode: TestMode) {
let input = parse_input(test_mode);
let orthogonal_lines: Vec<_> = input
.lines
.into_iter()
.filter(|line| line.p1.x == line.p2.x || line.p1.y == line.p2.y)
.collect();
let mut point_count = HashMap::<Point, i64>::new();
for line in orthogonal_lines {
for point in line.points() {
*point_count.entry(point).or_insert(0) += 1;
}
}
let num_intersections = point_count
.iter()
.filter(|point_usages| point_usages.1 > &1)
.count();
println!("{}", num_intersections);
}
pub fn hydrothermal_venture_2(test_mode: TestMode) {
let lines = parse_input(test_mode).lines;
let mut point_count = HashMap::<Point, i64>::new();
for line in lines {
for point in line.points() {
*point_count.entry(point).or_insert(0) += 1;
}
}
let num_intersections = point_count
.iter()
.filter(|point_usages| point_usages.1 > &1)
.count();
println!("{}", num_intersections);
}

View File

@ -0,0 +1,500 @@
541,808 -> 108,808
982,23 -> 45,960
558,21 -> 558,318
907,877 -> 43,13
532,213 -> 532,801
599,387 -> 870,387
762,208 -> 78,208
739,527 -> 739,907
64,21 -> 64,958
258,267 -> 929,938
22,75 -> 725,778
347,950 -> 347,345
705,906 -> 61,906
53,16 -> 950,913
468,474 -> 475,481
567,602 -> 914,602
570,531 -> 570,530
180,307 -> 180,823
546,374 -> 390,374
750,142 -> 861,31
586,631 -> 905,950
971,680 -> 784,680
428,174 -> 352,174
825,676 -> 228,676
630,617 -> 70,617
156,912 -> 944,124
805,203 -> 25,983
726,808 -> 726,96
986,564 -> 908,642
594,293 -> 594,458
182,126 -> 182,476
979,43 -> 35,987
642,272 -> 642,446
759,690 -> 891,690
951,518 -> 161,518
357,769 -> 336,769
904,297 -> 904,533
326,332 -> 326,316
758,356 -> 654,460
432,425 -> 432,819
31,602 -> 31,421
318,555 -> 898,555
326,220 -> 777,671
708,957 -> 708,273
26,24 -> 974,972
341,172 -> 341,394
33,926 -> 864,95
486,324 -> 486,704
850,82 -> 132,800
62,506 -> 113,506
816,429 -> 816,141
184,17 -> 184,328
40,680 -> 30,670
640,294 -> 127,807
654,512 -> 654,296
722,301 -> 629,301
255,430 -> 811,430
376,385 -> 376,379
227,207 -> 227,947
363,533 -> 757,533
150,616 -> 150,284
943,100 -> 79,964
275,963 -> 275,461
409,768 -> 409,574
516,349 -> 516,656
19,666 -> 847,666
962,358 -> 962,907
781,789 -> 781,870
778,380 -> 501,657
895,29 -> 12,912
12,677 -> 12,761
614,728 -> 690,652
415,786 -> 778,423
683,84 -> 683,574
973,909 -> 75,11
854,791 -> 260,197
965,55 -> 559,55
624,542 -> 235,153
924,57 -> 16,965
540,736 -> 540,65
293,293 -> 929,293
587,176 -> 587,432
389,126 -> 389,590
267,149 -> 92,149
424,596 -> 697,323
412,697 -> 773,697
473,579 -> 310,416
149,175 -> 837,863
848,984 -> 159,295
608,613 -> 292,613
344,970 -> 546,970
589,531 -> 589,74
220,727 -> 220,831
651,240 -> 318,240
717,609 -> 847,479
917,954 -> 327,364
780,102 -> 228,654
201,95 -> 201,921
403,88 -> 403,79
755,76 -> 755,529
654,762 -> 654,97
957,988 -> 44,75
899,43 -> 51,891
855,892 -> 175,212
295,962 -> 613,962
284,800 -> 946,800
494,771 -> 793,472
212,799 -> 212,784
25,829 -> 824,30
277,135 -> 396,135
981,986 -> 187,192
794,88 -> 308,88
425,140 -> 73,140
155,900 -> 155,500
339,768 -> 339,621
720,407 -> 824,303
746,679 -> 746,490
672,632 -> 30,632
74,628 -> 49,628
534,770 -> 249,485
59,802 -> 605,256
481,543 -> 723,301
163,425 -> 947,425
10,782 -> 779,782
185,851 -> 68,968
536,479 -> 536,217
987,472 -> 960,472
802,623 -> 202,23
548,73 -> 548,838
711,678 -> 711,655
91,578 -> 91,162
803,27 -> 31,27
198,524 -> 282,608
45,585 -> 45,973
741,157 -> 317,581
263,843 -> 819,843
240,786 -> 240,272
453,148 -> 657,148
646,74 -> 646,758
367,521 -> 367,185
355,356 -> 503,356
306,780 -> 41,780
783,779 -> 513,779
81,886 -> 81,531
403,592 -> 69,926
575,458 -> 575,709
406,695 -> 406,563
342,690 -> 269,690
712,766 -> 712,693
929,608 -> 929,355
562,750 -> 571,759
353,780 -> 424,780
296,111 -> 944,759
341,28 -> 859,28
415,28 -> 415,153
104,16 -> 966,878
554,780 -> 726,780
749,645 -> 749,466
188,724 -> 933,724
806,185 -> 614,185
244,181 -> 682,181
104,937 -> 367,937
400,116 -> 183,333
749,94 -> 301,542
638,543 -> 811,716
694,727 -> 694,44
46,259 -> 46,721
467,748 -> 620,595
739,543 -> 391,543
734,454 -> 734,101
200,954 -> 200,654
592,358 -> 592,581
758,29 -> 758,401
386,685 -> 519,685
538,294 -> 298,294
82,269 -> 766,269
840,114 -> 332,114
924,542 -> 185,542
936,311 -> 369,878
820,724 -> 590,724
277,689 -> 68,689
76,634 -> 167,634
255,503 -> 144,503
753,915 -> 753,27
288,329 -> 513,554
729,297 -> 447,297
128,533 -> 128,530
252,718 -> 856,114
947,498 -> 312,498
142,917 -> 950,109
815,845 -> 770,845
863,98 -> 332,98
927,694 -> 927,276
682,232 -> 227,687
640,474 -> 840,274
98,37 -> 98,149
203,170 -> 99,170
736,956 -> 736,539
686,384 -> 882,580
976,152 -> 380,748
80,859 -> 80,208
687,252 -> 687,748
819,978 -> 101,260
17,722 -> 17,306
99,96 -> 99,929
713,757 -> 157,201
469,986 -> 469,393
813,210 -> 789,186
360,319 -> 360,43
777,707 -> 606,707
928,413 -> 380,961
566,850 -> 828,850
380,650 -> 717,650
863,889 -> 373,399
521,97 -> 967,97
12,989 -> 975,26
965,974 -> 965,848
639,331 -> 639,948
815,731 -> 235,151
823,369 -> 610,369
22,930 -> 898,54
635,113 -> 635,495
462,123 -> 771,123
445,274 -> 304,133
743,633 -> 43,633
811,267 -> 811,936
745,549 -> 636,549
321,720 -> 511,530
949,138 -> 308,138
563,34 -> 720,191
449,313 -> 966,830
857,308 -> 392,773
244,80 -> 820,80
212,345 -> 327,460
28,941 -> 28,22
122,988 -> 122,50
51,889 -> 880,60
328,161 -> 328,820
703,639 -> 40,639
107,47 -> 839,779
338,260 -> 828,750
947,304 -> 276,975
509,281 -> 281,281
200,42 -> 820,662
728,940 -> 728,897
511,770 -> 376,635
197,99 -> 929,99
699,648 -> 352,301
915,291 -> 256,950
641,586 -> 641,346
337,722 -> 965,722
739,92 -> 739,884
656,676 -> 649,676
369,450 -> 686,450
33,672 -> 409,296
336,979 -> 336,595
95,289 -> 888,289
286,128 -> 985,827
471,300 -> 899,300
824,959 -> 66,201
884,104 -> 166,822
562,681 -> 562,948
987,974 -> 77,64
61,47 -> 61,528
359,647 -> 709,647
34,398 -> 270,398
526,66 -> 257,335
744,545 -> 502,787
805,726 -> 728,803
184,749 -> 184,839
136,148 -> 842,148
538,244 -> 306,476
202,698 -> 958,698
264,519 -> 903,519
367,310 -> 26,310
391,747 -> 307,831
580,945 -> 706,945
199,776 -> 98,776
408,242 -> 408,474
929,706 -> 929,570
351,524 -> 221,524
773,783 -> 274,783
770,790 -> 770,653
572,182 -> 572,574
979,13 -> 29,963
175,454 -> 457,736
170,399 -> 170,202
570,406 -> 97,406
813,37 -> 556,37
80,886 -> 465,501
317,799 -> 876,799
602,399 -> 362,639
891,257 -> 445,257
346,275 -> 297,275
345,136 -> 345,755
252,460 -> 252,731
17,573 -> 729,573
901,838 -> 230,167
754,582 -> 754,970
415,964 -> 415,605
857,758 -> 857,612
319,613 -> 256,613
34,614 -> 34,802
443,118 -> 443,891
335,300 -> 80,45
284,340 -> 552,608
154,31 -> 33,31
440,720 -> 20,720
331,219 -> 311,219
163,83 -> 868,788
775,733 -> 775,284
859,240 -> 859,159
144,249 -> 105,210
710,809 -> 710,910
76,481 -> 76,86
825,603 -> 916,603
404,575 -> 740,575
612,427 -> 612,571
63,765 -> 63,538
979,170 -> 183,966
781,372 -> 911,372
237,732 -> 321,648
457,474 -> 954,971
887,110 -> 551,446
894,962 -> 48,116
276,534 -> 800,534
40,42 -> 950,952
986,941 -> 123,78
348,857 -> 899,857
57,728 -> 57,802
776,536 -> 776,462
683,618 -> 683,443
468,914 -> 750,914
420,129 -> 984,693
254,913 -> 166,913
832,737 -> 832,422
472,724 -> 12,724
483,916 -> 483,226
206,751 -> 206,250
890,901 -> 20,31
156,198 -> 883,925
972,367 -> 67,367
409,196 -> 320,196
59,188 -> 815,188
856,856 -> 856,756
84,871 -> 942,13
475,91 -> 475,784
363,578 -> 363,405
219,976 -> 219,717
243,25 -> 243,173
115,194 -> 462,194
91,960 -> 889,162
20,957 -> 915,62
60,955 -> 250,765
882,707 -> 267,92
122,626 -> 122,202
705,74 -> 147,632
418,122 -> 976,680
500,522 -> 936,522
715,652 -> 82,19
118,872 -> 118,479
918,70 -> 918,811
968,968 -> 347,347
985,479 -> 587,877
749,259 -> 749,841
475,102 -> 200,102
808,976 -> 515,976
761,726 -> 761,358
778,523 -> 729,474
266,251 -> 396,251
46,921 -> 914,921
384,424 -> 804,844
442,359 -> 646,155
929,774 -> 920,783
344,958 -> 344,281
33,173 -> 607,173
40,125 -> 897,982
345,640 -> 432,640
845,170 -> 403,612
763,84 -> 763,885
855,388 -> 123,388
861,858 -> 861,940
449,736 -> 97,384
576,592 -> 906,262
868,817 -> 868,633
14,100 -> 610,696
878,412 -> 416,412
43,427 -> 118,502
250,829 -> 770,829
814,444 -> 814,769
647,857 -> 528,857
648,102 -> 514,102
956,12 -> 887,12
665,957 -> 665,891
760,367 -> 178,949
704,524 -> 815,524
269,88 -> 322,88
414,881 -> 414,788
550,696 -> 550,788
624,367 -> 391,367
133,536 -> 432,835
635,154 -> 169,154
396,149 -> 396,166
796,505 -> 886,505
348,235 -> 530,235
428,851 -> 240,851
535,441 -> 637,441
661,175 -> 782,175
589,780 -> 99,290
771,746 -> 771,239
16,567 -> 821,567
320,572 -> 320,136
200,990 -> 513,990
984,987 -> 11,14
60,712 -> 60,137
629,405 -> 738,405
541,536 -> 541,225
250,579 -> 507,579
825,264 -> 974,264
380,832 -> 262,832
598,695 -> 598,159
716,782 -> 418,782
124,698 -> 713,698
930,83 -> 212,801
389,287 -> 344,287
571,788 -> 106,788
519,498 -> 135,114
281,761 -> 221,761
295,133 -> 295,654
686,960 -> 592,960
984,495 -> 984,913
677,751 -> 677,102
252,326 -> 252,824
921,500 -> 357,500
608,908 -> 608,381
587,878 -> 587,677
392,929 -> 449,929
239,444 -> 822,444
87,897 -> 252,897
865,887 -> 368,887
368,934 -> 368,308
64,950 -> 182,950
747,429 -> 636,540
378,109 -> 378,865
187,266 -> 856,935
159,769 -> 708,769
71,119 -> 892,940
629,825 -> 87,283
900,903 -> 900,656
749,703 -> 812,703
225,421 -> 842,421
15,896 -> 837,74
800,887 -> 58,145
456,798 -> 679,798
434,963 -> 434,166
508,491 -> 976,959
809,540 -> 809,614
624,632 -> 975,983
733,102 -> 195,640
83,691 -> 623,151
140,257 -> 29,257
429,934 -> 429,482
702,91 -> 702,137
986,185 -> 986,386
84,920 -> 448,920
927,779 -> 927,679
903,345 -> 546,345
303,523 -> 303,862
360,877 -> 360,202
544,593 -> 544,802
27,192 -> 27,837
105,24 -> 574,24
955,783 -> 556,384
416,85 -> 416,322
923,139 -> 553,139
527,523 -> 828,523
788,320 -> 949,320
391,652 -> 391,166
754,378 -> 607,378
563,409 -> 563,27
285,169 -> 285,883
16,10 -> 988,982
452,563 -> 452,479
881,800 -> 881,542
564,272 -> 457,272
887,441 -> 887,298
941,837 -> 119,15
606,137 -> 606,152
135,472 -> 135,322
881,775 -> 881,132
566,263 -> 406,103
912,696 -> 912,965
123,554 -> 123,911
735,737 -> 533,939
61,197 -> 534,197
91,856 -> 91,465
624,411 -> 624,247
607,899 -> 607,786
139,408 -> 466,735
89,274 -> 545,730

View File

@ -0,0 +1,10 @@
0,9 -> 5,9
8,0 -> 0,8
9,4 -> 3,4
2,2 -> 2,1
7,0 -> 7,4
6,4 -> 2,0
0,9 -> 2,9
3,4 -> 1,4
0,0 -> 8,8
5,5 -> 8,2

View File

@ -0,0 +1,95 @@
use std::{
collections::HashMap,
io::{BufRead, BufReader},
};
use crate::types::TestMode;
#[derive(Debug)]
struct Input {
nums: Vec<u64>,
}
#[derive(Debug)]
struct Input2 {
occurances: HashMap<u8, u64>,
}
fn parse_input(test_mode: TestMode) -> Input {
let f = test_mode.input_file(file!());
let mut buf = String::new();
BufReader::new(f).read_line(&mut buf).unwrap();
let nums = buf
.split_terminator(',')
.map(|n| n.trim_end().parse().unwrap())
.collect();
Input { nums }
}
fn parse_input_2(test_mode: TestMode) -> Input2 {
let f = test_mode.input_file(file!());
let mut buf = String::new();
BufReader::new(f).read_line(&mut buf).unwrap();
let mut occurances = HashMap::new();
buf.split_terminator(',').for_each(|n| {
let n = n.trim_end().parse().unwrap();
let e = occurances.entry(n).or_insert(0);
*e += 1;
});
Input2 { occurances }
}
pub fn lanternfish_1(test_mode: TestMode) {
const NUM_DAYS: isize = 80;
let input = parse_input(test_mode);
// println!("{:?}", input);
let mut current_gen = input.nums;
for _d in 0..NUM_DAYS {
let mut next_gen: Vec<u64> = Vec::new();
for fish in current_gen {
if fish == 0 {
next_gen.push(6);
next_gen.push(8);
} else {
next_gen.push(fish - 1);
}
}
// println!("day {} - {:?}", d, next_gen);
current_gen = next_gen;
}
println!("{}", current_gen.len());
}
pub fn lanternfish_2(test_mode: TestMode) {
const NUM_DAYS: isize = 256;
let input = parse_input_2(test_mode);
// println!("{:?}", input);
let mut current_gen = input.occurances;
for _d in 0..NUM_DAYS {
let mut next_gen = HashMap::new();
for fish in current_gen {
if fish.0 == 0 {
*next_gen.entry(6).or_insert(0) += fish.1;
*next_gen.entry(8).or_insert(0) += fish.1;
} else {
*next_gen.entry(fish.0 - 1).or_insert(0) += fish.1;
}
}
// println!("day {} - {:?}", d, next_gen);
current_gen = next_gen;
}
println!("{}", current_gen.values().sum::<u64>());
}

View File

@ -0,0 +1 @@
5,1,5,3,2,2,3,1,1,4,2,4,1,2,1,4,1,1,5,3,5,1,5,3,1,2,4,4,1,1,3,1,1,3,1,1,5,1,5,4,5,4,5,1,3,2,4,3,5,3,5,4,3,1,4,3,1,1,1,4,5,1,1,1,2,1,2,1,1,4,1,4,1,1,3,3,2,2,4,2,1,1,5,3,1,3,1,1,4,3,3,3,1,5,2,3,1,3,1,5,2,2,1,2,1,1,1,3,4,1,1,1,5,4,1,1,1,4,4,2,1,5,4,3,1,2,5,1,1,1,1,2,1,5,5,1,1,1,1,3,1,4,1,3,1,5,1,1,1,5,5,1,4,5,4,5,4,3,3,1,3,1,1,5,5,5,5,1,2,5,4,1,1,1,2,2,1,3,1,1,2,4,2,2,2,1,1,2,2,1,5,2,1,1,2,1,3,1,3,2,2,4,3,1,2,4,5,2,1,4,5,4,2,1,1,1,5,4,1,1,4,1,4,3,1,2,5,2,4,1,1,5,1,5,4,1,1,4,1,1,5,5,1,5,4,2,5,2,5,4,1,1,4,1,2,4,1,2,2,2,1,1,1,5,5,1,2,5,1,3,4,1,1,1,1,5,3,4,1,1,2,1,1,3,5,5,2,3,5,1,1,1,5,4,3,4,2,2,1,3

View File

@ -0,0 +1 @@
3,4,3,1,2

View File

@ -0,0 +1,67 @@
use std::io::{BufRead, BufReader};
use crate::types::TestMode;
#[derive(Debug)]
struct Input {
nums: Vec<i64>,
}
fn parse_input(test_mode: TestMode) -> Input {
let f = test_mode.input_file(file!());
let mut buf = String::new();
BufReader::new(f).read_line(&mut buf).unwrap();
let nums = buf
.split_terminator(',')
.map(|n| n.trim_end().parse().unwrap())
.collect();
Input { nums }
}
pub fn the_treachery_of_whales_1(test_mode: TestMode) {
let input = parse_input(test_mode);
let min = *input.nums.iter().min().unwrap();
let max = *input.nums.iter().max().unwrap();
let mut best_sum = i64::MAX;
let mut best_pos = i64::MAX;
for p in min..=max {
let new_sum = input.nums.iter().map(|n| (n - p).abs()).sum();
if new_sum < best_sum {
best_sum = new_sum;
best_pos = p;
}
}
println!("{} fuel @ pos {}", best_sum, best_pos);
}
pub fn the_treachery_of_whales_2(test_mode: TestMode) {
let input = parse_input(test_mode);
let min = *input.nums.iter().min().unwrap();
let max = *input.nums.iter().max().unwrap();
let mut best_sum = i64::MAX;
let mut best_pos = i64::MAX;
for p in min..=max {
let new_sum = input
.nums
.iter()
.map(|n| {
let d = (n - p).abs();
d * (1 + d) / 2
})
.sum();
if new_sum < best_sum {
best_sum = new_sum;
best_pos = p;
}
}
println!("{} fuel @ pos {}", best_sum, best_pos);
}

View File

@ -0,0 +1 @@
1101,1,29,67,1102,0,1,65,1008,65,35,66,1005,66,28,1,67,65,20,4,0,1001,65,1,65,1106,0,8,99,35,67,101,99,105,32,110,39,101,115,116,32,112,97,115,32,117,110,101,32,105,110,116,99,111,100,101,32,112,114,111,103,114,97,109,10,51,812,37,278,203,12,1699,10,24,482,200,197,433,141,1854,148,529,748,46,1366,41,329,300,29,159,767,661,238,586,940,139,1606,273,1093,1687,694,232,1069,264,162,752,250,138,471,828,72,285,136,817,258,586,308,191,478,43,750,570,38,207,1221,434,124,1410,125,743,7,827,963,873,263,478,938,686,250,1022,7,917,717,1354,618,639,24,113,417,550,279,919,736,75,117,1173,32,172,88,1435,15,442,232,272,102,253,113,173,86,57,536,1282,111,18,197,117,738,427,910,740,861,90,706,520,8,1,129,80,79,36,788,1545,119,971,1435,945,808,821,1080,227,1257,973,39,303,818,669,7,197,819,1683,50,2,1248,1459,669,210,653,978,76,509,173,304,183,228,45,1032,672,792,12,540,839,135,153,55,29,1190,42,395,626,487,54,831,956,1,1012,1461,929,561,34,733,629,49,146,469,220,1368,89,265,128,521,402,557,1121,853,240,655,100,341,137,525,371,288,1389,430,1148,398,130,174,176,982,74,923,1438,469,572,33,261,126,456,300,174,27,60,1052,428,196,403,394,392,40,474,27,351,194,619,657,722,181,300,448,1037,525,1388,854,1459,967,211,46,1708,1175,1225,613,1315,479,973,573,324,887,2,116,752,447,3,1074,1135,72,595,601,632,511,1349,267,164,6,1300,172,412,3,298,1120,93,161,176,141,150,67,37,144,421,45,1451,781,1120,205,487,344,372,150,136,614,265,536,1740,265,1367,0,322,204,76,97,1112,717,444,418,279,943,597,309,322,205,1167,292,18,383,367,621,770,13,243,1641,500,313,785,106,184,310,615,248,664,98,221,740,450,460,7,23,1226,183,75,449,806,721,1057,266,254,1083,0,125,27,151,16,664,73,94,44,1347,73,325,958,475,862,1096,1523,114,307,1418,46,113,188,462,194,535,282,1144,26,1106,1465,39,133,445,177,481,233,696,181,72,1466,747,266,44,311,1061,505,140,956,360,716,98,844,1059,305,162,1679,817,873,969,793,1079,320,318,70,417,1170,628,1628,1515,894,482,1757,423,1024,267,1280,10,474,806,684,378,425,816,243,388,27,116,569,777,946,593,646,91,639,508,63,405,1310,639,380,323,75,860,67,42,58,198,35,58,180,75,530,25,194,1743,476,1092,795,243,121,1326,409,1300,218,1393,371,64,412,209,255,648,480,71,125,1398,45,1035,1245,1426,1765,596,187,353,0,261,774,958,1303,397,1024,1076,1225,307,69,789,307,450,143,203,259,21,2,297,963,1236,1292,595,784,100,1194,1246,1820,534,58,244,12,194,1316,211,368,192,741,1232,23,87,551,291,12,512,6,42,1513,619,62,1339,375,743,137,1486,254,53,274,299,1443,844,899,753,414,241,161,52,163,66,86,503,823,528,150,376,403,1346,125,363,412,774,374,1090,1001,177,1379,74,193,49,92,294,679,108,228,199,1203,324,64,321,89,601,32,46,1274,519,1089,1107,63,379,1062,1034,129,736,716,156,526,445,1,299,388,444,1080,1016,101,735,315,517,13,390,537,155,140,1119,975,259,254,402,277,1160,372,55,392,1022,1119,4,735,266,260,1550,389,824,1426,23,65,480,151,176,1761,0,16,139,152,383,358,1155,95,1138,310,232,71,1073,22,1,335,1168,792,136,902,33,204,59,146,1063,1012,103,1083,160,885,445,499,473,278,451,191,1940,249,37,722,325,495,615,70,85,50,107,560,597,75,206,767,990,113,530,94,1343,250,116,67,417,390,500,633,736,132,473,646,1502,249,119,228,3,64,212,19,1005,324,14,418,619,847,20,878,533,204,49,820,216,34,60,62,119,680,88,359,8,473,882,138,387,297,419,664,693,420,101,53,829,3,101,272,726,639,368,363,0,33,70,0,626,525,364,784,271,73,536,318,598,794,34,314,1248,1596,764,34,202,1383,635,158,1095,76,0,119,176,1158,301,409,796,242,1765,808,59,0,278,4,8,359,1111,818,931,220,109,292,353,532,750,333,223,725,1476,199,1,201,55,72,117,37,210,400,108,619,863,187,372,15,574,380,635,332,1,1210,64,897,501,12,822,508,250,263,1044,72,15,210,901,219,471,292,179,572,733,422,1354,1197,202,538,662,261,973,0,465,522,412,9,166,325,237,757,115,1046,273,549,174,30,96,215,113,7,1032,671,262,202,332,1078,629,555,26,8,29,349,206,123,1093,673,1356,513,1454,518,1240,337,96,115,1160,17,331,1450,114,107,782,995,168

View File

@ -0,0 +1 @@
16,1,2,0,4,2,7,1,2,14

View File

@ -0,0 +1,128 @@
use std::{
collections::HashSet,
io::{BufRead, BufReader},
};
use crate::types::TestMode;
#[derive(Debug)]
struct Display {
patterns: Vec<HashSet<char>>,
output_values: Vec<HashSet<char>>,
}
#[derive(Debug)]
struct Input {
displays: Vec<Display>,
}
fn parse_input(test_mode: TestMode) -> Input {
let f = test_mode.input_file(file!());
let mut displays: Vec<Display> = vec![];
let mut br = BufReader::new(f);
loop {
let mut buf = String::new();
br.read_line(&mut buf).unwrap();
if buf.is_empty() {
break;
}
let mut s = buf.split_terminator(" | ");
let patterns: Vec<_> = s
.next()
.unwrap()
.split_ascii_whitespace()
.map(|s| s.to_owned())
.collect();
let output_values: Vec<_> = s
.next()
.unwrap()
.split_ascii_whitespace()
.map(|s| s.to_owned())
.collect();
displays.push(Display {
patterns: patterns.into_iter().map(|p| p.chars().collect()).collect(),
output_values: output_values
.into_iter()
.map(|p| p.chars().collect())
.collect(),
});
}
Input { displays }
}
pub fn seven_segment_search_1(test_mode: TestMode) {
let input = parse_input(test_mode);
let mut n = 0;
for display in input.displays {
for p in display.output_values {
if [2, 4, 3, 7].contains(&p.len()) {
n += 1;
}
}
}
println!("{}", n);
}
fn filter_num_segments(display: &Display, n: usize) -> Vec<HashSet<char>> {
display
.patterns
.clone()
.into_iter()
.filter(|p| p.len() == n)
.collect()
}
fn solve_display(display: &Display) -> usize {
let one = filter_num_segments(display, 2).into_iter().next().unwrap();
let four = filter_num_segments(display, 4).into_iter().next().unwrap();
let seven = filter_num_segments(display, 3).into_iter().next().unwrap();
let eight = filter_num_segments(display, 7).into_iter().next().unwrap();
let size_6 = filter_num_segments(display, 6);
let f_char = size_6
.iter()
.find_map(|d| {
if one.is_subset(d) {
None
} else {
Some(*one.intersection(d).next().unwrap())
}
})
.unwrap();
let c_char = *one
.difference(&[f_char].into_iter().collect())
.next()
.unwrap();
let size_5 = filter_num_segments(display, 5);
let five = size_5.iter().find(|d| !d.contains(&c_char)).unwrap();
let two = size_5.iter().find(|d| !d.contains(&f_char)).unwrap();
let three = size_5.iter().find(|d| one.is_subset(d)).unwrap();
let six = size_6.iter().find(|d| !d.contains(&c_char)).unwrap();
let nine = size_6.iter().find(|d| four.is_subset(d)).unwrap();
let zero = size_6.iter().find(|d| !five.is_subset(d)).unwrap();
let digits = [
zero, &one, two, three, &four, five, six, &seven, &eight, nine,
];
let mut sum = 0;
for number in display.output_values.iter() {
let value = digits.iter().position(|p| p == &number).unwrap();
sum = 10 * sum + value;
}
sum
}
pub fn seven_segment_search_2(test_mode: TestMode) {
let input = parse_input(test_mode);
let sum: usize = input.displays.iter().map(solve_display).sum();
println!("{}", sum);
}

View File

@ -0,0 +1,200 @@
bgcfda gecbda abdgf aedfbg eda efcbd ae agfe bdefagc fbeda | ae egdafb ea fcdeb
gfadb fbagcd cagf agecdb adg fdbcg bdcfaeg bcgfde ga efbda | cbfdge dfcebga aedcgb dgbfa
bgdeca agdbe gfb fdbgce bf eafb dfgab efbgdca gebafd dgcaf | gfbdea gfb bacedg adcgf
fbc becgdf fcbdg adcgf edafgb bgec dcgfbea gbfed bc cbeafd | gfacd gcbdf bgedfa cbdefa
ad dcafeb adcef feagc egdbaf aed cadb bgcedf cbeafgd cdfeb | dabc gabefd ecfdb ecgfbd
badcgf gdaf gadbc ebfcg fceabd gacdbe gedfbac dbf df dfcgb | df adfg cbfgad fdb
cfdb dca bdgca efgbad bafcgd fgdba cgdfea cd abdfegc bagec | agcbe adc dabcgf adc
egdab gcefd ecbgd gdebcaf eacb cb bagcdf abgfde dcegab bdc | bc gedbc dcb fgdec
gefa gedabf cfdbea bdfea bdegcfa dgbfa gfd gdcab fg fecbdg | fgd gf dbfage afbdec
fagdb be abdfe agfebdc eab aefdcb acfed bdec fceadg afegcb | be aegcfb efbcag eb
fdce fcadeg afegdb adceg cgafdb de ebcga dae cdagf abdfgce | efcd efacgd edfc afgcd
gefc defca aefbd ec gfdceba dcabge fgcad cae fcadeg bagfdc | fgcad bdfea ebadgcf ce
cdabe dcg cgef gc beadgf fegbd gcbde edgcfb gecfabd fdgcba | dcg deacb fdegab bdafgc
aefc ebcdfa eabfgd deacb ac dac gdbce gfbadce fdeba gcfbda | adc afdbe aefc ecdafb
eabfdg fdgba fac fc ebdac bfcg fcdabg bgdface bacdf fgedca | fdagcb cedba cf fc
db dbg debag fcgdeb gfeba dagfec ceadg bdfecga deagcb acbd | gbdae afebg cfedgba ecdagb
cabegd befcd aefdcb defbcag dgfb gd edg bfgecd gfdce eagfc | dbgacfe efcdg gd bdfagce
abde fbecag gcabde cefdg bgedc bgcea bgd becgdaf gdcfba bd | baegc egbdc cedgf gdfec
gedc eagbd bgadcef aebcdg dfbag eacfdb cabde fgecab ge aeg | gcbeda gabdfec dgecba gea
afedgc deabf gbfceda fg cgdea dgf bdgfec edgaf ebacgd gafc | fgd gfac cbefgad cagde
gfcbeda bfecda dgca ebgacd gbfae ecg gc gaebc cfgebd dbeca | dfcbea cegab cg cge
adefc eabdf bcafeg bd edb fcbeadg dabegc fdbg baegf fgaebd | fcead fcdea fbgd ecdbga
ac bedgc acdb gacbed bfgeadc degcfb aec cafdeg eagfb cegab | agecb gdbec abgcde agbef
cfgbead cfgdeb gafb baecd bg cgdba bgc adcfbg agfecd dcagf | dgcefb bgaf fdbcgae agcdf
agc dabc dcbgef efadgcb ebdgc baefg ac abgce ebdcag facdeg | ecbdgf ac afdgbec decfbg
fgdcb cbdeaf eabfcg cefdb gcbeadf cdeg gc afbdg cfg ecdgbf | cg eafbcd dgec cbfdge
da dbae fdaecb cdebgf afgced bedfc adf cbfeadg bdacf bfcag | dfbec egbdfc bdfca cefbd
abgfced cf cfdbe geacfd bfac bdacfe gdebc bfedga dcf bdafe | bcfa dbfce cf egdbc
dcagb feagbc dbef edafg fagebd bf fedgac fba bfagd dbcefga | fbdga dgafe cfabge fdbe
gbfce bfc acgb fbgaec bc acfedbg gface cgefad bfacde defgb | cbgfe bgfeca gacb degacf
cfgeda bac dbafg dbcfa bdfcage ecfb acedf cb ecfadb eacbgd | gafbd faced dacfb bcef
fbgec eca cbag cfegba ac efacb bgcedf caegdbf fecgad fdbea | ca acgb ebdfa agcb
acgbf cdabegf egbfd dc dacbef dacg gecabf fdc dbgfc dfcagb | dgabecf befgd acgd abgdefc
dagbfc bc agcdf cgbd bfc ebfad dfcba befgac gaecdf becdgaf | cdgb dbcg abfcdg cfb
bdcga ebdgca da cdeafg dac acfbg daeb cdbgef egbfcda bdegc | bcgad adgcbe fedacgb cegdb
gacfeb aefbg fbaegd cedgfb dfcba gc cgae abcdegf gacbf gbc | cfabd debfgac cega egca
abcfeg fdbegc ac bdafgec ebfad fcdae dcga fcged gdceaf ace | gbcfaed ebfda ac gefcab
gefbc gdeacf cag ac acgebf ceab adebcfg bfcag cgbfed bgfad | cga cagdef gac fcgba
gcbe gacfdbe dfgaeb bfecd ecdgfb dfcgae bef fcedg bcfad be | fadcb ebgc cbfda dfcab
ebcgfd dafe adcfg gfacde cfabge bcadg dfc fd fbdaceg gefac | fceadbg fcgabe fade fbeacg
fbacg cef cagebf gacefd ef bgced cfbeg dagcbf fbea cegfdab | eabf fce cdgeabf agefcb
bca gcadbfe ca edca fbgade dcageb ecbgf egacb ebadg dgcfba | cgafbd feagdb gabecd cba
fcebdg cgefb fagbce edcbg bfed eacbgfd cegda db bcd dfgcba | becdg cdbafg bedgfc cgbeafd
fcadebg edbafc cgfda decba egab gcadb gb debgfc cgbdae gdb | bgd agbe eadcbf gdb
cedfa gad ebcfda aedgc defagb gdafceb gebca dcfg dg edgacf | cdfg fadec gedac bcgae
dafegbc dcfbae dce fcebgd egdaf ecadf dfcbag acbe ec cafbd | ced dce ce ec
fegb ef bdcefg fabdgce efd fegdac cbdaf bcdef ecbdg acbedg | debgac edf ef dfacb
afgedb caedbg egfba baecf efgdb gbcadef fagd age cfegdb ag | afbec degbafc fceab eagbdc
cebd dfb dbecgaf gefcb afbegd bd fdcgb gfcebd cgdaf gfeacb | abecfg gacbfe cgfda bfceg
acdfgb ecbga ba ecgdafb deafgc aedgc aedb aecgdb bag gfecb | cgaefd gabce cdgbae ab
be fcgdae fcbag efdbcg ceb cfebg degb cdebfa fgdec gdcabfe | eb dfegc debg bce
fdecg dafgebc fgcae fgd bcfd bcged edbgfc aecdgb fbgade df | afbdecg bgfaed fcged bgedaf
cabd egdfa fbgac bcgafd ebdafgc bd eagbfc dgb fdabg cgbdef | bd bd dbg cbad
fgecba bd dgcbea gfeda gdb geacb ecbd bfadcg efcabdg bgade | dgbae befgacd ecbag afcebg
ga abefc afg fecadbg egfcbd dgfbac edgfac adbg fcgdb gbcfa | dbag fag fgdbcea dcbfeg
geacf cadfe bfaced fg fcdgae fged ebagc cfg edbfgac bfdacg | afcbged efadc fg fcg
cbd cdefbag cfadg bd bfdac abed fdabce dbegcf egabcf ecbfa | gcebdf faegcb ecdgbf fbeadcg
fagcde cgdfa daef acgde ecgadb dbcgf fca eacgbf gcafbde fa | cdage cebfga adceg deafcgb
feadgcb efcag dbfge edba dfcabg ebgfa ba gefbad ebgdcf bfa | abdefg daeb bfa fdgaecb
dgfcea becdfg bdefg abgdfec beg cgedf eb decb bgefac fgdab | beg fedgc egfacd fcedg
fcagbd adbfeg gbeda bgfde ebfcdg dfae gaceb gda ad fdbcaeg | agd gdeba fdgeab ebcga
dbcefa bfaegd fdcb fdeca agceb fcabe cfaged beagdfc fab fb | edabgf gcabe adefbc fba
eac cfgeab agdfc cdgbe gdbefc dabe gefdbca abcged ea agecd | baegdcf bfcegd gdbfce gfcebd
cdbeaf cd dcf adcgfeb defab gbfca dgbafe cead fdcab bcefdg | dfcabe fcd dc cdfgeb
gdcfbe bag dcebg dgabce cfgdba geacb bdea ba febcagd faegc | gcfea dgbfec fecag cefbgd
ebfdgc baecdfg gabfe acbg bce dgabef cdeaf gfabce ecafb cb | eabcf gdfebc egfabc gafbed
dbgacfe adgcf cedafb ecbad bdfe bf egcdab abfgec fab cbfad | bdfe fba bcfead abedc
dca afdcb befagc gacedf dbecf cgfabd da dbag ceabfdg afbgc | da adgb gfedcba dabfcg
fgcda bcgf cgfeda bfdcga gbd badfg cebafdg gcadeb bg dfbea | fcbg geacfd abfgcd bafgcde
cf gacbfe feadcb cdgaeb fbcd fcdae efdag ecf gcabedf edacb | ecabgd gbcfaed gebcad efdac
ae abcefdg faedbg agedf dgbfce gabcfe adfcg dfbge efa bdae | dcfebg fae agfdbe egfdba
fegdcab dgcaf bfd fgdab edcgaf eadgb fbcg deacbf fb gbdcfa | acefdb gdcaf fcgda gabde
ca cefa bgcfed facdbe dgbfeca adc dbacfg cabde edabg fcbde | dcbefa bedfc dfcgab dbfacg
ce bgcedf cgdef gdabfe cabefd dec aegfdcb dgcfa gcbe fgbde | cegb dce aedbfc ce
fcgaebd ef febdac feb cafbd edcfb fabecg acbdgf dfae ebgcd | feb bfe fegacb bafdec
fgdec adcb dfbega afcgeb egcbd db bcaeg beagdc dbgfcae deb | bd egcdab fecdg bed
ad dcaeg edcbag fgbdeac acd gcdfeb afegc ebda cadbfg dgbce | bfgadc dca ad cda
bdegaf egfdb afb acbegdf fgdba af feda bcfgea fbdecg abgdc | fdegb fabecg degabf adgbef
gdfbe gcf gbcda cadfbg cfdbg gceafb dfca badegc cf cedgbfa | fc fcg egcabfd bdcag
fcbag ebdcgfa ce fgaced fbgeca gfbec cgdfba fbgde efc aecb | beac ce cgbfe bfegd
dcgaef gedfb fegdabc eabfdg dgc cefgdb bafdc cg gecb gcdbf | debgcf gbcfd gbce baefgcd
bfacdeg cedgf gfdbec eca edgcfa dbeaf ca fcdea dcebga cgfa | cefgd gcedfa bdfegc aefdc
cdfea ebgdf ab adebfgc bdcegf gbda fab dagfeb ecgabf ebfad | defba cfdea ba ab
gbefcd cbedf fcaed dgfb dacgbe ebfgca fb bfecadg cfb gcbde | eabgdc egcbdf dcgbef fb
afgedb cabdefg afcdb efdgca ef abgde edf ebfad agdbec fegb | edagcfb cdfba efdagc dcebga
dcfg afbgedc efgdb agbfe fde dgbce dabegc gdbfec df fadcbe | dbfeg gdfc efd faegb
ebgdf acfge bae gbafe efdabc ba feadbg bgda gfebdc gedacfb | fbdcge dbag ab bafedc
abedcg dcagfb cf dagbc afc fceabd fbcag dfcaebg dcgf abefg | afcdbg dcfabe fcabg adgcefb
fdbc egabc bfegc fc gbfced aefbgd gfebd gfacde bgdafec fec | fbcd fedbg daefcg cef
bgacde fegad febc gfacedb gebca gaebf bfa efbgca gfadcb fb | bf dcgfeba fdcgeab fb
cgdfe fagec fbace afecdb acg fgcbead ga abfcge bcgdfa ebga | gabcef cfgde dfgcba gcbdfa
bgae ae acegfdb dbfeg dbcgfe ebgafd dfcba cgefda adebf eda | aebfdgc bedaf feabd fbadc
dfagcb cbdgea efabdcg ecdab gceaf daecbf df cdefa fad befd | afd gefca adgfcbe fd
dcfeg acgf aec degfbc ca efbcgda fcdea gfdeac faedb cegdab | egcdaf gebdcf bdaegc aefdc
cgebaf cbdagef cadgb efda degbaf fgbea dbe bgfdce edbag de | de feda dagbe agfcdbe
eagbcfd eag efcad acefgb ag gadce fadg bdgec baefcd aefgcd | bgecd edcfab baecfg ga
adbcfge eadfb faecd ab dba aebgdc ebdgf ecbfda fcba gdfaec | efcad febdg bfdgeca dba
adgfc dfegc cdfeag edf dbecagf fdabge fbgce ed dcea cagdfb | agdfbe def efd deac
cgb bfgde bdcfa fgcbd fgeadbc fcabed befcag cg dacg bgafcd | cg acfedgb bcagfe bdfagc
fcbdge egdac acgfe efgcad fc cdaf gacefbd fcg faebg dcebag | ecdag efcgda fdac dgcbea
dgabef fdabg bcdagef dcfbg fgac adfcbg fc gdbec cbf fedcba | bdcagf dfagb fabgd cf
defc cgafed ecafbg fbdag aef cdgbae gecad facdgbe ef dgaef | fe fbecga gfdba adgebc
gbdfac gcfbd dgceb bcgdfe fedabcg gdfe fbceag eg baced gec | gacfbe cdgebf cge cge
acgdbe cfa gfba cfdab cbgadf cefdb bcfadeg fa fedgac agcbd | abfg agcbd gcfeabd dfcbe
dagebf defcb dgecaf edfbac aecbdfg deacf be aceb bfdgc fbe | ebac dcfagbe feb bcae
gbcfad fadgbce ag fdcegb gbda ceagfd agc cfdbg gfcba eabcf | dagbcf gdacfb dafcgeb eabfc
dbgcea ag acg dbag aegcd bgdefca bdgce ebgfac cgbedf acefd | agecfb abdegc dgcefb agbd
dgebfca cgdfa bgafe eda cgedfa dfage fgdbac ed ecbfda egdc | befgdac daefg acfdg acdebf
fgadce dcb abgdefc cedgf fecdb cb dgcfbe bgdeca gcfb defab | fcegbd cb gdfce gfebcd
cfbdgea ec dacgf fdeab adfec dbce dbegaf cfe deafbc ebfgca | ec decb ebagfd gcfaedb
gd bcegad afdec dgbf efcdagb cdg fgdcba fgadc abfgc cgafbe | gacfb bdcgea fabcg gd
dbega afebdg ca ecabdg deca cfgbae bca gebdfac gfcbd adgcb | cab caed abc cead
cgdbfe ab fgade gefcab afcdebg agdbe dcbeg bdca abe cgabed | ba debgc gcefba cegdab
cdfegb dbafe abdgce cbfgea bfg fg cgbed dfgc bfceagd dgbfe | gf fadgcbe aebgcf cafegb
ecafb efgcab gbdecaf fbega egabd gef dfbcge fg dcbfae cafg | dcgfeb bafdceg agfbe gf
efdcbg fgbca dcgbf dbfcag faebcd abegc afc egcdabf fadg fa | fa afc adefgbc fbcedg
dfa gcdbeaf dgbafe gacfbe dcagb adcbfe febca df dbfac cefd | adgefbc daecfbg efbca bagefdc
aec fecbgd aegdc fgcad bcedaf bdcge gbea defacgb ae acbgde | ae deacg edgcb afdecb
cdab bedgf dcbegaf gdfac bc efabgc fbc cdbgf afgcbd efdagc | cfb fgebca cgfdab faedbcg
ab eacbgf bdfa fcaedg fdebagc degba bcdeg afdgeb dgeaf gab | gfdbea afdb afdb gcebd
acbed bfeacd aedgc aeg ag gdbfae egbadc bacg fcdeg gcdbfea | gdaec aefbcd gae bdaegcf
fgadbc geafb dafbgec ecdb ce bcega deacgf egc geadbc cgdab | ecgba cdbe gacedf aecgb
gbea eb cfdbaeg cebad gbdac fcdae dgebcf fcabgd adgbce ceb | dagecb ebc dagbc ebc
fbeda ga aeg aebgd agfd bdgcfae fedgab dfeacb fbaceg begcd | ga ag afcbeg bfecad
efabcg bdc fedgc adebgcf gcabed cfdbg dfba db cafgb bfcgda | dcb bdc agfbc afbegcd
fbade bcagfe cgfea cdag dfc cfade begcfd cd gecbdaf dfceag | cgda gcda fcd dc
dbcfa bg gafcde bdceafg gaecbf cbg bgde bcgad aegdc bceagd | gcdab eacgdf bg cgb
ecfagd fbadcg fgbae bdgae gbf befc facgbe gecbfad fb eagcf | gabed fb egabfcd facbegd
abef dcabf ecfdba afbcgde bgdfce af acf cagdb gaefdc bfcde | cfadb abef bcfde cgfdbe
acedbf abcfdeg bdceag bgade acdfgb aeb bgdfe eacg ea gdabc | begda dfbge ae badcg
fcda abgef da dea dacgeb fbdae cedfab bagfced ebcgfd cdfeb | bcedf edcbfg dbaceg bdcfe
dgcfba cadeb agcdb aebfgc gdaf bcfdg gac ga fgebcad fdcebg | dgacbf ebdca egfdcb bcfdg
afbedg egbd bfcdag edgcaf gbdcfae afegb fgdae gbf fcbea gb | dgbe edbg bg efdag
acb abcfde fcgead efacd eabfc dbfa ab egfcb bedgac fdcaebg | defca acb caefd acbdge
afcde dbe fbcaed aecbfgd bfec be eadbc afgdbe dacbg gcfead | be ecfb fgdabe aedgfb
fdeacg cbgfa gefb bfc bf gbafce bdefac agefc aegcdbf agcdb | bf gceaf efbg bfcaeg
fdce gacfb agbdec cgafd df caegd gdcefa bcgadef agfbde adf | fagbc fcgdea fd bfgca
gdfacb aebcg ga dgae egfcbda cedbg dbegca acg gbedfc ecbfa | ga eagd eagd edgfbc
cgbdf eb caged aegfcd egb badecg gdbce ecba egfdba dfbaecg | cagfdbe geb dgcefa dafegc
cd gbecaf acfdb cdfg cda gbdface gfbac dgacfb bfade dgecab | dfcgaeb dc bgdacef bfacd
geab fecdb gb feabcgd ebgcda gdcae gecdb gbc afegdc gbfdac | debcga bgdaec dceagb cbaged
edgfcba agd bacd afcdg degbfc acefg efdgba bgadcf da gdfbc | gcfbd dbac cgfdb gecaf
bfca fcbged dfgab cgbadf cdfabge degcab adfeg cgfbd ab bad | abcf afbc gafdb acfb
gc fdgac fgeadb afdcb gecdfa gac gecafbd cabged gfce gaefd | ecbdgfa cag bgfeda eafgcd
eagdc efdacbg cdgbe cbfdg egadfc egb aebc be gefabd dbecag | bafcgde beg gbacde dcbge
ac afbgcd gfdba fabecgd ebdafg bac cfag adgcbe abcfd cfdeb | adbegf egacdb bac acb
cafbed gf gfd cgdba eadfbcg gfdcb gbef cfdbe fdgeac gedbcf | facbegd cbdag fg fdg
fbgcd fadb fcd fd dfcbga decabg egbcf gacdb fcgeda aecfdgb | bedcafg cgdbaf fabgdec dcbgf
gb bcgeaf aebfcd cdefbga cbaef cgafb gbf dfacg gbcfed bega | bg bfacg bdfcge geba
cadfe afbgced aedfcg cebfg aeb fdageb bfcade bfaec ab bcad | bacef bcafe bfgeda baefgd
cgbde abcg cea dbegca ac agced befdgc bgafdec efgda badefc | cae eca gdfae dcega
ab fgcbed agb cafge cegba deabfcg dbgec aedgbc gfadbe bdac | ab bdca cgfedb dgceb
bfgadce cfadg fa cagdfb bcgdf fagbec gfa fbad bgcefd degca | fa ecgad gceabf dcfgb
gfd dcabegf fg cefdg cbefgd gefb ebgdc egacbd gcafdb dcfae | ebfgdc cegdabf cbafged fdg
dfecagb fbag bdcega gecdaf fg cgbad gfdacb fbcde fgd dfcgb | gfd gfd dgbeac fg
gfaed gad feacg dfaebcg deba efgbdc gefbad ad agdfbc fbdge | dga bdae fegbcd dgfabc
agdbc egdcafb caegdb afgdec cbadfg fdgba efdba bcgf gf fga | dfabg cagfdb eadbf cdfaeg
bgefac fecdabg cebdaf de aedfb ade bcefa gcafed bcde adgbf | ebcgaf fbadg ead dea
edbgc dcba egcfbad cfdage abfegd caged ebd bd gdaecb cgbef | cdebg badc edgbc ebcdg
cgfabe dce fcdba efbcg bdcef bagfecd eabgcd ed ecgbdf dgfe | fdcgbae egfd edc bfegc
fgecad gebda ebgcd egc dbecgf ce cbef cgdfb egacdfb agdbcf | edbfgc cdgbe edgfcb bcefgad
geafcd cdage ec cdegbaf gec cgafeb abgcd gebfda gafed dcef | ce gdbfea egdabfc gec
be abdgfc daecgfb bae dbfag gfdeba dbafe gbfeac egdb dfcae | bdge eba ecfgba gcaedbf
bcge gbeadcf acg bfcdga bfcaeg cfaeg gc feabc bfaecd gedfa | bfeac begc fgdbca bgfedac
gfbed ce ced fceabd dabegf bcdgef egcf becfagd gcdbe gcadb | ce ec bdfaceg cdeafgb
gfdea db cfdgbe badc gbaec ebfagc bde dgbcae egbda cbgedfa | badegc cbeagdf bd egfbcad
deabcg egbca agfdbc edagfb abe eb afbdceg efcag becd agcdb | bced eb be bea
ebfgda bgade dbcea geaf gcbadf dgfba eg gcedabf geb cgfbed | edabg ebg cedgfb bgdcfa
fc adgbecf fbcd abgfdc cgfab aedcfg dgcab gfeab cfg cbdgae | fcg badegcf bgdac fgc
edg egbda dafbgec begc aedcb cadgeb eg eacdfg gafbd cebadf | dfgba bdegac cbge afdbec
bgfecad gdbaf afdbeg egafcb db dba fdbace egafb gdeb dcgfa | fcbade fdagbe bad cdbefa
bcgedf ecba bagcef beg cgafed eb gfedcab gfbad efgba egafc | bace fbgdec cbgefa beg
dgfecb fcd edfbag bcfade fc caegd bcgedfa defab caefd cbaf | dbeafcg fc fabed dcf
ceafg fbad cfeba edbca afebdc bf cegbad bcdfge cgdefab feb | gdbecf faebdgc dcaeb ebf
cgdfba cdabf gfedca efgcadb baegfd bcfg fc caebd gdafb caf | cgfb dgcfab befgadc debgcfa
ebfgca abcfe gca bagde bgcae dbgcfa efbadc gcef agefbcd cg | abegc cbfae acefbgd cag
dfegac gedc bcdaef abgfd egf ge fagecb agefd fecdbga ecfda | gbafd dacbef gdefac gef
feb begcdf fcab edcafb bf aefdc gbade bfead daegfc feagcbd | bcfeagd gcdbfe fbceda fb
baefgcd fedcb cfbaed dgc abdgf bdfgc bcedgf gc gcfe aegdbc | ceafgbd fbcdge bgdface efbdgc
fd gbdae bfda ebfgdc edf gfdea caegf aedgcfb fadgbe bcagde | abedg adegb efd fcaedgb
befg gecdb dbcgafe adebcf dge ecfbd bcefdg aecgdf ge bgcda | bgfe gdebc dge ge
cfdgb be aecfg gefdbc bgfec afbdgec cdabfe egbd cgabdf cbe | fbdgc eagcf eb faegc
gfdbae aedcfg gbcfed bcde dbefgac gfacb ceg bdgfe ce efcbg | efcbg ce edcb ebgadf
ba fecba dbcgfae gcab gfcade febdc adfegb fab aegfbc gcfae | adfgbce gebdaf cefba fba
eadbf acfde cfdeab bagfcde bgfcae eab gafdce cedb dbgfa eb | bea fgeacd eb fegabc
ecb baedc ec adbfgc cedf faedcb acdfb fdgaebc agbde aegcbf | fdce cbfdage bdacgf cedf
fegc cefgdb fdbge bfg badgcf eabcdf fg begad dafgceb fedcb | bgf fdbec gf ebdacf
gfecdab gbeac aebd be eacgd ebc dfegca cbagf agbdce bcdgfe | bec ecdga ecb gabec
cb bcfg cgdfa adegfc bca aegdb afcebd gcdfab dbgca becfdag | bc badcgf bca fcbg
gabdce dc fbgdc gbfec cfda fgadcb bafgd fbdeag abdcegf cdg | fcda adbfg cfgbead dgacfbe
ecgab gbdecaf bcfa cdegbf eabgdf fgeabc ba egfcb gedca gba | fcbeg gacbfe ecdbafg deacg
agc bgfca cg cgafdbe dcfg dcfagb fbdag cabef gdbace gdefab | dcfg agbfc bcfea cag
cagb ga dfcga fdaebc fdebga cbdafg gfcbdea fcgde cfbad afg | cbga cedgf cdbfa afdbcge
cafgdeb aebfd gfbeda ebcgdf agcbf egf decbaf egda abgfe ge | bgacf bdfea agfbe beadcf
be agbefdc aedfgc gabfc adgfeb egb efdag afgbe deba bgfedc | fedgacb dcafge bdae ebad
cfbae dcaegf cdbfaeg fcebga efgca cdfab ageb gcbfde ebc be | acfgde be be bcfagde
bafdgce cea gdbcfe ae agecd agcdbe ebfcad agbe bcdeg fgadc | cebgad ecbgd cbdeg gaeb
bgce agefcbd egbcfa eg fadgb bfcae dfbcae egfab gae fdcega | age gafcbe eacfbg gea
fgacde gfaedb ebacf gc gdfc dcbeafg fadge gec bdcgea cgfea | fdagcbe gfdc aefgcbd gc
gb acfdgb bga aedgf bafdg abdfecg gacfeb bgdc bfcad fcedba | fgaecbd dabcf bcdg bg

View File

@ -0,0 +1,10 @@
be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb | fdgacbe cefdb cefbgd gcbe
edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec | fcgedb cgb dgebacf gc
fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef | cg cg fdcagb cbg
fbegcd cbd adcefb dageb afcb bc aefdc ecdab fgdeca fcdbega | efabcd cedba gadfec cb
aecbfdg fbg gf bafeg dbefa fcge gcbea fcaegb dgceab fcbdga | gecf egdcabf bgf bfgea
fgeab ca afcebg bdacfeg cfaedg gcfdb baec bfadeg bafgc acf | gebdcfa ecba ca fadegcb
dbcfg fgd bdegcaf fgec aegbdf ecdfab fbedc dacgb gdcebf gf | cefg dcbef fcge gbcadfe
bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd | ed bcgafe cdgba cbgef
egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg | gbdfcae bgc cg cgb
gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc | fgae cfgab fg bagce

143
src/t09_smoke_basin/mod.rs Normal file
View File

@ -0,0 +1,143 @@
use std::{
collections::{HashSet, VecDeque},
io::{BufReader, Read},
ops::Index,
};
use crate::types::TestMode;
fn parse_input(test_mode: TestMode) -> Floor {
let f = test_mode.input_file(file!());
let mut floor: Vec<Vec<u8>> = vec![];
let mut buf = String::new();
BufReader::new(f).read_to_string(&mut buf).unwrap();
for line in buf.split_terminator('\n') {
floor.push(
line.chars()
.map(|c| c.to_digit(10).unwrap().try_into().unwrap())
.collect(),
);
}
Floor { floor }
}
struct Floor {
floor: Vec<Vec<u8>>,
}
impl Floor {
fn rows(&self) -> usize {
self.floor.len()
}
fn cols(&self) -> usize {
self.floor[0].len()
}
fn get_neigbours(&self, p: &Point) -> Vec<Point> {
let mut points = vec![];
if p.row < self.rows() - 1 {
points.push(Point {
row: p.row + 1,
col: p.col,
})
}
if p.col < self.cols() - 1 {
points.push(Point {
row: p.row,
col: p.col + 1,
});
}
if p.row > 0 {
points.push(Point {
row: p.row - 1,
col: p.col,
})
}
if p.col > 0 {
points.push(Point {
row: p.row,
col: p.col - 1,
})
}
points
}
fn find_low_points(&self) -> Vec<Point> {
let mut res = vec![];
for r in 0..self.rows() {
for c in 0..self.cols() {
let p = Point::new(r, c);
if self.get_neigbours(&p).iter().all(|n| self[n] > self[&p]) {
res.push(p);
}
}
}
res
}
fn find_basin_size(&self, p: &Point) -> u32 {
let mut used = HashSet::new();
let mut q = VecDeque::from([*p]);
while !q.is_empty() {
let p = q.pop_front().unwrap();
used.insert(p);
self.get_neigbours(&p)
.into_iter()
.filter(|p| !used.contains(p) && self[p] != 9)
.for_each(|p| q.push_back(p));
}
used.len().try_into().unwrap()
}
}
impl Index<&Point> for Floor {
type Output = u8;
fn index(&self, index: &Point) -> &Self::Output {
&self.floor[index.row][index.col]
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
struct Point {
row: usize,
col: usize,
}
impl Point {
fn new(row: usize, col: usize) -> Self {
Self { row, col }
}
}
pub fn smoke_basin_1(test_mode: TestMode) {
let floor = parse_input(test_mode);
let sum: u32 = floor
.find_low_points()
.iter()
.map(|p| floor[p] as u32 + 1)
.sum();
println!("{}", sum);
}
pub fn smoke_basin_2(test_mode: TestMode) {
let floor = parse_input(test_mode);
let mut basins: Vec<_> = floor
.find_low_points()
.iter()
.map(|p| floor.find_basin_size(p))
.collect();
basins.sort_unstable();
let sum: u32 = basins.iter().rev().take(3).product();
println!("{}", sum);
}

100
src/t09_smoke_basin/prod.in Normal file
View File

@ -0,0 +1,100 @@
9987653234589654549865432101235789979765459832101234567943986543101239874321012932397319985214896543
9898541015678943239876973876545699868954398765212345678932399654592345965432129891985498764323789432
9765432126789654123998765987897789759893219894333656789543498986989957898543498789976579895454678943
9879656737899797434589986798998897646789398989454789898959987899876899987654989678899699976565789656
8998767945678996545678997999569965435678987678967897947898765698765678998799878466798989988676898777
7679898998799987676789398998439876323999876567898976736569894329954569789987964345987878999788999888
5569959349898799787892139987321987549898765456789765623478989497542345699876543239876567898999998999
4398943245999659898993045996210499698759876345678954212349876989321234589876455123954453567899987897
6997832134678945959889159895432349999898763234567894303456965878943445678954321019843212456789976995
9876543265789434345678999789943498899987654126898985424579854567894578789986532129654325677899854789
9987654378894321256789987687894997789976543245789876539798768978965989897898763298767534798998765699
9998867459943210167899976576789876678898674689894988798999899989876999976987654349879545899999876789
8989978967894345234989895434598985568789788799933299987988989999987899895498765767989656987899987890
7679989878965976349876789323987654345699899899321012976567678998998998789329876878998767896789899951
6567895999876765498765678912396543236899956978932129854323567897899989698934998989679878945698789543
4367896989989896989824567893459874345789543569549299765434579945799877567895789997543989432397678954
6456789678998989873213678994598765456795432678998989879765678936998765456789896789732394321234589899
7867894569987678965302759789679876567894321799997878998976789029876744348978945699901298636346698778
8998923459876567973212345698789997678965433899876768987897899998765432124567934989893397545458975667
9769212598765459895463456789894398789877549998765656346789998769876545635688949879799498656867894356
9854324987654346789654577899943239899988698999854341247898987653987678986799198765678999987978965457
9985459876532235678968688987652124989998796598763210123567897632598789987891019654569998798989976768
9876598754321014567999799999873015678999987679874331234898998543469893498989198543456989659497899879
4987999865432323456789899998754123789687798798765765345789239754567912989678976432345678943236910989
3999998978543476567896978899875234896545679969896889656890198765678909876549876321234567894345899894
9899987987654587678965356789986346789639889345987992987999349989999999995434965430999698995456789763
8789995398765698789643235894398956998798999239899893998978969999898989984329876999878989976568997654
7679876219876789896542129901239899889897778998765789879569998998787878965510999876567678987879998795
6569997901989891987843458913498788778976667897654678965456987997676567894323987955476567898989989989
5498789892399932398954567894987654567895456789642567894235695987543478999499876545313456789899876567
6987678789999543459987678959876543679932378996543456965123984399864569998989985432101239898798765456
9876546679898954567999789546987621767891567789654587893239973298765678987879876543212345995697654367
9965434569767895978999893234598832348932345698765698954398764569976789656568987654323476789987743256
8954323698956789899998942195679953567893478999876799965499865678989896543459999866455678999976551045
6561012987545698788987893989998769878987567896987899896989987889298985432198754986566789678985432136
7432123498699987667896789878899989989987679975699989689678998994367965321096563497678994567986544357
6543434679987893459945698766789994597899789654599876575569899765459876432985432398989323456987655456
7654545689876789967897987655679913456789898765989975433456789876767989949874321019998910127898767667
8965656798765667898998986543489894768998999899879874321387899989898998899865432125987891238989898798
9878969899874356789999997432356789899567899999867965434458989999999987678976743434976989349876999899
4989878998543235678989854321247895912456789998759876545789678949899998789987854589865878998785679978
3497989987654146789679765432346954323457991987643987856894589439789769893498767679764567987654598767
2356799998763259895434996566459899496567890987732298997893499998698954902999898798943458976543987656
1345998759998768999323987878698798989698999896521019498902578896587893219899999987654569998651098945
2349887646789878998909998989789657478989298765432329399312456789476789399789999898765878979732129857
3498765434899989987898999999896545365679109876543498989423969893345995987678989769876989569843598767
4599894323987899876667899899987621234568914997656987878944878932239894598547568978989895479955679878
5689989210196998765456789789876210145679323498979766567895989210198795987832456899998794398766789989
6799878923965449887567895698765421234789459989898753456789994345989689976541368954989689239877899999
7899769999874335998789989899876532365678998876789866567899985459878599987430267893976578999988998989
8998958989910126999895678916998645466989897665789877689939876598766469898521256799865456789799987879
9987847768921237899934689105679656567898786544598999994324987987654349765432345987674347889659876567
9876426457892357998925693223998769779987654323987887889212998998743234978545469876544235678998765458
8765212346895468987998789339899878989876543105986455678929899999832123987676569998432123789989896345
9893101287896778956789895498799989294987654219875334599998798987654016898987978987654234599868987456
7654242398969889545678976987689990123499864329883223678998657998762145679398989398766345678956897678
8765357899954995434569989876578921934987986598761019999999736799763234789239799219875458789345999789
9876459987899894323678998875467899895976797987652128789989945689654756894345678998986567891239899899
9987699896798789434789897654323456789985698976543234567968896798765897895656799997897678910198776999
8998988765667678945896789876212345699996789987654345678959789989878998976767899976798989321987665678
7999876554354588996945679877103456789869899898765458789345678969989329987878999865659796499895434789
6789987432143456789439998765213467899953998769896569994234569658991012998989998764546679987654323890
5679896541012345799598789874327878969892987654987678996545678967989129879099876543234567899795434891
4598765432123569898987678985456789459789898743498789989756789879878998765146998732125698901989545789
3759986543234678987656589876769898698657789992109899978997894998767899943237984321016989992978956897
2349898757655989898743456989899999998745678989234998868789923499656798895349875432234678989867897896
3498769898767995679754687894999899899987789878976987654689934987543236789767976543345689876546789965
5957656999898994889898798943499789789998898769897998743476899876432125698978989954496898765435789954
9843545989919989998989899954987679678969987658789876532545899976521034567899999795987999876323567893
8732135678909878967879999899995564599357897645679987621236798765432145678978988689999899943213679954
7643014789998765456568998788994323788969919876798768730345679896543267989569876578998798654323789875
8654123898999654345457897656789515677896323987898654321656789987655358993459875467987698765434892996
8766234567898743212345789534994323456965434598989765442767895698767467892398764345696549876545993989
9874345678999832101234679423989434699799565989679877653458954349878878921987653234986539987656789878
9965657899298743212345894212878945987678976976547989765669765689989989439876543123497698998767899764
9876798921019654323456982104567899866567899867435699878878998799997699545998653234598987899889998943
3989899892198767434569877323456998753467997654526789989989219989876578976789854447679996867995387891
1298956789299878565678965434567897542346789643214567999894329878964347897899965557798765456894216789
0987645898989989678789876545878965321578998759323456999765498767894256798949876678987654345892105899
2698766987678997989898987856989954320789869898937599878978987856943145789432988989998743236789324678
3499989898598965397997698967898765434899754997999987567899876645894235699921299096999984345679434567
9988998765467893236789579989999887545678975986878976458998765434689346789890932125789965456796545678
8976899754359992125679456799999999698789876795467891367999986323478956896799843234699878767899696899
7895789973298989014568967898989439899994987984356910179898797212667897975689754348789989878998789999
6434890989987878923699878987678921999893499878268899298789598601457898954579765678895699989349899678
5423921298756867895789999896569890198789989862156788965678987542356789343467999789934989993210998569
8737893989943458976799998765476789987679876545034567894589999753478993212979878992129878994921987678
9656794979899569987999899654345679876543987632123478943499898764589432109898769994939769789892998989
8767999868767989799898698765476789999432499843234569654986789878997643498789356989898654698789899194
9879898657657895698795439876987899987654678954545678999875688989019759569653239876789543987698789012
6998775432348894239679921997898978998767989876789789789654567899998967898764699865678952986567678929
5987654321256789197598799998929867899898999987899897698973456789787978979978989976789769878454567898
4598763210368898986487678999213456789949989998921954567892567998656899754989978987899879764323678986
3459875391239897697396568789428589890129878999530123678943688974545799843499867898963989875214567895
2345965485446797543212345678937678932398767985432654789764569763234899754998756789642199932108678914
1459879876568999852101267899548789654987659876543465999875678954356999879877645678921019853219989103
2345989987879998763254345678959898767899536987754576789986789765456895998763234599934329764324598912
3589996598989899854395456789967999878998745699865679896597897987587894309854123578896449895434567893
4678965409599798765986577899878986999899656789986789965498986987698963219767012456789567976545998954
5789654312345679876897689910989754346798787891099899876899765698999654397654323767897689987656899865

View File

@ -0,0 +1,5 @@
2199943210
3987894921
9856789892
8767896789
9899965678

View File

@ -0,0 +1,96 @@
use std::{
collections::{HashMap, HashSet},
io::{BufReader, Read},
};
use crate::types::TestMode;
#[derive(Debug)]
struct Input {
lines: Vec<String>,
opening_chars: HashSet<char>,
opposites: HashMap<char, char>,
}
fn parse_input(test_mode: TestMode) -> Input {
let f = test_mode.input_file(file!());
let mut buf = String::new();
BufReader::new(f).read_to_string(&mut buf).unwrap();
let lines = buf.split_terminator('\n').map(|s| s.to_owned()).collect();
let opening_chars = HashSet::from(['(', '[', '{', '<']);
let opposites = HashMap::from([
('(', ')'),
('[', ']'),
('{', '}'),
('<', '>'),
(')', '('),
(']', '['),
('}', '{'),
('>', '<'),
]);
Input {
lines,
opening_chars,
opposites,
}
}
pub fn syntax_scoring_1(test_mode: TestMode) {
let input = parse_input(test_mode);
let score_chart = HashMap::from([(')', 3), (']', 57), ('}', 1197), ('>', 25137)]);
let mut score = 0;
for line in input.lines {
let mut stack = vec![];
for s in line.chars() {
if input.opening_chars.contains(&s) {
stack.push(s);
} else {
let old_char = stack.pop();
if old_char.is_none() || old_char.unwrap() != input.opposites[&s] {
score += score_chart[&s];
break;
}
}
}
}
println!("{}", score);
}
pub fn syntax_scoring_2(test_mode: TestMode) {
let input = parse_input(test_mode);
let score_chart = HashMap::from([(')', 1), (']', 2), ('}', 3), ('>', 4)]);
let mut scores = vec![];
for line in input.lines {
let mut stack = vec![];
let mut valid = true;
for s in line.chars() {
if input.opening_chars.contains(&s) {
stack.push(s);
} else {
let old_char = stack.pop();
if old_char.is_none() || old_char.unwrap() != input.opposites[&s] {
valid = false;
break;
}
}
}
if valid {
let mut local_score: u64 = 0;
stack
.iter()
.rev()
.map(|c| input.opposites[c])
.for_each(|c| local_score = 5 * local_score + score_chart[&c]);
scores.push(local_score)
}
}
scores.sort_unstable();
println!("{}", scores[scores.len() / 2]);
}

View File

@ -0,0 +1,110 @@
{{{[(<<[[[<<([[]()]<()<>>)((()()))>([(()><{}[]>]({()[]}))>{[{(<>[]){{}[]}}<({}()){{}[]}>][<<(){}>(()<>)>{[{
{<<((({{<<[<<{[]()}(())>([[]()])><[[{}{}]<[]<>>](({}())[{}<>])>][([{[]{}}](<<>{}>{()()})){({[]{}}<<><>>}{({}
(<[{<{<(<[[<<<[][]>(<>)>><{<(){}>{[]{}}}<{{}[]}{()[]}]>][<([{}<>]<[]<>>)<{[]()}[()<>]>>[{{<><>}<<>{
([{([{{{<[[{[{{}()}<{}[]>]{{<>()}<()>}}{<<{}()>{<>{}}>}]]<(<<[<><>]<<><>>>{<{}()>({}())}>){(<{()<>}><<<
{(<{[{[{([<<{<()()>((){})}[[{}{})<()[]>]>>{([<{}<>><<>[]>]<{<>[]}[{}<>]>){<[<><>]{{}{}}><(
[<([<(<{[<[{<<()[]>{()[]}>}{<[{}][[]{}]){[()()]{<>[]}}}]>{(<(<[]()><{}<>>)(<{}<>><{}()>)><<(<>())>>)
(<{<[<[(({([(([][])([]())){(()[]){[][]}}][({[][]})[[[]{}]({}[])]])}{{{[{<>()}{()[]}](<{}[]><[]<>>)}<(
<<(({([[[([{[[()()]{<>[]}]<[<>[]]({}[])>}]((<[[]())(()<>)>(<{}()>([][])))<([<>[]](()[]))>))[<[[<{}<>>([][])]
(({[{[{<(({([{[]{}}(()())]{{()()}<<>()>})([{{}{}}[{}{}]]<<(){}>[()<>]>)}<{{(()[])}}{{[[]()
[<{<<<{<[[[[(((){})(()[])){<[]{}>}](<[<>[]][<>()]><[()<>]<<>[]>>)]({{[<><>]}<<<>{}>[{}<>]>}[{{{}{}}([]{})}
{[<({<{{<([(<<<>{}>{<>[]}>{{()<>}<<>{}>})(<<[]>((){})>)][<[<()<>>[()]]<((){})<<><>>>>[<{()[]}<{}[]>>]])>}{{<
([[<{([{<<[{<[{}<>]{<>{}}>{[(){}][()()]}}<{(<>[])(<>())}([<><>])>]>{{{<([][]){<>()}>{([]()><()()>}}{{({}()
(<[[({{<([[[[{{}<>}[<>()]][(<>[])<<>()>]]]([([[]()][()()])]([[{}[]]<{}[]>]{[{}<>]{<><>}}))))>(
<<{[([(<<({{([{}[]]<<>()>)<<()[]>[{}<>]>}({(<><>)[{}()]})}[{<[(){}]>{{<>[]}[<>{}]}}[<(<><>){{}[]}>{{<>{
[<({[({[<{[(<[{}()][{}<>]>{{()[]}(<>())})({{{}<>}<[]()>}{<()<>>})]}[<<{{[]}({}{})}([[]<>][()<>])>>[[[
(<<[[<{<(<<<<[()[]]<<>()>>({[]<>}[[]()])>([<<><>>](([]()){[]()}))>{<[{{}<>}<<>[]>]<<<>><[]()>>>{
<[[{[<({{[{(<([]())<{}[]>>(<<>{}>{<>})){[{[][]}{<>[]}]{[[]{}]<[]>}}}<<(([]{})[<>{}]){[{}()](<>{})}>[[([]{})[{
([(<[<[[{([[(({}{})[(){}])([<>[]]{()[]})]([[()<>][<><>]])]{[<{[][]}[[][]]>{<[]()><()[]>}]<[(<>())<<>{}>]>})}
([([{<<{{{({<<<>[]><<><>>>[[<>[]]<[]()>]}<<{[][]}[{}<>]>{[<>()]{()[]}}>)[[[<<><>>{(){}}>[({}[
[[<[[({{(<[{<{[]{}>>({[]()}{[]<>})}{(([]{})[<>{}])[(<>{})<[][]>]}]>){{<[{<{}<>>}<([][])([]<>)>]((<(){}>(()
([[{<[<((<{([{[]<>}({}[])]){{[[]]{<><>}}[<[][]>([][])]}}{[(((){})([]())>]{{{[]()}<<>()>}}}>){<[(
{<(<{{<{[{<{{<(){}>{<><>}}}[{[{}<>][{}<>]}[[[]{}](()[])]]>}][<<<{[{}[]]<()()>}(((){})[()[]])>)((<(()
{([<{<{([(<{[{{}[]}[(){}]]<{()}<{}()>>}[[[[]{}]{<>[]}}]>{[[[<>]<<>()>]{<()<>>{[]<>}}]})<<{[{<>()}<{}<>>]
{<<(<[<[[[[{{(()())([]<>)](<<>>({}()))}<[{<>()}[()<>]]>]({[[[]()]<()<>>]}[[<[]<>><()<>>]{<{}()>[
[<{(<<[<[(<[{(<>{})<{}()>}]<[[()<>]{<>()}]>>)]](({<{<[()()]{[]()}>((<>())(<>{}))}{[<{}()>]<(<>[
[<({({[[[<{<[[()<>]<<><>>]{([]<>)([][])}>[[(<>[]){(){}}]]}{{<{<>{}}<<>>>(([]{})[[]<>])}[<(()[]){{}{}}>]}>[{
{(<<{<[{[{({{[<><>]{{}[]}}}(([{}[]]<[]<>>)})[<(<[]{}><<>()>)[<{}<>>[<>[]]]>(<((){})[{}<>]>{(
[<(({<<([{[{{{<><>}([]<>)}}]}](<(<[([][])<{}[]>]{{{}{}}[{}{}]}>{(<[][]>{{}{}})})>)){[([{{<[][]><()>}
[({[[[(<<((<(<{}()>([]<>))(<()()}<{}[]>)>(([{}[]](<>{}))[{[]<>}(<>[])])))>>)][<{({<[[<{}{}>{[]()}]{(<>{}){
{(([((([<[{[(({}[]){()<>})[[{}[]]({}())]]{<[[][]](()<>)>{[{}[]]{[]()}}}}<{(<<><>><()<>>)<({}[])
{([{{[[{<({([[<><>]<{}<>>]<[()[]]<()[]>>)}[[{[{}{}]{{}{}}}]{[{[]{}}([]{})]{(()())[<>()]})])>}]<
([<[{{{([{{<[[{}{}]]{<()()>}>{{{()[]}([]())}}}<<{<[][]>{<>{}}}>{[[{}()]([]{})]{([]{})(()())}}>}])<<<((<<{}{}
[{{[<[[[<({[[<(){}>{{}[])]{{[]<>}([])}]}<({{<>()}[{}{}]}{[()()]{[]<>}})[{[()<>][()[]]}]>)>[<(<{{
(<{{({{{[({([({}[])<{}{}>](([]{})[{}{}]))})[(<<((){}){()<>}>[[[]<>][{}[]]]>{<([]){{}<>}>([[]{}])}){[(((
<[<{{(<(<<[{([(){}][()()])((<>())]}]>>)>)}[{(({({[[{(){}}]][{(()()){{}[]}}<(()())(<>())>]}{([{[]<>}<[]<>>]
{({<[[<<{<{(({<>{}}<<>{}>))[<<{}()><<>()>><<(){}>({}())>]}<(([{}[]][{}{}]){<{}>}){[<[][]>((
(<[{{[{([<<<(((){})([][]))>([[<>{}])[<{}()>{<>{}}])>>{{{{<[]{}>[()()]}[(<>{}){<>{}}]}{([[]{
{(<[{({<<(<<<(<>())[{}[]]>>{([()()]({}{})){<[]()>[[]<>]}}>[[<[{}{}]{{}<>}>{[<>()][<><>]}]{<[<>][[]<>]>((<>{
{[<<(<<({(<<[{<>[]}]{[{}{}]<[]()>}>(((())<<><>>)((<>{})[{}[]]))>[<({<><>}[[]{}])[<()[]>{()()}]>{{({}[])({}()
<<<(<((([<{[{{{}<>}{{}[]}}[(<>)<<>[]>]]<[([])]<{()()}{<>{}}>>}><{<[[[]<>]<{}<>>]>[(({}[])<[]<>
{<(([[{{[({(([[]{}]<{}()>)<([]())[[][]}>)<<<<>>{()[]}>[<<>()>]>})<<[({[]<>}[[]<>])<<{}()>({})>]{<<()[]>(()[
([<([{<[<{<([<()>({}[])](<<>{}>{{}{}}))<<({}{}){{}()}>[([]{})[[]()]]>>{{<[()<>]{()<>}>((<>())[{}<>])}<[(<
<([{({({<({[[(()[])[[][]]][(()){()[]}]](<{()<>}(()())><[[]()]([][])>)}{[{[<><>][{}{}]}<(<>[]){{}[]}>]
(({[<<[[{{<[{{[][]}(()[])}{<()<>>}]([<[]<>><{}<>>]{<{}()>[<><>]})><{{<<>{}>[[]{}]}{<{}<>>[[]{})}}<<<{}<>>
<{{<(({{(<[<<[[][]]{[]()}>><<{()[]}{<>)>(({}{})(()()))>]{(<({})({}{})>([()()]<{}<>>)){([{}[]]([]{}))[<[]
{({[<<[[<[<(<{<>{}}{{}()]>([{}[]][()[]])){{[()[]]{<><>}}({<>()}[<>()])}><<{[()<>]<[]>}<({}[]
<{{<<{<{<([(([<>()])<({}())<{}[]>>)[[(()())[()()]]{{()()}<{}()>}]])<[([<{}{}><[][]>])[[<[]<>>{[]<>}
<{<([[<{[{([[[[][]]<[]<>>][({}()){()()}]]<(([]())[<>[]]){[()<>]}>)[[<<{}<>>([]{})>[(()<>)[[]()]]]]}((((<<>>
[[{[<<([[({{<<{}<>>{<><>}>[<(){}>([]<>)]}<{<{}[]>{()[]}}((<>{})<()[]>}>}[<([[][]]([][]))({()()}<()
[{[[{(<({(<({({}<>){{}[]}}{<<>[]>{()[]}})<(({}())[[]{}])>><[<{{}[]}>{<[][]>[()]}]{((<>{})(<>
{{<{[[[(<({<(<[][]><{}<>>)[(<>{})<<><>>]>})>)]]]}{([[<<{[{<<()[]>({}{})>({{}()})}]<{[{()()}]<[[]]<()>>
[{{[<((<<(({{[[]()]([]())}{<[]{}>[{}{}]}}<({<>{}}{()[]})<{{}}{{}[]}>>)(<<<{}[])<[]()>>({[]
([[[<(<([{(({{{}[]}[{}]}[[()()][{}{}]]}({(<>{})({}())}({()()}([]<>)))){{({()<>}(<>[]))[(()())<[][]>
{([<<{<(<((({({}[])({}[])}([<><>]([]())))<<<[]{}><<><>>>({{}[]}{()[]})>}(<[({}[]){<>[]}](<[][]>{{
{{(<{<([[<[[(((){})<{}[]>)[[[]<>][<>()]]]{((<>[])(<>())){<<>>([]{})}}]<[([[][]][{}])]>><(({{<>()}}[<[][]>])
{[[{<<<{{{[[(<(){}>{[]{}})]]}({<[[{}()][{}{}]]{({}[])[{}<>]}>{{{[]<>}[<>[]]}<{[][]}(<>[])>}})}{[<{[([][]){<
<[<{<[{<({(<<([][])<()[]>><[<>()]{{}()}>)<([()<>]<<>{}>)(({}<>)[()<>])>)})>}][(<<<[[({{}()}{{}[
<{{(<<{[{<{{{{[]<>}{()()}}{(<>()){()[]}}}[{(<>[])}(({}[])[()[]])]}<<[{<>()}]<{<>()}<{}()>>>{((<
{[<[{{[[<[{<<[[][]]<{}[]>>><<<()[]>({}[])>>}([[[[][]]]((<>())<<>()>)](<[{}()]<[][]>>({<>[]}{[]()}))>]{{<[{{}
<([[{[{<<(<{<[{}[]]{(){}}>(<{}<>>{<>()})}{(<(){}>{[]))((()<>)[{}[]])}><(<<(){}><()>>){(<()<>>(()()))<({}())[
{<(<<{<[<<<[[<[]{}><[]()>]<{{}{}}{{}<>}>]>[[(<<><>>((){}))]({<()()><{}{}>}({<>()}))]>>]([(({<{()()}[
([{[([({[[<[{(<>[])<<>()>}[[{}[]]<[][]>]]><(<<()<>>({}<>)><(()[])(()())>)({[[]{}]{<>()}}{[[]
[(({([{[<<[{{((){})({}())}}]{{{<()<>>[[]()]}((<>[])(<>()))}[[[<><>]([]())]({()[]}{()[]})]}>>({[[<<
<{{(([[[<(<(([[]()]<[]{}>))[<[{}{}](()())>[[()<>](()())]]>{<{<()<>>}(<{}<>>[[]()])>})>[({<
[<{([{{<<{{(<([]{})[<>{}]>[<()<>><()[]>])({(()<>)<<>[]>)(([]{})([]<>)))}}>[{[({({}())[(){}]}<(
<([<<[<(<<<<({()[]}<{}<>>)[[()[]][[]()]]><({{}{}}){[[][]]}>>{[((()[])[[][]]>{{()()}{()<>}}]<{<
<((<{<{<{<<[[<<>()>{<>}]{({}[])<<><>>}]([{()<>}{[][]}]([[]<>](<>())))>[<<[[]<>]<()()>>[{()()}<(){
<<{[{[{[<{{<<[<>()]<[]<>>>[[()()][{}{}]]>([{{}[]}<()<>>])}[((([]{})[[]<>])<<{}{}>{<>[]}))]}><[{(
{{{{<<[{<{(({<{}()><[]<>>})[{<[]{}><()()>}{<<>[]>[[][]]}))<{<[<>[]][[]{}]>{[(){}][[]()]}}{{
<<{([({{[{{<[<[][]>{[]<>}][<[]()>]>{{[{}][()[]]}({{}[]}[()[]])}}{[[<<>{}>](<()()>)][<[[]{}]<{}{}>>
[{{<{([[<{(({<[]{}><[]()>}<<()<>><<>()>>)<{(<>()}[{}<>]}>){(<{<>{}}({}[])><{()<>}>)}}[({(<[]()>({}<>))<[[]
{{{({({<{<{[[{[]{}}{[]()}]<{{}()}<{}[]>>]({<[]<>>[[]()]}<[<>[]}<{}<>>>)}{<<(()[])<()[]>>[(<>[])<()[]>]>(<[[](
<{[(([[[([[<[[{}()]([]<>)][(()[])[<><>]]>(([<>{}]({}()))<{{}}[[][]]>)]({{<{}[]><[]()>}({[]{})[[
{(<({({(<(<(([<>()][{}<>])<{()<>}<{}()>>)[[[()[]]<[]{}>][[()<>]<[]{}>]]>{<<<[]><[]()>><(<><>)<[]{}>>>})(([
<{<(([(<<<<<{(()<>)}{<()()>(<>[])}>>>>>){{(<<[[{<>[]}(<><>)]<{[]{}}>]>>)[([{[[<><>]<()()>]{(()[])<<><>>}
({{[(<([({((([()[]]{{}{}})[(<>{})([][])])){{({<><>}[()()]){<{}[]>[<><>]}}}}{(<[<<>[]>]>><<[{()}<()()>]
([{[[([{<[({{({}[]><[][]>}}((<()()>[<>[]])<({}())({}())>))]<[[[(<>{}){[]}]{{[][]}{()[]}}]]<{<<{}<>><<
({{<[[({{((<[{()<>}[{}[]]]([[]()](()[]))>{[{()[]}{{}[]}][{{}{}}{[]{}}]})({{[{}](<><>)}[([]()
[<<{[{({{[[{{<[]{}>[<>[]]}<({}[])[()[]]>>{[[<>()]{{}[]}][{[]{}}{[]()}]}][<<([]()){[]<>}>((<>){{}()})>[{{{}(
(<{[{<{[[[<[<{{}<>}<()>>{[<><>][()()]}][<[<>()]{{}{}}>([[]())[[]<>])]>]{[(<[()()]{<><>}>{[{}[]]<
<{[[[{(({{<({(()[])(<>{})}(<[][]>[<>]))({([]<>)<<>[]>}<[<><>][()()]>)>[<[[<><>][()()]](<()>[()<>]
[<[([[<<[{((<[<>{}][{}[]]><(<>{}){[]<>}>))}][{{<<<[][]><{}<>>>><({()[]}<()[]>)>}<([<(){}>({}())]<[{}][()]>)[
[([({{[[{{[[{{{}{}}<[]<>>}]]<{{{{}{}}<<>()>}((()())({}[]))}[[<(){}>(<>{})][[<>{}]{{}<>}]]>
<<[{<<([[({([[[][]]{{}()}])[[{{}[]}<{}()>]{[(){}]{<>[]}}]}([{{[][]}>]<{{()[]}<()[]>}<(()<>)
[[[{(<[[{(({<({}())>{{<>{}}[{}{}]]}<{{()()}[{}<>]}[<{}<>>(<><>)]>))<<<{<<>[]>}{[{}()][{}{}]}>[<[(){}][{}{}
{<([{[[([{[{(<()()><<>[]>)(({}{})[{}<>])}[<<()[]>[()<>]>{({}<>){()()}}]]<<{(<>[]){()()}}<{{}[]}(<><>)>>((
(({{<(<({[[{({{}[]}([][]))[{[]<>}<<>[]>]}[<{[]{}}([])>]]{{([<>[]]({}()))(<()<>>{[]{}})}}][{<<{<>()
{{[<[<(([<{[([()()][[]<>])[<()<>>{()[]}]]}>[{({{()()}([][])}({()}(()<>)))[[({}())]{{<>()}[<>]}]}]]{<{({({
{{([([({[<((<{<><>}>{(<>[])})([({}{}){<>()}]))<[{<{}{}>{<>[]}}]<(<(){}>{()<>}){<<><>>}>>>[{[
[<{<[(<{{[((<[<><>]{(){}}>(([]<>)[()()]))(<{{}()}[{}[]]>{<<><>>{()<>}}))({[<()()>{<><>}]<{[]<
<((([(<[{<<[<<<>()>({})>][((()[]))<{{}()><<><>>>]>>({[[[<>{}][<><>]]({{}()}<{}{}>)]}((<<[]>
[[{({[({[[[[<[<>()]{<>{}}>]{([<>{}](()<>))[[{}<>]({}[])]}]{{{(<>{})({}[]]}}[{[()<>]{()()}}]}]]<<{[((<>())<()<
{<{{{([(<([<[{<>()}{<>{}}]<([]{})<[][]>>>([<(){}>[[][]]]{<()<>><<>[]>})]<<<[()()]>>[([()()]
[([[(({[[{{<{(<>{})<(){}>}[({}{})[<><>]]>({<<>{}>[<>{}]})}}[[<{<()><{}{}>}{{<><>}}>([[()()]{{
{{[({({([([<({()}([]()))>[[(<>[]){<><>}][{[]{}]<{}>]]]{{[({}{}){<><>}][[[]<>][<>[]]]}((([]<
{{<<[<<(({[[{{{}()}({}{})}<{()[]}{()()}>][{{<>{}}[()<>]}([{}[]]{{}[]})]]<[{{[]<>}{<>{}}}{[<>[]]<[][]>}]
[{<({({<{{({{{<>{}}<{}()>}{(<>())[<>{}]}}[(<<>{}>[{}])])((<<<><>><{}()>>(<<><>>([])))(<([]<>)>{{
{[<[((<{{[{{<({}{})>}<((<>)<[]<>>)[[<>()]{{}}]>}]}}>))]({[{{([[<<<{}[]>><<<>()><<>[]>>>[{{(){}}}<[[]{}]({}
<<<[<({[(<(<<(()<>)<{}{}>>{([]<>){<>{}}}>{{<{}<>>(<>{})}<[()<>]>})<{[({}())[<>[]]]({<>()}<<>{}>)><{
({<<[{<<[{{<([(){}]<<>()>)<<<>()>[[]()]>>(<{<>()}([]())>{[[]<>](<>{})})}}]]{<(<<[<<><>>((){})]{
<([{{[{<<[[{{[{}[]][()]}<[{}()][[][]]>}}[<{[<>[]]{{}()}}>{(<()()><[][]>)({()<>})}]]<[<[<{}<>>([]<>)
([<<[[([(<<[({()<>})<{[]()}{()}>]({{()<>}})>{[([<>()]([]()))[{[]{}}]]}>{{{(<[]()><[]>)<{{}{}}[{
([[{[[{<{[{<({()()}[<>{}])<<{}{}><<>()>>><[[<>{}](<>()]]<<[][]>{<>{}}>>}][<{(<<>()>[()[]])<{<>{}}
<{<<{<((([{[<(())<{}[]>>]<(<{}()><<>{}>)<<<>{}>>>}<{{<<>()>{{}()}}<<(){}>>}[[{[]()}({}())]((<>
([[([(<[<<{[<[()<>][[]()]>[[[]<>]<()()>]][[(<><>)]<<<>>(<>[])>]}><{([(<><>)]{[()<>}})}>><<[[(<
{{(<<({<([([{{[]()}}([<>{}]{{}()})])])[[[[[[<>{}>({}())]{{[]{}}({}())}]]([{{<>}(()<>)}[{<>()}<[][]>]])](<[[[
((({{<[<<{{{<<[]()>[<>[]]>[({}[])([]{})]}<<[[]{}]<()<>>><({}())(())>>}[<<[(){}]<[]()>>[{{}<>}
[<([<{[([[[(<[<>{}]({}{})>([()[]]({})))([{()()}{()[]}]<[<><>]{()()}>)]({({(){}}{<><>})<[<>()]{{}()
<(({(<[[{[{[((<>())[{}])[[<>]{<>()}]](<{[][]}{<>()}>([<>{}>{[]<>}))}[[({<>{}}{<>()})((()[])<<>
<{{{{<{[[<[({{<>[]}(()<>)})<(<<>{}>[(){}])[{<>{}}{<>{}}]>]({[{<>()}<()<>>]{<[]()><{}()>}}([

View File

@ -0,0 +1,10 @@
[({(<(())[]>[[{[]{<()<>>
[(()[<>])]({[<{<<[]>>(
{([(<{}[<>[]}>{[]{[(<()>
(((({<>}<{<{<>}{[]{[]{}
[[<[([]))<([[{}[[()]]]
[{[{({}]{}}([{[{{{}}([]
{<[[]]>}<{[{[{[]{()[[[]
[<(<(<(<{}))><([]([]()
<{([([[(<>()){}]>(<<{{
<{([{{}}[<[[[<>{}]]]>[]]

View File

@ -0,0 +1,157 @@
use std::io::{BufReader, Read};
use crate::types::TestMode;
const NUM_ROWS: usize = 10;
const NUM_COLS: usize = 10;
#[derive(Debug)]
struct Input {
map: Vec<Vec<u32>>,
}
fn parse_input(test_mode: TestMode) -> Input {
let f = test_mode.input_file(file!());
let mut buf = String::new();
BufReader::new(f).read_to_string(&mut buf).unwrap();
let mut map = Vec::new();
for line in buf.split_terminator('\n') {
let m = line.chars().map(|c| c.to_digit(10).unwrap()).collect();
map.push(m);
}
Input { map }
}
fn flash(m: &mut [Vec<u32>], fm: &mut [[bool; NUM_COLS]; NUM_ROWS], r: usize, c: usize) -> u32 {
let dirs: [(i8, i8); 8] = [
(-1, -1),
(-1, 0),
(-1, 1),
(0, -1),
(0, 1),
(1, -1),
(1, 0),
(1, 1),
];
if fm[r][c] {
return 0;
}
fm[r][c] = true;
let mut num_flashes = 1;
for (d_r, d_c) in dirs {
let r: i8 = ((d_r as isize) + (r as isize)).try_into().unwrap();
let c: i8 = ((d_c as isize) + (c as isize)).try_into().unwrap();
if r < 0 || c < 0 || r as usize >= m.len() || c as usize >= m[0].len() {
continue;
}
let r: usize = r.try_into().unwrap();
let c: usize = c.try_into().unwrap();
m[r][c] += 1;
if m[r][c] > 9 {
num_flashes += flash(m, fm, r, c);
}
}
num_flashes
}
fn print_matrix(m: &[Vec<u32>]) {
for row in m {
for c in row {
print!("{}", *c);
}
println!()
}
println!()
}
pub fn dumbo_octopus_1(test_mode: TestMode) {
let mut input = parse_input(test_mode);
const NUM_STEPS: usize = 100;
println!("Before any steps:");
print_matrix(&input.map);
let mut num_flashes = 0;
for s in 1..=NUM_STEPS {
let mut fm = [[false; 10]; 10];
for r in 0..NUM_ROWS {
for c in 0..NUM_COLS {
input.map[r][c] += 1;
}
}
for r in 0..NUM_ROWS {
for c in 0..NUM_COLS {
if input.map[r][c] > 9 {
num_flashes += flash(&mut input.map, &mut fm, r, c);
}
}
}
for (r, row) in fm.iter().enumerate() {
for (c, cell) in row.iter().enumerate() {
if *cell {
input.map[r][c] = 0;
}
}
}
println!("After step {}", s);
print_matrix(&input.map);
}
println!("{}", num_flashes);
}
pub fn dumbo_octopus_2(test_mode: TestMode) {
let mut input = parse_input(test_mode);
println!("Before any steps:");
print_matrix(&input.map);
let mut s = 0;
loop {
s += 1;
let mut fm = [[false; 10]; 10];
for r in 0..NUM_ROWS {
for c in 0..NUM_COLS {
input.map[r][c] += 1;
}
}
let mut num_flashes = 0;
for r in 0..NUM_ROWS {
for c in 0..NUM_COLS {
if input.map[r][c] > 9 {
num_flashes += flash(&mut input.map, &mut fm, r, c);
}
}
}
for (r, row) in fm.iter().enumerate() {
for (c, cell) in row.iter().enumerate() {
if *cell {
input.map[r][c] = 0;
}
}
}
println!("After step {}", s);
print_matrix(&input.map);
if num_flashes as usize == NUM_ROWS * NUM_COLS {
println!("{}", s);
break;
}
}
}

View File

@ -0,0 +1,10 @@
8261344656
7773351175
7527856852
1763614673
8674556743
6853382153
4135852388
2846715522
7477425863
4723888888

View File

@ -0,0 +1,10 @@
5483143223
2745854711
5264556173
6141336146
6357385478
4167524645
2176841721
6882881134
4846848554
5283751526

View File

@ -0,0 +1,154 @@
use std::{
collections::{HashMap, HashSet},
io::{BufReader, Read},
ops::{AddAssign, SubAssign},
};
use crate::types::TestMode;
#[derive(Clone, Debug)]
struct Input {
graph: HashMap<Cave, HashSet<Cave>>,
start: Cave,
end: Cave,
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
enum Cave {
Small(String),
Big(String),
}
impl From<&str> for Cave {
fn from(s: &str) -> Self {
if s.to_lowercase() == s {
Cave::Small(s.to_owned())
} else {
Cave::Big(s.to_owned())
}
}
}
fn parse_input(test_mode: TestMode) -> Input {
let f = test_mode.input_file(file!());
let mut buf = String::new();
BufReader::new(f).read_to_string(&mut buf).unwrap();
let mut graph = HashMap::new();
for line in buf.split_terminator('\n') {
let mut s = line.split_terminator('-');
let a = Cave::from(s.next().unwrap());
let b = Cave::from(s.next().unwrap());
graph
.entry(a.to_owned())
.or_insert_with(HashSet::new)
.insert(b.to_owned());
graph.entry(b).or_default().insert(a.to_owned());
}
Input {
graph,
start: Cave::from("start"),
end: Cave::from("end"),
}
}
fn find_paths(input: &Input, path: &mut Vec<Cave>) -> usize {
let current_node = path.last().unwrap();
if current_node == &input.end {
// println!("{}", path.join(","));
return 1;
}
let mut paths = 0;
for n in &input.graph[current_node] {
let n = n.to_owned();
let is_small_cave = match n {
Cave::Small(_) => true,
Cave::Big(_) => false,
};
if !is_small_cave || !path.contains(&n) {
path.push(n.to_owned());
paths += find_paths(input, path);
path.pop();
}
}
paths
}
fn find_paths2(
input: &Input,
path: &mut Vec<Cave>,
small_cave_occurances: &mut HashMap<Cave, i32>,
) -> usize {
let current_node = path.last().unwrap();
if current_node == &input.end {
// let v: Vec<_> = path
// .iter()
// .map(|p| match p {
// Cave::Small(n) => n.to_owned(),
// Cave::Big(n) => n.to_owned(),
// })
// .collect();
// println!("{}", v.join(","));
return 1;
}
let mut num_paths = 0;
let has_double = small_cave_occurances.values().filter(|v| **v == 2).count() >= 1;
for n in &input.graph[current_node] {
if n == &input.start {
continue;
}
let e = small_cave_occurances.entry(n.to_owned()).or_default();
if *e >= 2 || (*e == 1 && has_double) {
continue;
}
if let Cave::Small(_) = n {
e.add_assign(1)
};
path.push(n.to_owned());
num_paths += find_paths2(input, path, small_cave_occurances);
if let Cave::Small(_) = n {
small_cave_occurances
.entry(n.to_owned())
.or_default()
.sub_assign(1)
};
path.pop();
}
num_paths
}
pub fn passage_passing_1(test_mode: TestMode) {
let input = parse_input(test_mode);
println!("{:?}", input);
let mut path = vec![input.start.clone()];
let paths = find_paths(&input, &mut path);
println!("{}", paths);
}
pub fn passage_passing_2(test_mode: TestMode) {
let input = parse_input(test_mode);
println!("{:?}", input);
let mut path = vec![input.start.clone()];
let mut small_cave_occurances = HashMap::new();
let paths = find_paths2(&input, &mut path, &mut small_cave_occurances);
println!("{}", paths);
}

View File

@ -0,0 +1,23 @@
start-YA
ps-yq
zt-mu
JS-yi
yq-VJ
QT-ps
start-yq
YA-yi
start-nf
nf-YA
nf-JS
JS-ez
yq-JS
ps-JS
ps-yi
yq-nf
QT-yi
end-QT
nf-yi
zt-QT
end-ez
yq-YA
end-JS

View File

@ -0,0 +1,18 @@
fs-end
he-DX
fs-he
start-DX
pj-DX
end-zg
zg-sl
zg-pj
pj-he
RW-he
fs-DX
pj-RW
zg-RW
start-pj
he-WI
zg-he
pj-fs
start-RW

View File

@ -0,0 +1,153 @@
use std::{
collections::HashSet,
io::{BufReader, Read},
};
use crate::types::TestMode;
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
struct Point {
x: u16,
y: u16,
}
#[derive(Clone, Debug)]
enum Fold {
X(u16),
Y(u16),
}
#[derive(Debug)]
struct Input {
points: HashSet<Point>,
folds: Vec<Fold>,
}
fn parse_input(test_mode: TestMode) -> Input {
let f = test_mode.input_file(file!());
let mut buf = String::new();
BufReader::new(f).read_to_string(&mut buf).unwrap();
let lines = buf.split_terminator('\n');
let mut first_input_stage = true;
let mut points = HashSet::new();
let mut folds = vec![];
for line in lines {
if line.is_empty() {
first_input_stage = false;
continue;
}
if first_input_stage {
let mut s = line.split_terminator(',');
points.insert(Point {
x: s.next().unwrap().parse().unwrap(),
y: s.next().unwrap().parse().unwrap(),
});
} else {
let mut s = line
.strip_prefix("fold along ")
.unwrap()
.split_terminator('=');
let f = match s.next().unwrap() {
"x" => Fold::X(s.next().unwrap().parse().unwrap()),
"y" => Fold::Y(s.next().unwrap().parse().unwrap()),
_ => panic!(),
};
folds.push(f);
}
}
Input { points, folds }
}
pub fn transparent_origami_1(test_mode: TestMode) {
let input = parse_input(test_mode);
println!("{:?}", input);
let mut paper_points = input.points.clone();
let mut new_paper_points = HashSet::new();
match input.folds[0] {
Fold::X(x) => {
for p in paper_points {
if p.x < x {
new_paper_points.insert(p);
} else {
new_paper_points.insert(Point {
x: 2 * x - p.x,
..p
});
}
}
}
Fold::Y(y) => {
for p in paper_points {
if p.y < y {
new_paper_points.insert(p);
} else {
new_paper_points.insert(Point {
y: 2 * y - p.y,
..p
});
}
}
}
}
paper_points = new_paper_points;
println!("{:?}", paper_points);
println!("num points: {}", paper_points.len());
}
fn show_paper(paper_points: &HashSet<Point>) {
for y in 0..=paper_points.iter().map(|p| p.y).max().unwrap() {
for x in 0..=paper_points.iter().map(|p| p.x).max().unwrap() {
if paper_points.contains(&Point { x, y }) {
print!("*");
} else {
print!(" ");
}
}
println!();
}
}
pub fn transparent_origami_2(test_mode: TestMode) {
let input = parse_input(test_mode);
let mut paper_points = input.points.clone();
for f in input.folds {
let mut new_paper_points = HashSet::new();
match f {
Fold::X(x) => {
for p in paper_points {
if p.x < x {
new_paper_points.insert(p);
} else {
new_paper_points.insert(Point {
x: 2 * x - p.x,
..p
});
}
}
}
Fold::Y(y) => {
for p in paper_points {
if p.y < y {
new_paper_points.insert(p);
} else {
new_paper_points.insert(Point {
y: 2 * y - p.y,
..p
});
}
}
}
}
paper_points = new_paper_points;
}
show_paper(&paper_points);
}

View File

@ -0,0 +1,763 @@
1288,822
842,72
693,68
561,254
1007,192
405,794
929,45
58,296
411,317
164,3
894,317
371,306
272,205
646,865
1027,700
920,667
728,665
1240,105
390,227
972,257
561,640
954,128
440,194
1136,712
37,516
1072,93
1210,576
390,486
682,611
343,368
236,840
366,889
238,644
55,130
939,388
619,726
1092,23
1180,458
338,189
176,516
915,210
550,100
773,466
351,554
460,389
266,560
154,810
604,646
969,880
627,793
211,0
842,520
460,647
678,72
1007,702
863,170
371,836
716,233
1260,695
682,84
710,745
852,94
200,756
574,383
112,306
1072,698
1103,460
617,826
276,827
1158,73
959,702
454,327
90,502
790,463
480,742
985,577
1205,334
298,766
371,388
320,754
1004,252
765,437
1290,550
1191,210
1170,296
865,522
262,663
504,714
406,222
174,630
402,609
30,169
164,127
1074,54
663,77
136,353
316,632
164,52
512,718
1038,82
351,453
1027,413
68,600
1110,756
5,18
1160,180
632,822
600,793
798,718
428,625
964,443
904,672
596,122
174,745
366,403
648,252
1121,164
920,606
464,807
749,254
624,501
1282,543
1126,494
554,891
236,54
333,483
1233,459
843,504
928,171
500,278
1168,512
937,504
6,177
830,264
584,105
1287,788
375,250
982,58
788,695
386,324
1273,378
88,129
756,3
218,471
686,501
20,326
326,114
885,750
536,182
209,56
20,568
944,98
428,94
1101,693
644,413
373,410
912,830
1096,833
390,219
0,828
93,684
1038,306
1101,390
1037,493
1290,763
288,403
346,317
1191,140
840,821
962,131
1116,505
688,458
55,460
416,196
405,100
856,474
528,376
929,849
873,381
483,621
1002,317
1004,730
375,644
279,250
378,442
1128,317
1022,768
681,843
164,630
80,606
20,763
112,588
972,189
514,150
1084,766
433,782
373,484
1180,436
894,196
1089,560
1290,26
1192,731
576,672
1185,145
296,228
38,441
870,733
1059,747
1006,856
1255,434
1275,278
718,38
806,714
1304,177
313,14
412,563
877,112
238,698
976,502
1168,624
1139,397
1038,588
1010,308
242,115
679,393
935,838
959,620
62,550
8,29
1230,383
1256,745
1252,632
1184,376
140,296
1002,353
594,774
734,222
1242,70
1146,400
296,725
87,187
786,10
982,562
1128,577
1096,385
542,285
1198,588
433,112
161,82
492,854
209,504
80,624
984,556
325,364
126,868
1128,50
760,346
1287,554
1059,644
54,745
865,372
283,33
288,5
1038,597
944,348
863,252
994,262
788,505
37,378
1174,311
1124,374
990,140
383,764
1037,885
874,812
1120,171
662,754
1119,116
142,718
492,171
326,506
1146,630
1283,793
234,382
990,754
1230,606
1287,106
1116,4
1148,499
296,666
437,381
944,770
209,693
472,437
749,192
296,218
1248,102
1183,502
885,592
8,865
820,2
130,94
774,387
1086,263
119,140
962,252
1236,161
683,793
539,642
147,190
632,374
932,173
7,525
50,621
528,742
806,266
832,516
835,256
537,649
1014,423
15,816
1166,661
1223,747
105,334
408,507
636,465
1138,84
273,9
405,346
1139,716
761,560
559,12
328,836
1238,665
126,119
1310,828
981,418
390,606
348,252
80,383
600,275
502,98
875,130
405,548
756,563
351,441
232,292
510,213
358,808
241,11
1290,131
190,171
1232,672
306,730
340,404
1198,509
840,73
744,765
935,250
612,462
308,17
174,264
271,802
970,404
338,705
229,750
782,518
937,670
574,288
271,259
1148,575
1303,817
47,56
808,613
956,801
28,543
994,705
130,542
119,618
775,460
519,824
1248,744
574,848
1303,824
592,102
303,702
303,340
928,619
219,578
798,644
1037,849
150,180
1305,876
1180,94
908,285
706,646
490,52
214,283
6,588
236,137
584,718
218,306
850,330
981,866
1136,264
818,171
710,275
0,605
873,65
207,434
1255,460
952,808
850,135
1201,737
594,661
718,856
23,788
1184,308
1044,101
416,698
325,577
1165,887
535,140
162,575
1021,602
932,721
118,546
1184,294
634,868
584,294
528,518
596,150
902,507
627,742
810,278
63,266
115,476
985,317
870,161
529,590
214,611
20,131
49,520
1039,259
972,262
26,588
1261,520
375,838
1027,861
343,526
771,642
1146,127
838,577
77,459
907,634
492,275
1195,670
1235,634
126,308
62,38
58,324
390,288
191,116
905,794
478,378
846,737
1163,190
830,546
1230,507
1031,364
674,429
1084,317
1220,392
791,824
592,344
522,505
174,149
464,82
1220,502
276,4
346,353
112,385
266,793
1077,192
1062,448
42,432
528,824
346,577
1084,128
383,802
395,210
1027,33
592,150
1195,700
574,894
648,754
592,856
1267,77
1267,58
340,490
749,702
421,702
863,460
251,147
1238,341
698,462
440,611
249,346
964,281
172,138
664,865
115,726
136,801
155,845
810,362
283,506
412,779
219,477
944,5
226,128
1022,403
636,429
984,562
388,334
70,401
764,759
1074,502
50,695
846,157
972,632
666,413
1146,3
383,316
1149,82
356,128
1180,800
103,44
174,712
273,493
629,51
499,49
1274,742
416,252
683,242
596,296
316,637
882,129
1103,684
226,317
1077,702
1093,65
846,807
817,812
144,233
130,800
724,70
522,340
985,364
1310,156
1207,850
683,652
959,254
1297,462
390,675
856,518
1248,38
574,831
1061,548
447,460
818,40
182,801
676,26
308,613
272,689
1292,541
333,484
694,348
1146,842
792,353
1185,749
492,619
54,149
207,460
736,270
681,51
1222,250
985,353
319,402
316,262
272,306
20,282
406,672
990,541
1027,194
584,124
1260,646
190,801
850,759
1178,505
1091,130
1103,194
325,353
194,505
1223,187
686,277
105,177
308,283
216,763
288,546
1260,173
13,462
523,852
512,644
850,389
806,378
1263,56
764,135
718,150
736,0
773,354
1092,471
440,733
1138,810
874,530
775,684
959,640
818,275
775,434
1155,49
841,80
88,196
546,135
1265,873
782,586
283,413
1081,144
308,353
200,138
929,401
80,224
354,588
550,346
1222,765
522,695
832,378
920,219
937,581
171,397
728,229
830,854
25,51
226,331
1037,9
1176,431
686,871
694,98
928,597
1038,569
939,588
288,768
375,56
827,621
709,401
1021,292
340,42
281,448
1148,52
744,250
422,548
437,9
1014,725
662,140
1002,283
928,588
736,471
792,541
70,124
726,572
300,84
157,746
390,238
1260,273
1088,520
447,252
1078,346
234,512
577,883
1002,765
288,348
207,724
249,11
185,528
771,224
464,535
714,122
731,260
691,726
616,403
694,403
1168,718
279,530
894,698
686,23
771,726
1280,169
892,392
413,435
781,590
1175,742
774,182
313,880
828,138
88,250
539,224
582,105
724,518
551,268
490,891
23,106
991,402
229,144
566,250
1285,51
1068,779
182,577
472,885
770,840
1007,340
80,288
644,848
574,176
251,644
1014,218
1272,43
248,448
468,520
810,616
25,724
348,642
194,389
604,695
189,164
216,131
1116,564
674,465
125,749
907,147
10,413
937,410
550,822
fold along x=655
fold along y=447
fold along x=327
fold along y=223
fold along x=163
fold along y=111
fold along x=81
fold along y=55
fold along x=40
fold along y=27
fold along y=13
fold along y=6

View File

@ -0,0 +1,21 @@
6,10
0,14
9,10
0,3
10,4
4,11
6,0
6,12
4,1
0,13
10,12
3,4
3,0
8,4
1,10
2,14
8,10
9,0
fold along y=7
fold along x=5

View File

@ -0,0 +1,136 @@
use std::{
collections::{HashMap, HashSet},
io::{BufRead, BufReader},
ops::AddAssign,
};
use crate::types::TestMode;
#[derive(Debug)]
struct Input {
template: HashMap<String, u64>,
rules: HashMap<String, Vec<String>>,
template_terminal_chars: HashSet<char>,
}
fn split_template(s: &str) -> HashMap<String, u64> {
let x: Vec<_> = s
.as_bytes()
.windows(2)
.map(|w| String::from_utf8(w.to_vec()).unwrap())
.collect();
let mut hm: HashMap<String, u64> = HashMap::new();
for e in x {
hm.entry(e).or_default().add_assign(1);
}
hm
}
fn parse_input(test_mode: TestMode) -> Input {
let f = test_mode.input_file(file!());
let mut br = BufReader::new(f);
let mut template = String::new();
br.read_line(&mut template).unwrap();
template.truncate(template.len() - 1);
let template_terminal_chars = template
.char_indices()
.filter_map(|(i, c)| {
if i == 0 || i == template.len() - 1 {
Some(c)
} else {
None
}
})
.collect();
let template = split_template(template.trim_end());
let mut buf = String::new();
br.read_line(&mut buf).unwrap();
let mut rules = HashMap::new();
loop {
buf.clear();
br.read_line(&mut buf).unwrap();
if buf.is_empty() {
break;
}
let mut s = buf.trim_end().split_terminator(" -> ");
let pattern = s.next().unwrap().to_owned();
let pattern_chars: Vec<_> = pattern.chars().collect();
let new_letter = s.next().unwrap().to_owned();
let mut pattern_chars_it = pattern_chars.iter();
rules.insert(
pattern,
vec![
format!("{}{}", pattern_chars_it.next().unwrap(), new_letter),
format!("{}{}", new_letter, pattern_chars_it.next().unwrap()),
],
);
}
Input {
template,
template_terminal_chars,
rules,
}
}
fn run_for_steps(input: &Input, steps: usize) {
let mut template = input.template.clone();
for _ in 0..steps {
let mut new_template = HashMap::<String, u64>::new();
for (t, num_t) in template {
if input.rules.contains_key(&t) {
for i in input.rules[&t].iter() {
new_template
.entry(i.to_owned())
.or_default()
.add_assign(num_t);
}
} else {
new_template.entry(t).or_default().add_assign(num_t);
}
}
template = new_template;
}
// println!("{:?}", template);
let mut occurances = HashMap::<char, u64>::new();
for (s, i) in template {
for c in s.chars() {
occurances.entry(c).or_default().add_assign(i);
}
}
for (s, i) in occurances.iter_mut() {
if input.template_terminal_chars.contains(s) {
*i = *i / 2 + 1;
} else {
*i /= 2;
}
}
println!("{:?}", occurances);
let min = occurances.values().min().unwrap();
let max = occurances.values().max().unwrap();
println!("{:?}", max - min);
}
pub fn extended_polymerisation_1(test_mode: TestMode) {
let input = parse_input(test_mode);
run_for_steps(&input, 10);
}
pub fn extended_polymerisation_2(test_mode: TestMode) {
let input = parse_input(test_mode);
run_for_steps(&input, 40);
}

View File

@ -0,0 +1,102 @@
SVCHKVFKCSHVFNBKKPOC
NC -> H
PK -> V
SO -> C
PH -> F
FP -> N
PN -> B
NP -> V
NK -> S
FV -> P
SB -> S
VN -> F
SC -> H
OB -> F
ON -> O
HN -> V
HC -> F
SN -> K
CB -> H
OP -> K
HP -> H
KS -> S
BC -> S
VB -> V
FC -> B
BH -> C
HH -> O
KH -> S
VF -> F
PF -> P
VV -> F
PP -> V
BO -> H
BF -> B
PS -> K
FO -> O
KF -> O
FN -> H
CK -> B
VP -> V
HK -> F
OV -> P
CS -> V
FF -> P
OH -> N
VS -> H
VO -> O
CP -> O
KC -> V
KV -> P
BK -> B
VK -> S
NF -> V
OO -> V
FH -> H
CN -> O
SP -> B
KN -> V
OF -> H
NV -> H
FK -> B
PV -> N
NB -> B
KK -> P
VH -> P
CC -> B
HV -> V
OC -> H
PO -> V
NO -> O
BP -> C
NH -> H
BN -> O
BV -> S
CV -> B
HS -> O
NN -> S
NS -> P
KB -> F
CO -> H
HO -> P
PB -> B
BS -> P
SH -> H
FS -> V
SF -> O
OK -> F
KP -> S
BB -> C
PC -> B
OS -> C
SV -> N
SK -> K
KO -> C
SS -> V
CF -> C
HB -> K
VC -> B
CH -> P
HF -> K
FB -> V

View File

@ -0,0 +1,18 @@
NNCB
CH -> B
HH -> N
CB -> H
NH -> C
HB -> C
HC -> B
HN -> C
NN -> C
BH -> H
NC -> B
NB -> B
BN -> B
BB -> N
BC -> B
CC -> N
CN -> C

157
src/t15_chiton/mod.rs Normal file
View File

@ -0,0 +1,157 @@
use std::{
collections::{BinaryHeap, HashMap},
io::{BufReader, Read},
};
use crate::types::TestMode;
#[derive(Debug)]
struct Input {
risk_level_map: Vec<Vec<u32>>,
}
fn _print_table(t: &[Vec<u32>], separator: &str) {
for row in t {
println!(
"{}",
row.iter()
.map(|u| u.to_string())
.collect::<Vec<_>>()
.join(separator)
);
}
println!();
}
fn parse_input(test_mode: TestMode) -> Input {
let f = test_mode.input_file(file!());
let mut buf = String::new();
BufReader::new(f).read_to_string(&mut buf).unwrap();
let mut risk_level_map = Vec::new();
for line in buf.split_terminator('\n') {
let line: Vec<_> = line.chars().map(|c| c.to_digit(10).unwrap()).collect();
risk_level_map.push(line);
}
Input { risk_level_map }
}
fn get_neigbours(index: (usize, usize), n: usize) -> Vec<(usize, usize)> {
let mut res = Vec::new();
if index.0 > 0 {
res.push((index.0 - 1, index.1));
}
if index.1 > 0 {
res.push((index.0, index.1 - 1));
}
if index.0 < n - 1 {
res.push((index.0 + 1, index.1));
}
if index.1 < n - 1 {
res.push((index.0, index.1 + 1));
}
res
}
#[derive(Eq, PartialEq)]
struct State {
node: (usize, usize),
cost: u32,
}
impl Ord for State {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
other
.cost
.cmp(&self.cost)
.then_with(|| self.node.cmp(&other.node))
}
}
impl PartialOrd for State {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
fn find_min(table: &[Vec<u32>]) -> Option<u32> {
let n = table.len();
let start_node = (0, 0);
let end_node = (n - 1, n - 1);
let mut best_paths = HashMap::new();
best_paths.insert(start_node, 0);
let mut frontline = BinaryHeap::from([State {
node: start_node,
cost: 0,
}]);
while let Some(State { node, cost }) = frontline.pop() {
if node == end_node {
return Some(cost);
}
if cost > *best_paths.entry(node).or_insert(u32::MAX) {
continue;
}
let neigbours = get_neigbours(node, n);
for neighbour in neigbours {
let neigbour_cost = table[neighbour.0][neighbour.1];
let best_neigbour_cost = best_paths.entry(neighbour).or_insert(u32::MAX);
if cost + neigbour_cost < *best_neigbour_cost {
*best_neigbour_cost = cost + neigbour_cost;
frontline.push(State {
node: neighbour,
cost: *best_neigbour_cost,
});
}
}
}
None
}
pub fn chiton_1(test_mode: TestMode) {
let input = parse_input(test_mode);
let min = find_min(&input.risk_level_map).unwrap();
println!("{}", min);
}
fn new_val(v: &u32) -> u32 {
if *v == 9 {
1
} else {
*v + 1
}
}
pub fn chiton_2(test_mode: TestMode) {
let input = parse_input(test_mode);
let n = input.risk_level_map.len();
const FACTOR: usize = 5;
let mut new_risk_level_map = input.risk_level_map;
for i in 0..FACTOR - 1 {
for line in new_risk_level_map.iter_mut() {
line.extend(line.clone()[n * i..n * (i + 1)].iter().map(new_val));
}
}
for i in 0..FACTOR - 1 {
for line in new_risk_level_map.clone().iter().skip(n * i).take(n) {
new_risk_level_map.push(line.iter().map(new_val).collect());
}
}
// 2973 - too high
// 2966 - just right!
let min = find_min(&new_risk_level_map).unwrap();
println!("{}", min);
}

100
src/t15_chiton/prod.in Normal file
View File

@ -0,0 +1,100 @@
8795561346959611984851795223993199426691978637498615942935297722897942579728189182383276311836891971
9971917929239749392313919138788785878436257678478978964999643688839227886859465518439985489795943919
1519761672617778785983978488277814115999116451991978869729122618211379791865816152742849499151214682
9897768578325969757223986999919271436414939289747483386572979869519481591978913852845739134918128698
6131844837515911671618346728935389713865862689991891968992859599212148719512981655915843896819419699
9994158898369771516892943629594768792167999156447986693115896589979189597554487929195418898275991939
7899714915991971118529915219739988517292138842395144993792669384948991998684649619885511938319926529
8439945422238997549198746429761826973391669898967884389843379199899887495381477519258959688562189998
8193158996336869219329798167599696253894399636289548599616776971684358719751199457412129189416339337
8915467845679571319719627819399444276933882997919488941218392246725699575931358682988728811692994888
2183821795618759299397119999912447724899136432675238876256982162673596561194541917989927328994884775
8789678487971129849969177111293971322992799168799694179116768117969482793885681672825369745192292798
9899995799751915683316369897193479341895969161197819529318324971956766824967977979682349271859692499
9738389836681141541749679428151189548958914789921712997989991986792969749119449656969797779397987971
9699386193977846925881916878943588579248899536993991794576926484969985986958761239832944749381396877
2938918897233812797889785112782764111371659727455889948919937998893948984527919431599729431858423312
6669951996546696289498264116385727972556973497368864657695549198987126379129178799142291929595944531
9593327798991199813978669827818164893823995153184287992392877622665469666148268197736278655385853893
4489883181443655445696668196922477872145911769238794459937988771294974551878994626812499591419882438
9828537889259279861788699459666699933159822299169969994311839953287658281994334947114298299449399739
4188562547592911131922397722799988999499427811169546472448222946122399994149695698195849512899141156
7812927129675599119818993368441873851158999359195519823173938847593874317319938262871877982994151854
8925469217741929237517277969589678521292549411978981218821255391918872181337782152599971935434586641
5539173242188516261195962935992419699921211771998999829366384756736985679791323524894919899791751927
7997814295618599112279785547892749118824587414992484558129913948987899996139582741731588699495997181
9294699723167493823939899799875297999693938976152179786642196948529943458129635959926986719188788299
3382299747968914256996638119179579489495894917369311729612961928153965889817885929465121747699993923
1269933289896689643116489371719798989991587811658981468139317814827819327181648686519922623159872716
8749822282959139712191898935929168514293911199984129154669719688839883778199899299699619241169648787
7789461829886475855754227981617959439666958496998884492729328586718954493775988189711859281328961118
1996981999456693988591117919251641821968898256445988799178396189162182741171111899482722939388958511
3281911384597899419698914798194911889589839273747496947197364795912194969393957795992989291837591999
1293812999287868589289214241179594649119899992343411877615638772289591199319196215849889992628429262
2598787636835395941715619539222694792812975975481748539877868612571891699419489899939987379366597698
4781462274896758851786199964998495994884799928116913548959881394813979886818979751785863571721749154
9781899957313649191946278938943668952725533114949839896192161387789132989919592489457498991499145634
8939291595887684897787947956711653882547657238349261375815578499482296127299179887193666585829749989
3987392781148476892679669911899929328939559696918985892627927769683956664885969479599936511452997261
9718491979119959846848498131157967812971882896718922983832319776899132516188992612996481749999992686
8919943758399972755642585691411913377667687791139364126793319649518996979999699899691164918399189872
7377178689183729718471814618846971671488612597556998591379995391219797917687997492569812892162829899
6198691883598199239981463965166178891395862717797769949534348818992699479916324441967852879113914389
9216989165552946188295851854481689328373995258899376993895872229212951841639956946995929695927819249
9188886992578419341444721993392494111118832893996694812192621891797989996585963719519899668496979761
6178959769221537294292836517919989929381873925111177297994731171813548771847917883712814928381315828
3716898729898898116791923615175892681249798369195979923194893399955242992199314988739418974466288117
6712671172426933931999899491979987495165963185198717561866882185885895989659986798139793799819433118
4584379959151684983916286557263499959997582313992956794499595279918993992689611978329772677199695197
9983899196819771699641681959945138993831935591154986184953899351941365399295712466533478897952142168
6899162163198818566972762787289989382494938538296469493317537999999195467999754271988141381266789918
1452932263797497952199118888137861961329529437698719177689997249821121712191719777639699188944321936
8937299999674997371979425811822714647182423856472824694292841198359827339725542547754913932519964957
9994946989869977763295691999117176543478371299189888681177844256187727685388616713239797198891771592
7554973739824921451596999899131383498318934996922147599937751455999839159799296692986193923888918716
2183293736212193119141897255991593533998965599699986684333511369999247828115626787252781948997399355
7166988753117411929413382997948886498187158558318973167282939868978289154979461968949393388198748635
3848991198627879899197562857896159183619819718874617129918483191133991967698128553471899459892397929
2765314119181599176729133659328796299792767869687928921291778482422185719921369149813699944966311469
8482691159398923761627882926982881117188945644896835748384958683499176311775975826629528934976436159
8629967953659486936456699817244387935152911955491258868396712994893218779896713818697822473611899472
9869569395962994217619893792989865797229926372388199383181753799972387947281751917911412193198648899
2911975492385168179852911515718689961977743548821737615588468287984829778119138931993495899999411139
6627692176997581928496111176845898128798398982139541832937981149719315979978979925639977479916288297
7971686592726999387593121969616452677783738499693549294716146947144911287158388446849188523198921159
9654356698192284395828218447993789572441791288511865165275957319726133989295828997899981479293199367
1983295918471185535848787124187154967288585591968899954121721297447389146121365419765845984999697995
3772299933945255981887183847913843946135928498137753611472573189883189388898147899899311559517594526
6935119987289199372691298387496561978547581187375589699139538917889149981989973389296691993898137585
9596981933155656783964173864653743482911961278323228972783896794973182687777999797879485992688899864
7946745867994687182819593938168758438993987764997671339742591814996711459998829197381873761169833199
7998761391174687853825798296855419379326297812911135911535769961499179699318147216113263387981953698
6975995197822218189738184719494471188758894169771883498878198461499469547885896968499963693355941716
4351931919415521116137351999816179752593389213165382519362992963377712912373918797312697527115653474
4219916669692263574989959697799511818886799247369637226524896979114739798437327795849691299932928316
9469815173589995599191717511929851241512129287419528975628498911949738259719499364539555799899987749
8479915281919179814239178416812398824865776973192848267616874181477979169919153488172116912595685919
1289893698599996944651792299632392688252343961243995892218884283891981753441377799862931899681885987
1918997431886381819198395396582984757593892248817191118278199825665792383989396744979898488291819787
1593953997936192775781629434971795219615284969133998265488117988899576939192195943941191263898454787
4116191989544268918925399499117319147593767434896144251686491796711286886419319219768919724488339918
5724615913863188944231161142188759223298646797932191661684846867251942817233499629528599748994238819
9568989891141898878889593392781881437292997772591628197913688948429735171975377969524966319878852198
1451975884978888899851513598956919574939335964999412895192972398975389988737989199973987816587729912
8266921296948786193491779253862131431671987882857169537999157578838774176914698161799177147198972998
9899977419231149419726862744398113392793194994238576864681993162199811196897926857981885899436634991
8863975882112417779869387818526982917699448259848195932976179866278536198163191794669219691194232985
8964676952961795683996858297181963591827634248715774196944191928959643811872914981968142899983398969
3819129921898559367111958191971712538989297786949383519349717524859963945439921799892281989822321974
6843888714292337419966941942419999999577112282934993189218898131672285193999999299488947947756888796
4897177883982914929221319627191749689347559979399184189239223629815698729511514457552661229971845791
7951497555381399499653191719458486151898656381679791193539839579318194299745115665134795986878886958
8999796933696665971996778728127922964493478822898217169185834914999629245188781933911948789292989969
9196969381136611468918217972388437845173999195912928197355989571899481559772197996243198878913819631
7166988339662186997128362499643382919978799844892892946796715324518121699785894695193874729916993919
9298281551968695911458882111884212373479298692144188529258731314391839459899983882393129697999711159
8558438898992511996283529341897299969324918789817899999965999939196128992393994111199692651355275955
7689899924699293736392725716697734919419784152918395812482316549583957919989329617954687594936783898
1169796382849566792128293597457371455827987519511199778291917942178294599929539841167862969924979581
9496929588378869969158122779414228929724985988984299699786358995268989949878981121159539739996566173
9791763949837198338991393152592978229948887552199237768112426143281161433828316999916998589965921948

10
src/t15_chiton/test.in Normal file
View File

@ -0,0 +1,10 @@
1163751742
1381373672
2136511328
3694931569
7463417111
1319128137
1359912421
3125421639
1293138521
2311944581

View File

@ -0,0 +1,279 @@
use bitvec::prelude::*;
use serde::Serialize;
use std::{
fmt::Debug,
io::{BufReader, Read},
};
use crate::types::TestMode;
#[derive(Debug)]
struct Input {
lines: Vec<String>,
}
fn parse_input(test_mode: TestMode) -> Input {
let f = test_mode.input_file(file!());
let mut buf = String::new();
BufReader::new(f).read_to_string(&mut buf).unwrap();
let lines = buf.lines().map(|l| l.to_owned()).collect();
Input { lines }
}
fn decode_to_binary(s: &str) -> BitVec<Msb0, u8> {
let d: Vec<u8> = s
.chars()
.collect::<Vec<_>>()
.chunks(2)
.map(|c| u8::from_str_radix(&String::from_iter(c.iter()), 16).unwrap())
.collect();
d.view_bits::<Msb0>().to_owned()
}
const TYPE_ID_LITERAL: u8 = 4;
const LENGTH_TYPE_TOTAL_LEN: bool = false;
const LENGTH_TYPE_NUM_PACKETS: bool = true;
#[derive(Clone, Debug, Serialize)]
struct Packet {
#[serde(skip)]
version: u8,
packet_type: PacketType<Self>,
}
#[derive(Clone, Debug, Serialize)]
enum PacketType<P> {
L(u64),
Op(OpType, Vec<P>),
}
#[derive(Clone, Debug, Serialize)]
enum OpType {
Sum,
Product,
Min,
Max,
GreaterThan,
LessThan,
EqualTo,
}
impl From<u8> for OpType {
fn from(id: u8) -> Self {
match id {
0 => Self::Sum,
1 => Self::Product,
2 => Self::Min,
3 => Self::Max,
5 => Self::GreaterThan,
6 => Self::LessThan,
7 => Self::EqualTo,
_ => panic!(),
}
}
}
impl Packet {
fn version_sum(&self) -> u64 {
let s = if let PacketType::Op(_, subpackets) = &self.packet_type {
subpackets.iter().map(|p| p.version_sum()).sum()
} else {
0
};
let v: u64 = self.version.into();
s + v
}
fn new_operator(type_id: u8, version: u8, subpackets: Vec<Packet>) -> Self {
let packet_type = PacketType::Op(OpType::from(type_id), subpackets);
Self {
version,
packet_type,
}
}
fn new_literal(version: u8, value: u64) -> Self {
Self {
version,
packet_type: PacketType::L(value),
}
}
fn calculate(&self) -> u64 {
match &self.packet_type {
PacketType::L(value) => *value,
PacketType::Op(operator_type, subpackets) => {
let subs: Vec<_> = subpackets.iter().map(|p| p.calculate()).collect();
match operator_type {
OpType::Sum => subs.iter().sum(),
OpType::Product => subs.iter().product(),
OpType::Min => *subs.iter().min().unwrap(),
OpType::Max => *subs.iter().max().unwrap(),
OpType::GreaterThan => {
if subs[0] > subs[1] {
1
} else {
0
}
}
OpType::LessThan => {
if subs[0] < subs[1] {
1
} else {
0
}
}
OpType::EqualTo => {
if subs[0] == subs[1] {
1
} else {
0
}
}
}
}
}
}
fn _get_expr(&self) -> String {
match &self.packet_type {
PacketType::L(value) => format!("{}", value),
PacketType::Op(operator_type, subpackets) => {
let subs = subpackets
.iter()
.map(|s| s._get_expr())
.collect::<Vec<_>>()
.join(", ");
match operator_type {
OpType::Sum => format!("sum({})", subs),
OpType::Product => format!("product({})", subs),
OpType::Min => format!("min({})", subs),
OpType::Max => format!("max({})", subs),
OpType::GreaterThan => format!("gt({})", subs),
OpType::LessThan => format!("lt({})", subs),
OpType::EqualTo => format!("eq({})", subs),
}
}
}
}
}
fn parse_packet(
packet: &BitSlice<Msb0, u8>,
inner_len: &mut usize,
max_num_packets: Option<usize>,
) -> Vec<Packet> {
// println!("parse_packet: {:?} (max packets: {:?})", packet, max_num_packets);
let mut packets = Vec::new();
let mut i = 0;
loop {
if i >= packet.len() - 1 || packets.len() >= max_num_packets.unwrap_or(usize::MAX) {
break;
}
let version = packet[i..i + 3].load_be();
let type_id = packet[i + 3..i + 6].load_be();
// skip header
i += 6;
match type_id {
TYPE_ID_LITERAL => {
let mut value: u64 = 0;
loop {
let m = &packet[i..i + 5];
i += 5;
value = (value << 4) | m[1..].load_be::<u64>();
if !m[0] {
break;
}
}
packets.push(Packet::new_literal(version, value));
}
_ => {
// operator
let length_type_id = packet[i];
i += 1;
let subpackets = match length_type_id {
LENGTH_TYPE_TOTAL_LEN => {
let len: usize = packet[i..i + 15].load_be::<u16>().into();
i += 15;
let mut _unused = 0;
let subpackets = parse_packet(&packet[i..i + len], &mut _unused, None);
assert_eq!(_unused, len);
i += len;
subpackets
}
LENGTH_TYPE_NUM_PACKETS => {
let num_packets: usize = packet[i..i + 11].load_be();
i += 11;
let mut inner_len = 0;
let subpackets =
parse_packet(&packet[i..], &mut inner_len, Some(num_packets));
i += inner_len;
subpackets
}
};
packets.push(Packet::new_operator(type_id, version, subpackets));
}
}
}
*inner_len = i;
packets
}
pub fn packet_decoder_1(test_mode: TestMode) {
let input = parse_input(test_mode);
for line in input.lines {
let v = decode_to_binary(&line);
println!("{:?}", line);
// println!("{:?}", v);
let mut x = 0;
let p = parse_packet(&v, &mut x, Some(1));
let p = p.first().unwrap();
println!("{:?}", p);
println!("{}", p.version_sum());
println!();
}
}
pub fn packet_decoder_2(test_mode: TestMode) {
let input = parse_input(test_mode);
for line in input.lines {
let v = decode_to_binary(&line);
println!("{:?}", line);
let mut x = 0;
let p = parse_packet(&v, &mut x, Some(1));
let p = p.first().unwrap();
// println!("{:?}", p);
println!("{}", serde_yaml::to_string(p).unwrap());
println!("{}", p.calculate());
// println!("{}", p.get_expr());
println!();
}
// 327007 - too low
}

View File

@ -0,0 +1 @@
805311100469800804A3E488ACC0B10055D8009548874F65665AD42F60073E7338E7E5C538D820114AEA1A19927797976F8F43CD7354D66747B3005B401397C6CBA2FCEEE7AACDECC017938B3F802E000854488F70FC401F8BD09E199005B3600BCBFEEE12FFBB84FC8466B515E92B79B1003C797AEBAF53917E99FF2E953D0D284359CA0CB80193D12B3005B4017968D77EB224B46BBF591E7BEBD2FA00100622B4ED64773D0CF7816600B68020000874718E715C0010D8AF1E61CC946FB99FC2C20098275EBC0109FA14CAEDC20EB8033389531AAB14C72162492DE33AE0118012C05EEB801C0054F880102007A01192C040E100ED20035DA8018402BE20099A0020CB801AE0049801E800DD10021E4002DC7D30046C0160004323E42C8EA200DC5A87D06250C50015097FB2CFC93A101006F532EB600849634912799EF7BF609270D0802B59876F004246941091A5040402C9BD4DF654967BFDE4A6432769CED4EC3C4F04C000A895B8E98013246A6016CB3CCC94C9144A03CFAB9002033E7B24A24016DD802933AFAE48EAA3335A632013BC401D8850863A8803D1C61447A00042E3647B83F313674009E6533E158C3351F94C9902803D35C869865D564690103004E74CB001F39BEFFAAD37DFF558C012D005A5A9E851D25F76DD88A5F4BC600ACB6E1322B004E5FE1F2FF0E3005EC017969EB7AE4D1A53D07B918C0B1802F088B2C810326215CCBB6BC140C0149EE87780233E0D298C33B008C52763C9C94BF8DC886504E1ECD4E75C7E4EA00284180371362C44320043E2EC258F24008747785D10C001039F80644F201217401500043A2244B8D200085C3F8690BA78F08018394079A7A996D200806647A49E249C675C0802609D66B004658BA7F1562500366279CCBEB2600ACCA6D802C00085C658BD1DC401A8EB136100

View File

@ -0,0 +1,10 @@
C200B40A82
04005AC33890
880086C3E88112
CE00C43D881120
D8005AC2A8F0
F600BC2D8F
9C005AC2F8F0
9C0141080250320F1802104A08
EE00D40C823060
38006F45291200

View File

@ -0,0 +1,4 @@
8A004A801A8002F478
620080001611562C8802118E34
C0015000016115A2E0802F182340
A0016C880162017C3686B18A3D4780

View File

@ -0,0 +1,8 @@
C200B40A82
04005AC33890
880086C3E88112
CE00C43D881120
D8005AC2A8F0
F600BC2D8F
9C005AC2F8F0
9C0141080250320F1802104A08

116
src/t17_trick_shot/mod.rs Normal file
View File

@ -0,0 +1,116 @@
use std::io::{BufRead, BufReader};
use regex::Regex;
use crate::types::TestMode;
#[derive(Debug)]
struct Input {
min_x: i32,
max_x: i32,
min_y: i32,
max_y: i32,
}
fn parse_input(test_mode: TestMode) -> Input {
let f = test_mode.input_file(file!());
let mut buf = String::new();
BufReader::new(f).read_line(&mut buf).unwrap();
let r = Regex::new(r"target area: x=(-?\d+)\.\.(-?\d+), y=(-?\d+)\.\.(-?\d+)").unwrap();
let matches = r.captures(&buf).unwrap();
let min_x = matches.get(1).unwrap().as_str().parse().unwrap();
let max_x = matches.get(2).unwrap().as_str().parse().unwrap();
let min_y = matches.get(3).unwrap().as_str().parse().unwrap();
let max_y = matches.get(4).unwrap().as_str().parse().unwrap();
Input {
min_x,
max_x,
min_y,
max_y,
}
}
fn find_max_y(v_x: i32, v_y: i32, input: &Input) -> Option<i32> {
let mut x = 0;
let mut y = 0;
let mut v_x = v_x;
let mut v_y = v_y;
let mut max_y = 0;
while x <= input.max_x && y >= input.min_y {
x += v_x;
y += v_y;
if y > max_y {
max_y = y;
}
if x >= input.min_x && x <= input.max_x && y >= input.min_y && y <= input.max_y {
return Some(max_y);
}
match v_x.cmp(&0) {
std::cmp::Ordering::Less => v_x += 1,
std::cmp::Ordering::Equal => {}
std::cmp::Ordering::Greater => v_x -= 1,
}
v_y -= 1;
}
None
}
pub fn trick_shot_1(test_mode: TestMode) {
let input = parse_input(test_mode);
println!("{:?}", input);
let mut min_x = 0;
while min_x * (min_x + 1) < 2 * input.min_x {
min_x += 1;
}
let max_x = input.max_x;
let min_y = input.min_y;
println!("min_x: {}, max_x: {}", min_x, max_x);
let mut global_max_y = 0;
for x in min_x..=max_x {
for y in min_y..150 {
let max_y = find_max_y(x, y, &input).unwrap_or(0);
if max_y > global_max_y {
global_max_y = max_y;
// println!("x: {}, y: {}, max_y: {}", x, y, max_y);
}
}
}
// 2701 - too low
println!("{}", global_max_y);
}
pub fn trick_shot_2(test_mode: TestMode) {
let input = parse_input(test_mode);
println!("{:?}", input);
let mut min_x = 0;
while min_x * (min_x + 1) < 2 * input.min_x {
min_x += 1;
}
let max_x = input.max_x;
let min_y = input.min_y;
// v_y0 = (n^2 - n + 2M) / 2n = n/2 - 1/2 + M/n = (n - 1)/2 + M/n
let max_y = 2 * input.min_y.abs();
println!("min_x: {}, max_x: {}", min_x, max_x);
println!("min_y: {}, max_y: {}", min_y, max_y);
let mut num_options = 0;
for x in min_x..=max_x {
for y in min_y..=max_y {
if find_max_y(x, y, &input).is_some() {
num_options += 1;
}
}
}
println!("{}", num_options);
}

View File

@ -0,0 +1 @@
target area: x=81..129, y=-150..-108

View File

@ -0,0 +1 @@
target area: x=20..30, y=-10..-5

270
src/t18_snailfish/mod.rs Normal file
View File

@ -0,0 +1,270 @@
use std::{
cell::RefCell,
fmt::Display,
io::{BufReader, Read},
ops::{Add, Deref},
rc::Rc,
str::FromStr,
};
use crate::types::TestMode;
#[derive(Clone, Debug)]
enum SnailNum {
Pair(Rc<RefCell<Box<SnailNum>>>, Rc<RefCell<Box<SnailNum>>>),
Number(u8),
}
impl Display for SnailNum {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self {
&Self::Pair(left, right) => f.write_fmt(format_args!(
"[{},{}]",
left.as_ref().borrow(),
right.as_ref().borrow()
)),
&Self::Number(n) => f.write_fmt(format_args!("{}", n)),
}
}
}
impl Add for SnailNum {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
let left = Rc::new(RefCell::new(Box::new(self)));
let right = Rc::new(RefCell::new(Box::new(rhs)));
let n = Self::Pair(left, right);
// println!("after addition: {}", n);
n.resolve_explodes()
}
}
impl FromStr for SnailNum {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut _unused = 0;
Ok(Self::internal_from_str(s, &mut _unused))
}
}
impl SnailNum {
fn internal_from_str(s: &str, consumed_len_out: &mut usize) -> SnailNum {
if s.starts_with('[') {
let mut current_index = 1;
let mut left_len = 0;
let left = Self::internal_from_str(&s[current_index..], &mut left_len);
current_index += left_len + 1;
let mut right_len = 0;
let right = Self::internal_from_str(&s[current_index..], &mut right_len);
current_index += right_len + 1;
*consumed_len_out = current_index;
SnailNum::Pair(
Rc::new(RefCell::new(Box::new(left))),
Rc::new(RefCell::new(Box::new(right))),
)
} else {
let value = u8::from_str(&s[0..1]).unwrap();
*consumed_len_out = 1;
SnailNum::Number(value)
}
}
fn resolve_splits(&self, did_do_splits: &mut bool) -> Self {
let result = self.clone();
let mut q = Vec::new();
if let SnailNum::Pair(left, right) = &result {
q.push(right.clone());
q.push(left.clone());
}
let mut split_pair = None;
let mut split_number = 0;
while let Some(n) = q.pop() {
let pp = n.deref().borrow_mut();
match pp.as_ref() {
SnailNum::Pair(left, right) => {
q.push(right.clone());
q.push(left.clone());
}
SnailNum::Number(number) => {
if number > &9 {
split_pair = Some(n.clone());
split_number = *number;
break;
}
}
}
}
if let Some(p) = &split_pair {
let left = Rc::new(RefCell::new(Box::new(SnailNum::Number(
(split_number - (split_number % 2)) / 2,
))));
let right = Rc::new(RefCell::new(Box::new(SnailNum::Number(
(split_number + (split_number % 2)) / 2,
))));
*p.deref().borrow_mut() = Box::new(SnailNum::Pair(left, right));
// println!("after split: {}", result);
}
*did_do_splits = split_pair.is_some();
result
}
fn resolve_explodes(self) -> Self {
let mut result = self.clone();
let mut q = Vec::new();
if let SnailNum::Pair(left, right) = &result {
q.push((right.clone(), 1));
q.push((left.clone(), 1));
}
let mut numbers = Vec::new();
let mut exploding_node = None;
while let Some(n) = q.pop() {
let pp = n.0.deref().borrow_mut();
match pp.as_ref() {
SnailNum::Pair(left, right) => {
q.push((right.clone(), n.1 + 1));
q.push((left.clone(), n.1 + 1));
if n.1 > 3 && exploding_node.is_none() {
exploding_node = Some(n.0.clone());
}
}
SnailNum::Number(_) => {
numbers.push(n.0.clone());
}
}
}
if let Some(node) = &exploding_node {
if let SnailNum::Pair(nested_left, nested_right) = node.deref().borrow().as_ref() {
if let SnailNum::Number(nested_left_num) = nested_left.deref().borrow().as_ref() {
let nested_left_index = numbers
.iter()
.position(|x| x.as_ptr() == nested_left.as_ptr())
.unwrap();
if nested_left_index > 0 {
let mut x = numbers[nested_left_index - 1].deref().borrow_mut();
match **x {
SnailNum::Number(n) => {
*x = Box::new(SnailNum::Number(n + nested_left_num))
}
_ => panic!(),
}
}
}
if let SnailNum::Number(nested_right_num) = nested_right.deref().borrow().as_ref() {
let nested_right_index = numbers
.iter()
.position(|x| x.as_ptr() == nested_right.as_ptr())
.unwrap();
if nested_right_index < numbers.len() - 1 {
let mut x = numbers[nested_right_index + 1].deref().borrow_mut();
match **x {
SnailNum::Number(n) => {
*x = Box::new(SnailNum::Number(n + nested_right_num))
}
_ => panic!(),
}
}
}
}
*node.deref().borrow_mut() = Box::new(SnailNum::Number(0));
// println!("after explode: {}", &result);
}
if exploding_node.is_some() {
result.resolve_explodes()
} else {
let mut resolved_spits = false;
result = result.resolve_splits(&mut resolved_spits);
if resolved_spits {
result.resolve_explodes()
} else {
result
}
}
}
fn magnitude(&self) -> u64 {
match self {
SnailNum::Pair(left, right) => {
3 * left.deref().borrow().magnitude() + 2 * right.deref().borrow().magnitude()
}
SnailNum::Number(n) => *n as u64,
}
}
}
#[derive(Debug)]
struct Input {
numbers: Vec<SnailNum>,
}
fn parse_input(test_mode: &TestMode) -> Input {
let f = test_mode.input_file(file!());
let mut buf = String::new();
BufReader::new(f).read_to_string(&mut buf).unwrap();
let numbers = buf.lines().map(|line| line.parse().unwrap()).collect();
Input { numbers }
}
pub fn snailfish_1(test_mode: TestMode) {
let input = parse_input(&test_mode);
let mut s = input.numbers[0].clone();
for n in input.numbers[1..].into_iter() {
// println!(" {}", s);
// println!("+ {}", n);
s = s + n.clone();
// println!("= {}\n", s);
}
println!("{}", s.magnitude())
}
pub fn snailfish_2(test_mode: TestMode) {
let mut max = 0;
let input = parse_input(&test_mode);
for i in 0..input.numbers.len() {
for j in 0..input.numbers.len() {
if i != j {
let input = parse_input(&test_mode);
// println!("{}, {}", i, j);
let x = input.numbers[i].clone();
let y = input.numbers[j].clone();
let s = x.clone() + y.clone();
let m = s.magnitude();
// println!("|{} + {}| = {} ({})", &x, &y, m, s);
if m > max {
max = m;
}
}
}
}
println!("{}", max);
}

100
src/t18_snailfish/prod.in Normal file
View File

@ -0,0 +1,100 @@
[[3,[[6,3],[9,6]]],[6,[[0,9],[9,7]]]]
[[[3,9],[[0,8],[7,6]]],[[[7,9],1],[1,3]]]
[8,[[[9,6],[8,4]],4]]
[5,[[1,2],[3,7]]]
[[[[7,7],5],[[3,5],8]],4]
[[[5,[0,7]],3],[[5,[5,3]],[1,[9,4]]]]
[[[[3,5],[7,1]],6],[[[3,6],[5,6]],[[3,2],5]]]
[[[[2,0],[3,0]],[5,7]],[[4,4],[[9,9],[9,3]]]]
[[[[8,0],7],[[7,1],9]],[[3,[8,6]],8]]
[[6,[7,5]],[[6,8],9]]
[[[9,[1,8]],2],[[[4,0],[9,3]],1]]
[[7,[1,[3,8]]],[[4,7],[8,1]]]
[[[5,5],[[4,5],[2,9]]],[[[7,7],0],8]]
[[[[4,7],3],5],[[[4,3],[3,8]],[[6,5],5]]]
[[[[3,8],2],[1,7]],[[[3,1],4],9]]
[[[[2,1],4],[[9,5],[1,4]]],[[3,5],[[9,1],9]]]
[[[6,[1,8]],[0,0]],[9,[0,3]]]
[[[[2,2],[3,3]],[[4,8],4]],[[[6,8],4],5]]
[4,[[[7,8],[3,4]],[[3,2],9]]]
[[[9,0],3],[[[7,1],4],7]]
[[[1,4],8],[[7,5],[[8,0],[0,7]]]]
[9,[[4,6],[[2,9],1]]]
[[[[1,8],8],6],[[[2,0],6],[0,5]]]
[[[5,5],[6,4]],[[3,8],[9,[7,6]]]]
[[0,[8,[1,4]]],2]
[[[[9,5],0],5],[9,[7,5]]]
[[9,[4,8]],[[8,1],[[8,6],[7,1]]]]
[4,[[[9,6],5],9]]
[[[[3,7],6],0],[[7,7],[[2,7],[9,3]]]]
[[[6,[3,7]],[[8,3],2]],[8,[6,[8,5]]]]
[[[5,[2,7]],[[6,7],3]],[5,[[4,4],1]]]
[[1,0],[[2,8],[[0,4],9]]]
[[[1,4],6],[[[9,8],[1,0]],1]]
[[3,4],[[1,[8,4]],8]]
[[[[9,4],[0,7]],[[5,4],[8,2]]],2]
[5,[[[8,7],[3,4]],[2,4]]]
[[[[1,3],[8,6]],[[3,4],6]],[[8,5],[[9,3],[5,7]]]]
[[0,[[0,9],[7,8]]],[3,9]]
[0,[[8,[2,3]],[[3,5],[4,9]]]]
[[[4,3],[[1,9],[1,5]]],[4,[[9,1],1]]]
[[[[3,6],[2,5]],3],[[8,[8,0]],[[6,9],[5,8]]]]
[7,[[3,[3,6]],[[6,9],[2,7]]]]
[[[[8,3],[6,5]],[[3,9],2]],[6,1]]
[[[2,0],[2,3]],8]
[[1,[[8,7],2]],[[[9,4],8],[4,[9,0]]]]
[[[6,7],[[5,2],3]],[[0,5],[[9,4],[2,6]]]]
[[[9,[5,8]],[[9,3],[6,9]]],5]
[[[5,[4,6]],[5,[3,2]]],[2,[9,[5,4]]]]
[8,6]
[[[4,8],[3,1]],[1,[[7,8],[7,5]]]]
[[4,[[8,8],4]],[5,[8,[3,9]]]]
[[[4,[9,0]],[[0,3],5]],[[5,[3,0]],[6,[2,3]]]]
[[[4,0],8],[[[4,0],7],[[9,6],3]]]
[[8,[[7,8],5]],[[[6,2],8],[1,[0,4]]]]
[[1,[[3,4],[0,8]]],[[6,5],3]]
[[5,2],[[8,6],[1,[9,7]]]]
[5,[6,[[1,3],[1,0]]]]
[[0,[[1,9],[5,6]]],[[[6,2],[5,1]],[[1,2],[1,0]]]]
[[[7,1],4],[[[0,3],3],[[4,8],1]]]
[[3,[9,[3,4]]],[1,[[0,0],[1,4]]]]
[1,[7,[1,[3,7]]]]
[[[0,[5,6]],[[7,4],[5,7]]],[[[6,8],[4,6]],9]]
[[[9,8],[7,[1,3]]],3]
[[[4,[0,3]],[[3,0],6]],[[2,[9,2]],1]]
[[[[1,9],[3,3]],[8,1]],5]
[[7,[5,2]],[[4,[0,1]],[3,3]]]
[[[6,6],[0,6]],[[3,[5,9]],[[4,2],[4,3]]]]
[[[7,[5,4]],[7,1]],9]
[[6,[5,2]],[[7,[0,5]],4]]
[[[8,1],[[7,6],[4,1]]],2]
[[[[4,3],[1,4]],[9,6]],[3,[[2,5],3]]]
[[[[9,3],[5,0]],1],[1,[[9,7],9]]]
[[[8,5],[5,9]],[2,[4,[0,0]]]]
[[[[7,9],2],[[8,8],[6,3]]],[7,[0,9]]]
[[[[6,6],[0,2]],[2,[9,0]]],[[0,9],[9,9]]]
[[[9,[1,3]],[6,5]],[[[1,1],8],[9,[7,2]]]]
[[8,[[8,4],6]],[[4,[5,9]],0]]
[[8,[5,[6,7]]],[[[1,9],9],[0,[0,9]]]]
[[9,[9,[7,3]]],[4,[4,7]]]
[[[[9,3],7],5],[[5,[8,5]],[0,[8,0]]]]
[[[5,[9,0]],[[7,4],[5,3]]],[3,[[1,1],[1,8]]]]
[[1,[[1,4],[5,9]]],[[[9,1],[6,5]],[9,[0,7]]]]
[[[[9,4],9],[5,3]],[[[4,2],[2,2]],[[1,0],0]]]
[[[6,[8,6]],9],[8,[[0,1],[9,7]]]]
[[2,0],[5,[[8,3],4]]]
[[[[0,2],0],8],[8,[[2,5],[8,2]]]]
[[[[7,4],8],[9,[7,5]]],[8,[7,[5,3]]]]
[[2,4],[3,[3,8]]]
[[5,4],[[0,[5,8]],[4,3]]]
[6,[[5,[4,7]],9]]
[[[2,[6,8]],[5,5]],[[[3,0],4],[[6,6],[0,1]]]]
[[[1,[4,2]],[[8,0],8]],[8,[[6,1],[0,0]]]]
[[9,[2,[3,3]]],[[2,6],[[5,2],[5,8]]]]
[[9,[4,4]],[[[8,6],1],2]]
[2,[[[0,7],7],[[7,8],5]]]
[[[4,0],[[1,1],[7,6]]],[[6,7],[[7,2],1]]]
[[[[2,5],0],[[9,5],9]],[6,[7,[6,1]]]]
[[[7,8],1],[[[6,2],0],[[9,7],[3,5]]]]
[[[9,1],0],[3,[[6,1],[6,9]]]]
[[[[9,0],0],[4,[7,0]]],[[6,[4,0]],[8,[4,2]]]]

10
src/t18_snailfish/test.in Normal file
View File

@ -0,0 +1,10 @@
[[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]]
[[[5,[2,8]],4],[5,[[9,9],0]]]
[6,[[[6,2],[5,6]],[[7,6],[4,7]]]]
[[[6,[0,7]],[0,9]],[4,[9,[9,0]]]]
[[[7,[6,4]],[3,[1,3]]],[[[5,5],1],9]]
[[6,[[7,3],[3,2]]],[[[3,8],[5,7]],4]]
[[[[5,4],[7,7]],8],[[8,3],8]]
[[9,3],[[9,9],[6,[4,9]]]]
[[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]]
[[[[5,2],5],[8,[3,7]]],[[5,[7,5]],[4,4]]]

36
src/types/mod.rs Normal file
View File

@ -0,0 +1,36 @@
use std::{fs::File, path::PathBuf, str::FromStr};
pub enum TestMode {
Test,
Production,
}
impl TestMode {
fn input_file_name(&self, module_file: &'static str) -> PathBuf {
let in_name = match self {
TestMode::Test => "test.in",
TestMode::Production => "prod.in",
};
let mut p = PathBuf::from(module_file);
p.pop();
p.push(in_name);
p
}
pub fn input_file(&self, module_file: &'static str) -> File {
File::open(self.input_file_name(module_file)).unwrap()
}
}
impl FromStr for TestMode {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"p" => Ok(TestMode::Production),
"t" => Ok(TestMode::Test),
_ => Err("Unknown test mode!"),
}
}
}