add initial data

This commit is contained in:
timeshifter committed 2026-09-04 16:08:03 +02:00
commit c4e841b4bc
43 files changed
+5284

No files matched your search

+3
View File
@@ -0,0 +1,3 @@
**/target
**/debug
**/.idea
+1
View File
@@ -0,0 +1 @@
# Advent of Code 2021
+7
View File
@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aoc01"
version = "0.1.0"
+8
View File
@@ -0,0 +1,8 @@
[package]
name = "aoc01"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+2000
View File
File diff suppressed because it is too large. Load diff
+10
View File
@@ -0,0 +1,10 @@
199
200
208
210
200
207
240
269
260
263
+43
View File
@@ -0,0 +1,43 @@
use std::fs;
fn main() {
let data = read_data("data.txt");
let increase_count = process(&data);
println!("Increasing values: {}", increase_count);
let increase_count = process_windowed(&data);
println!("Increasing values windowed: {}", increase_count);
}
fn read_data(filename: &str) -> String {
fs::read_to_string(filename).expect("Could not read file")
}
fn process(data: &str) -> i32 {
let mut last_value = i32::MAX;
let mut increase_count = 0;
for line in data.lines() {
let value = line.parse::<i32>().unwrap();
if value > last_value {
increase_count += 1
}
last_value = value;
}
increase_count
}
fn process_windowed(data: &str) -> i32 {
let mut last_value = i32::MAX;
let mut increase_count = 0;
for i in 0..1998 {
let mut value = 0;
for j in 0..3 {
value += data.lines().nth(i + j).unwrap().parse::<i32>().unwrap();
}
if value > last_value {
increase_count += 1
}
last_value = value;
}
increase_count
}
+7
View File
@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aoc02"
version = "0.1.0"
+8
View File
@@ -0,0 +1,8 @@
[package]
name = "aoc02"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+1000
View File
@@ -0,0 +1,1000 @@
forward 7
down 1
forward 9
forward 4
forward 7
down 8
forward 9
down 2
forward 5
down 3
forward 5
up 7
forward 3
down 7
down 8
up 2
down 4
down 7
forward 1
forward 9
down 9
forward 7
forward 8
up 9
down 6
down 7
forward 1
forward 5
forward 3
down 3
down 4
forward 1
forward 1
forward 7
down 4
forward 6
forward 1
forward 9
forward 4
forward 6
forward 6
down 5
down 4
down 8
up 9
up 9
down 6
down 6
forward 4
forward 1
down 1
forward 7
down 1
forward 3
forward 7
forward 3
up 8
down 7
down 4
forward 7
forward 1
forward 2
down 1
down 4
up 2
up 8
forward 6
forward 5
down 9
up 8
forward 1
up 9
down 7
forward 1
forward 5
up 1
up 7
down 1
down 5
forward 9
down 5
up 5
forward 8
forward 9
down 8
forward 1
down 1
forward 7
down 8
up 4
forward 9
up 1
down 7
forward 4
up 8
down 5
down 2
down 1
forward 5
forward 3
up 5
forward 6
up 1
up 6
up 2
up 6
up 5
down 6
forward 5
down 4
forward 7
up 5
down 3
forward 9
down 5
down 2
up 2
forward 2
down 1
up 3
down 8
forward 8
down 8
forward 6
forward 4
forward 7
forward 6
up 6
down 4
forward 7
forward 9
forward 2
forward 5
down 4
forward 4
forward 9
forward 2
down 2
forward 8
up 8
forward 1
down 6
forward 5
down 4
down 4
down 7
down 5
forward 7
forward 7
down 9
up 9
forward 4
down 2
forward 6
down 4
forward 8
forward 4
up 5
forward 8
down 2
forward 8
forward 6
forward 9
down 7
forward 1
forward 1
forward 1
down 3
forward 2
down 5
forward 3
forward 8
forward 4
forward 7
forward 8
up 7
down 2
up 7
forward 4
forward 7
down 4
down 7
down 3
down 8
forward 2
down 2
forward 2
down 6
forward 2
down 8
forward 2
forward 5
down 5
forward 4
down 7
up 8
up 7
down 2
down 1
forward 4
down 2
forward 9
down 2
down 2
forward 7
forward 3
forward 6
up 8
up 7
forward 1
forward 3
down 9
down 1
forward 8
down 7
up 8
down 3
up 4
forward 9
forward 6
up 6
up 4
down 2
down 4
forward 7
down 2
down 9
forward 1
forward 9
down 6
forward 1
down 8
forward 9
down 9
down 6
up 4
forward 5
up 9
down 2
forward 7
down 1
down 1
down 7
down 4
down 1
down 6
forward 4
forward 6
up 1
down 8
down 4
forward 1
down 5
down 4
up 1
down 9
forward 1
down 1
up 8
forward 8
forward 8
down 5
down 2
down 3
forward 1
down 1
forward 3
down 7
forward 7
forward 1
forward 3
forward 5
forward 3
down 8
forward 1
up 2
up 3
down 4
down 4
up 4
down 4
up 8
down 8
down 7
down 6
forward 5
down 3
forward 2
up 9
down 7
forward 2
down 1
down 3
down 7
down 1
forward 7
down 2
down 9
down 7
up 1
down 5
forward 7
forward 2
forward 7
forward 3
down 2
down 8
up 3
down 3
forward 3
down 2
up 7
up 1
forward 5
down 6
down 3
forward 6
forward 8
forward 2
down 9
down 1
down 1
down 4
forward 8
down 3
up 3
up 4
forward 3
forward 7
down 3
up 6
down 8
up 4
up 3
forward 5
down 9
down 2
forward 6
forward 4
up 6
up 1
forward 4
forward 7
forward 1
forward 8
forward 3
forward 3
forward 9
forward 4
forward 4
forward 4
forward 2
forward 7
down 8
forward 5
up 1
forward 3
forward 7
down 3
forward 7
up 6
forward 4
up 2
down 7
down 4
up 3
up 7
down 1
forward 5
up 7
up 7
down 7
down 9
down 7
forward 5
forward 7
down 1
down 5
down 6
forward 6
up 7
forward 7
forward 6
down 4
down 3
down 9
up 5
down 9
down 3
forward 8
forward 1
forward 5
down 6
forward 7
down 7
forward 8
down 9
up 1
up 2
forward 2
down 4
up 1
forward 2
forward 9
down 8
forward 3
up 3
up 3
forward 3
down 7
down 5
forward 2
up 3
forward 3
forward 5
up 5
forward 3
down 1
down 3
down 6
down 1
forward 7
forward 6
down 5
down 2
forward 7
down 3
forward 2
forward 2
forward 2
down 1
forward 4
forward 5
down 8
up 8
up 7
down 7
forward 9
down 8
forward 1
forward 6
up 3
forward 8
up 9
down 6
down 9
up 4
down 1
forward 1
up 7
down 4
forward 7
forward 4
down 2
forward 3
forward 4
down 7
forward 5
up 8
down 9
down 3
forward 3
forward 8
down 7
forward 1
up 7
down 7
forward 7
down 9
down 6
down 3
down 1
up 5
up 6
forward 8
down 8
up 1
forward 6
up 2
down 6
down 7
down 5
forward 7
up 3
up 6
down 3
up 9
forward 9
down 2
down 4
up 2
forward 7
down 7
up 4
forward 2
down 4
forward 7
up 9
forward 4
forward 9
down 8
up 2
up 2
forward 2
up 8
forward 1
forward 8
forward 8
down 9
forward 6
up 1
down 3
down 4
down 6
forward 6
up 6
down 2
down 1
down 1
forward 9
forward 2
down 7
down 8
down 6
down 3
forward 1
up 6
up 4
forward 3
down 1
forward 6
down 8
forward 6
forward 5
down 3
down 5
up 2
down 7
forward 8
down 8
up 3
down 4
down 3
down 4
up 9
up 6
forward 5
forward 8
down 8
forward 1
forward 8
down 1
down 3
down 4
down 6
forward 3
down 8
down 9
down 3
forward 3
forward 2
forward 2
down 1
up 7
down 6
up 2
down 9
down 4
down 3
down 5
down 4
forward 4
up 8
forward 1
down 9
down 3
down 3
down 8
down 5
up 6
up 5
down 7
down 3
down 7
forward 1
up 5
down 1
forward 1
down 7
down 9
down 3
forward 1
forward 7
down 8
forward 5
up 5
forward 2
down 6
forward 2
down 2
forward 2
up 7
down 2
down 5
down 2
forward 7
forward 2
forward 4
down 2
down 7
down 6
forward 9
down 8
down 3
down 6
forward 7
down 3
down 4
down 7
down 7
down 1
up 8
up 7
up 6
down 8
up 8
forward 8
down 2
down 4
forward 7
forward 4
down 5
down 4
down 3
forward 6
up 9
down 1
down 3
down 4
down 7
up 3
up 4
forward 1
up 4
down 9
up 4
up 3
down 1
down 2
forward 5
up 4
down 1
down 4
down 8
down 1
up 6
down 6
forward 8
forward 7
down 5
down 6
down 5
down 6
down 9
forward 6
down 6
down 7
forward 3
down 2
down 9
up 2
down 6
forward 2
forward 6
forward 4
down 4
forward 8
forward 2
forward 7
forward 4
down 9
forward 6
down 6
forward 1
down 4
down 5
forward 5
up 5
forward 7
up 2
down 9
forward 9
down 9
forward 9
forward 3
forward 7
forward 5
forward 1
down 5
forward 5
down 3
down 2
up 9
down 7
down 2
forward 3
down 8
down 6
down 7
up 5
up 9
down 6
down 8
down 5
down 8
forward 4
forward 5
down 2
down 1
forward 6
down 6
forward 9
forward 2
forward 1
down 3
forward 8
forward 6
up 7
forward 5
down 4
up 7
down 6
down 3
up 8
forward 5
forward 5
down 1
down 7
forward 2
down 5
forward 3
forward 6
forward 6
up 5
down 4
down 2
forward 7
up 8
forward 5
up 7
forward 6
forward 6
forward 6
up 7
down 1
down 4
down 4
forward 5
down 5
up 7
down 7
forward 8
down 6
down 2
forward 3
down 8
up 3
down 2
forward 7
down 1
forward 3
up 1
up 4
down 4
up 6
down 1
down 2
forward 9
up 8
forward 3
down 2
forward 3
down 7
up 6
forward 9
down 3
down 9
up 5
down 9
down 6
forward 8
forward 5
down 8
forward 5
forward 9
up 1
down 3
up 2
down 1
down 2
down 8
forward 9
forward 9
down 7
down 7
down 5
down 1
down 7
up 1
up 1
forward 8
down 6
up 8
down 7
down 1
forward 1
forward 4
down 9
up 2
down 6
forward 2
down 4
forward 7
up 8
down 3
forward 9
down 7
up 7
down 6
down 9
down 2
up 7
down 3
down 9
forward 7
down 7
up 6
down 5
down 4
up 1
down 1
up 7
up 9
down 1
forward 6
forward 3
down 5
down 1
up 8
forward 9
forward 6
down 1
up 3
down 2
forward 3
up 6
down 5
down 3
up 7
down 8
down 5
down 3
forward 6
up 8
down 4
forward 3
forward 5
forward 9
forward 1
up 4
forward 8
forward 9
forward 1
up 9
up 4
forward 4
forward 4
forward 8
up 5
up 2
down 7
down 4
up 5
down 5
forward 6
up 8
down 5
down 7
down 3
up 2
up 2
forward 3
forward 5
forward 1
forward 9
forward 1
down 2
down 2
up 6
up 6
down 5
down 3
down 4
forward 4
down 6
up 6
forward 8
down 9
forward 2
forward 6
down 7
forward 3
down 2
down 5
up 3
forward 6
forward 7
forward 3
forward 4
down 9
up 9
forward 7
down 8
forward 1
down 2
up 5
up 9
forward 8
up 5
forward 7
up 6
forward 9
down 6
forward 4
down 4
forward 7
up 8
down 8
down 7
down 9
up 1
down 5
forward 6
down 9
down 2
down 2
down 6
down 6
forward 8
forward 5
up 7
forward 9
forward 3
forward 8
forward 8
down 7
up 1
down 4
forward 7
up 5
down 9
down 7
forward 8
down 3
up 2
down 7
forward 3
up 8
forward 4
up 2
forward 1
forward 4
forward 5
forward 3
forward 6
down 9
up 7
forward 7
up 6
down 2
down 5
forward 2
forward 8
down 3
forward 6
down 6
forward 4
up 3
forward 3
down 4
up 3
forward 6
forward 7
down 5
forward 5
down 9
forward 7
down 5
down 6
forward 9
+77
View File
@@ -0,0 +1,77 @@
trait Move {
fn single_command(&mut self, direction: Direction, amount: i32);
fn multiple_commands(&mut self, sequence_of_movements: Vec<(Direction, i32)>);
fn result(&mut self) -> i32;
}
#[derive(Debug)]
struct Submarine{
horizontal: i32,
depth: i32,
aim: i32,
}
impl Move for Submarine {
fn single_command(&mut self, direction: Direction, amount: i32) {
println!("{:?} {}", direction, amount);
if direction == Direction::Down {
self.aim += amount;
} else if direction == Direction::Up {
self.aim -= amount;
} else if direction == Direction::Forward {
self.horizontal += amount;
self.depth += self.aim * amount;
}
}
fn multiple_commands(&mut self, sequence_of_movements: Vec<(Direction, i32)>) {
for (direction, amount) in sequence_of_movements {
println!("{:#?}", self);
self.single_command(direction, amount);
}
}
fn result(&mut self) -> i32 {
return self.horizontal * self.depth
}
}
#[derive(Debug, PartialEq)]
enum Direction{
Down,
Up,
Forward,
Unknown,
}
fn main() {
let sequence_of_movements = read_sequence_of_movements("data.txt");
let mut submarine = Submarine{ horizontal: 0, depth: 0, aim: 0};
submarine.multiple_commands(sequence_of_movements);
println!("result: {}", submarine.result());
}
fn read_sequence_of_movements(filename: &str) -> Vec<(Direction, i32)> {
let data = std::fs::read_to_string(filename).unwrap();
let mut sequence_of_movements: Vec<(Direction, i32)> = Vec::new();
for line in data.lines() {
let direction = line.split(" ")
.nth(0)
.unwrap();
let direction_actual = match direction {
"down" => Direction::Down,
"up" => Direction::Up,
"forward" => Direction::Forward,
_ => Direction::Unknown,
};
let amount = line.split(" ")
.nth(1)
.unwrap()
.parse::<i32>()
.unwrap();
sequence_of_movements.push((direction_actual, amount))
}
return sequence_of_movements
}
+6
View File
@@ -0,0 +1,6 @@
forward 5
down 5
forward 8
up 3
down 8
forward 2
+7
View File
@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aoc03"
version = "0.1.0"
+8
View File
@@ -0,0 +1,8 @@
[package]
name = "aoc03"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+1000
View File
@@ -0,0 +1,1000 @@
101011111001
100100110111
101010101011
010100010000
001101101101
110001101010
001001011001
101111101111
110001001000
111111000000
111100110001
101100010100
000010000011
000110000011
101010100101
000111001010
100101101110
111111010101
110010001100
000011001101
001001111110
100101001100
000011100100
000100110011
000101101000
101010011011
101011110000
010010110011
110110101110
001110000110
001110111101
110111100101
000010101010
101010101010
011011001101
110110000111
011010011000
000111000101
100000101011
010010101100
110000101110
000010001001
101010001100
101111110101
101111011110
110011100001
011011100100
111100011010
110110101001
101011100010
111000110110
111011100101
111000001111
101110110011
111101000000
101111100110
011111101101
100111110111
000111011011
001001001111
100010110000
001101010000
100101101001
100001010000
110101110001
100111111111
101101000101
001110100110
101010110000
101111010010
101011100101
001110010100
011110110100
100011000000
001000000100
010000010001
101101111011
111010001011
101010100000
101001000000
001000010101
010011000111
101010111100
111101000101
111111100010
100010011101
101001111101
010111001111
111111110110
100111101010
010101000101
100101010010
010110010001
011101000111
011110011010
100000111100
011111111000
001111010001
110110100111
111000001011
110010010011
101010110010
110000110111
101111010000
110111010000
110011100011
010000100111
001010110001
011101011010
110111010010
011001001101
101101001111
011101110100
001101101110
011011101101
111001010001
001100101000
010011100000
011000010100
111000111111
001001100001
010011001010
001110101100
010010111010
000111111011
010010000110
110001100100
001110110111
000010001111
100011100001
100001101110
100001110111
010110100000
100101101111
001100000111
101100111001
100100001100
100000101100
010101000110
101110001000
101110000110
101110011100
001001010001
110111101011
101001000001
111100111110
001100010100
010001111111
110011000011
100110011101
011010100100
011111000011
000110001000
010010111111
010011111011
001000111110
000111101011
100010111100
010110001111
011011000000
010111000011
010001011111
111000101100
010110110000
001100111011
011110100101
100011100011
001000101000
101011001011
101111001111
111000100101
111101100111
111010101000
001110000111
010101011110
001111010100
101011011101
000111101010
000011001011
111000010001
011001011011
011011011001
011110111000
000010001100
011110010000
010000101001
101011011111
100111001100
101000011100
111000000100
011100110000
011100101010
010001110011
100100100010
011110010010
011000100111
110101000101
111011000001
011000010111
010011110011
011001000001
000100101110
100111110001
010010011010
111101011001
100110101111
100010001100
010010000111
100011100100
111001111011
110000010111
101001010001
011111001010
110111111100
000101111101
101011000111
000100010000
111100001111
100000110010
110001110100
000001001100
000101000001
100111101011
011000110110
101111000000
010100101100
101111100011
100111001101
100000010010
100101001111
011110100010
110110100110
100000100001
110100001110
010011001111
011010010000
000000010101
111111011100
000011100000
011110111101
110000011100
011011011000
010100111010
010010100101
001110100011
000110000100
100111000101
000010001000
110110110000
010011000100
011001100010
110000011110
000110100111
010111111011
011100000011
100011101110
001100011010
000010100100
000011110000
000000101000
101001101001
101111010101
101100101101
110000000000
111011001110
001100101110
100101111000
010001000111
111100111010
000100011101
010001000011
100000101001
111111101101
000000011101
001101110110
001100000101
010110011110
001011100100
110001100101
001011110011
011101111000
001001100110
000001011000
001011010000
110001010100
000100011011
010101101010
101000101010
101111011000
110001010111
111100110011
101011101000
000100010110
111010000110
000100000001
101011110101
010010010011
100101011001
100101001010
110100110101
010011100111
100110010101
111010000011
011011110110
111000101110
001011101100
010110100010
011001101011
011000101101
011011111110
011011111001
100001001101
101110011011
001111011100
111110010011
000010101011
001111101010
100001101000
111100100000
010011100100
011101101100
000001101011
001100001110
000011000111
100110001000
101101101011
001011011110
101010111101
101110100110
011111000100
111101111000
001011110111
010010011001
111110110001
100101000100
011011010101
100010010000
100010101000
110010000100
000001001111
101110011010
010000111111
111101010001
100000000000
011101100110
001110101101
001001110010
110011101001
101101011001
000000110101
011101100011
010100000011
101011110010
100101000010
100110001100
000111111010
111100011001
111110101001
010000111000
100110000011
100011001110
000100010010
001101100100
000000001100
011101001001
100001001000
101100111000
110011011111
111110101110
101101001010
111000011001
111110011001
110111010100
011001111011
101101111000
010010010100
100000110110
011110010100
010010101010
001111100100
100000011101
110100010010
101101100000
111001111010
010001100110
011000110101
011110010011
011111100000
001000101100
000100010111
011000101010
011000010110
010001101011
110101010000
000100100100
011011001100
010100011101
100111110110
111010000001
001001111111
010100001111
011111101011
011000011011
111010000100
110001001111
111011011010
001110001110
100100101001
101101101001
001100001001
001000001110
101111000010
000110001110
101111110010
110011001011
000000010100
110100110001
111000011111
100000001110
100100011011
101100100011
011101000010
001100100101
111110101101
011100001001
101100000110
101011010011
000101101100
001011101001
110000000101
011111011110
000001100110
100110011011
111010100100
011001101001
010111110011
110111000000
011100101011
010110100111
110011001001
000010111011
101110010110
000001011001
100101000111
110110111011
000101110001
100000111101
011110001011
000101010101
101011110011
000110011001
101010000001
100001011110
010000000111
000011100110
100111100010
110101000111
001010101110
111000100000
001100101101
000111010111
101110000000
011111000010
111000001110
001100110101
101001000100
010111011101
000100000100
111111000111
111011001111
101000011110
110011101101
111101000011
101101111001
010010010101
110000100010
000100100111
000110111111
000011001000
011011100001
101100111101
011111000001
000110101100
010010111001
111100101110
100111010100
011111000000
001011001000
101111000011
000100001010
110100010000
110001000011
010010101111
001010001010
010001110001
011000001101
101001010111
000000011100
010011111000
110010011011
001110100111
011101000011
111110011101
100111000000
101011101110
111000010111
101001000011
011110011001
000100010001
101110000100
111101101101
000100100110
010011010001
000101100110
010111110001
001110111100
100000111111
101100000011
110011000100
100000001011
010101100000
101010000101
010110101101
010011100001
001101001111
101100100111
100000001101
100100101100
101111010111
010101001100
011001111111
010111110000
010100110101
101111010001
111101111100
011010011011
111100110100
111110001010
011110011111
000110001100
011001010111
000010010111
011100101111
111110111000
110010110010
000101111010
001010010100
100110110001
101101010001
100001111011
010101001001
000001000010
100101100011
010100101010
001001001100
101000110110
111110001000
100101110010
001000111011
100111010111
000000110000
110101111000
011000101110
110101001111
101000010010
101011000010
100110000111
110101011001
110010010100
010000001111
110011011001
111010110100
010000101010
101100111011
000000011010
110110001000
001011011011
101110100001
010100011011
111101001101
010000011001
110010100100
101101011010
110000100001
010101111000
101000111010
011011010100
011110011101
010000100100
001111101001
010011110000
111000100111
010101010101
000001001010
010001001100
111011111100
001110000011
110101101000
111010011110
110011010011
001010101101
001111110001
110001011110
011110010001
010010000100
111010111001
000100111111
000001010101
001010110111
000110010011
000010011111
011110100001
011111010110
001110010110
001001000011
101111011101
011100110111
010000001101
010010100111
010000110011
011110110111
010000110110
011101011011
000010100001
001100011111
011110110000
111011011011
101011101111
010001000101
010100001000
010100101110
001000000101
001001011011
111001000101
110101110100
111111010111
101010100111
110000011010
000100100000
011101000001
000011000001
010101010001
010010011100
010001110101
010110110111
010011110010
011011100110
000110100011
100010100011
101111110011
001011100101
011101110000
110001110000
111010010000
111111000011
010010001100
010111101010
010010101000
111001010111
010110110011
001101110010
011111001111
010100110100
010010100100
101011101100
000111101001
000001011010
101010111000
100011111111
100100011111
111000010010
011011100010
000111010010
101010101111
010000100101
100110110101
011011100011
011011110011
111011000100
100010001010
110000010000
100101011111
101100001100
111110101111
100111000100
011101001110
100011011101
111101011010
110101111001
001000110101
111110101100
001010010111
100110000000
110101010100
110110001101
011110101101
100110101110
000000000010
011010111001
000001101111
000100000101
110000100011
001010000101
100111011010
000001101001
001011000001
000111100110
111111110011
000101000000
111000110000
000010000111
000101010001
000011110001
101100110100
100001111110
001111111111
100011000110
011100011001
101011110100
001010001111
100010100001
001111100110
010110110010
110010110101
011000110011
000101011110
101000100100
010110010110
001000000110
110000100100
011101101011
100100000111
000111010101
110111000100
001010110000
101001110001
001001101011
101000100101
111111000010
000010110101
110000000110
000100110000
111111011011
101101100101
011001010010
111111101111
000111101101
100110110011
110000001000
111000001101
110011010010
110110111110
000001011111
001100001010
111101011100
010010100010
110100011011
110001101011
010111111010
011111100100
001010010110
001010111111
111110111110
011010000110
111111111001
011011011110
001010011101
001110010111
001001111100
110010111000
000111000100
001100001000
110110110100
101001010000
000101000101
010101101100
101010110110
111101011000
101010011100
110010110011
111110001110
100100101110
001010000000
001000110010
101000111111
111110111101
000110010110
101100100110
100010011001
100110100010
111101100110
111100111111
010111011011
111100011111
001010010011
111101010010
110111001010
010110000000
011111011001
101011100011
011100110011
111100011110
101001001001
100101100110
000100110010
010011101110
000011110111
000000011111
110011011100
111110100100
101110101111
001101011000
101101110001
101011000100
111001101100
110111111001
011010101001
000000000000
111100000101
110010111001
000100001110
110101111100
110010011111
001001110111
111111111111
000110011100
110101111010
011010111011
111000000101
011110100111
001010101111
001011011001
101010001011
100001000000
111110111010
001110111110
000000110100
111110111001
111000010101
110011110111
110111001101
111011010010
110100000110
100111111100
010001100010
111100010000
100111011101
101001000111
111110110011
010101010100
101000110111
000111101100
100001011001
010000011111
000100101011
011011011010
001111000010
111001101011
100011010111
110111100111
100001010110
100001110101
100110110111
011111111011
111111101110
111111001000
101010000110
110010010110
100110100111
011101011100
011010011101
101111101101
110111010110
110000000100
110000110100
001100000110
001011001101
001010011111
011001110000
100111100100
001111000000
110100011001
110000110011
000000101101
111101111111
111111010010
011101101110
111101101100
100000110000
110101010001
101001110100
010010110110
111011000111
101100000100
010011010101
111101000001
101001111000
101110000011
001011011101
111010000010
001001110100
111111001001
001001010000
001110000001
111100000000
010101110100
000101011011
001100011001
001110001100
001110100010
000000100111
000100111000
110100101100
110100111010
111001101101
111100101011
011101100000
100000100011
010111110010
010011001000
101001100110
100110001111
000110110101
111001110110
000101010110
011000011010
010100110110
110100111111
111000110011
001011111000
001000000000
111111010100
111111100110
000000001111
110001000001
010000101000
101111111010
100100001101
111011110100
110101110011
010111111110
001000101111
100110101100
110010010010
010010001000
011111101100
111100100011
110111101000
100000111001
001011100011
001010111100
000100001100
010111000110
001111000101
001110000010
110100101000
001110000101
101000110000
001001011101
101001011001
011110100011
101010010110
101000001101
110111111111
010010001101
011100010110
000100100001
001111111101
001001111000
001100101111
111010111100
110100010111
000000010011
100110110000
101100101111
100011010100
001001101110
110001101000
110101101110
010011110101
010000110001
100011111000
111110000010
100111100000
100001100110
110111000110
000101100011
010110001100
100000100010
001011000000
110101000010
101110101001
011010100110
111111110000
001010110010
010000001011
100000010011
010111010111
101111100010
010100010011
+104
View File
@@ -0,0 +1,104 @@
use std::convert::TryInto;
trait Matrix {
fn column_sums(&self) -> Vec<i32>;
fn gamma_epsilon_rate(&self) -> (i32, i32);
fn power_consumption(&self) -> i32;
}
struct Data {
data: Vec<Vec<i32>>,
rows: i32,
columns: i32,
}
impl Matrix for Data {
fn column_sums(&self) -> Vec<i32> {
let mut result: Vec<i32> = Vec::new();
for c in 0..self.columns {
let mut column_result = 0;
for r in 0..self.rows {
column_result += self.data
.iter()
.nth(r as usize)
.unwrap()
.iter()
.nth(c
.try_into()
.unwrap()
)
.unwrap();
}
result.push(column_result);
}
result
}
fn gamma_epsilon_rate(&self) -> (i32, i32) {
let mut result: Vec<i32> = Vec::new();
for value in self.column_sums() {
if value > self.rows / 2 {
result.push(1)
} else {
result.push(0)
}
}
return (bin2dec(complement(&result)), bin2dec(result));
}
fn power_consumption(&self) -> i32 {
let (gamma, epsilon) = self.gamma_epsilon_rate();
gamma * epsilon
}
}
fn bin2dec(bin: Vec<i32>) -> i32 {
let mut result = 0;
let length = bin.len() as u32;
for i in 0..length {
result += 2_i32.pow(length-i-1) * bin.
iter()
.nth(i.try_into().unwrap())
.unwrap();
}
return result
}
fn complement(input: &Vec<i32>) -> Vec<i32> {
let mut result: Vec<i32> = Vec::new();
for value in input {
if value.to_owned() == 0 {
result.push(1);
} else {
result.push(0)
}
}
result
}
fn read_data(filename: &str) -> Data {
let content = std::fs::read_to_string(filename).unwrap();
let mut data: Vec<Vec<i32>> = Vec::new();
for line in content.lines() {
let mut row: Vec<i32> = Vec::new();
for char in line.chars() {
row.push(char as i32 - 0x30);
}
data.push(row);
}
let rows = data.len() as i32;
let columns = data.iter().nth(0).unwrap().len() as i32;
return Data{data, rows, columns};
}
fn main() {
let data = read_data("data.txt");
println!("{:?}", data.gamma_epsilon_rate());
println!("{:?}", data.power_consumption());
}
+7
View File
@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aoc06"
version = "0.1.0"
+8
View File
@@ -0,0 +1,8 @@
[package]
name = "aoc06"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+1
View File
@@ -0,0 +1 @@
1,3,4,1,1,1,1,1,1,1,1,2,2,1,4,2,4,1,1,1,1,1,5,4,1,1,2,1,1,1,1,4,1,1,1,4,4,1,1,1,1,1,1,1,2,4,1,3,1,1,2,1,2,1,1,4,1,1,1,4,3,1,3,1,5,1,1,3,4,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,5,2,5,5,3,2,1,5,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,5,1,1,1,1,5,1,1,1,1,1,4,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,3,1,2,4,1,5,5,1,1,5,3,4,4,4,1,1,1,2,1,1,1,1,1,1,2,1,1,1,1,1,1,5,3,1,4,1,1,2,2,1,2,2,5,1,1,1,2,1,1,1,1,3,4,5,1,2,1,1,1,1,1,5,2,1,1,1,1,1,1,5,1,1,1,1,1,1,1,5,1,4,1,5,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,5,4,5,1,1,1,1,1,1,1,5,1,1,3,1,1,1,3,1,4,2,1,5,1,3,5,5,2,1,3,1,1,1,1,1,3,1,3,1,1,2,4,3,1,4,2,2,1,1,1,1,1,1,1,5,2,1,1,1,2
+58
View File
@@ -0,0 +1,58 @@
const SIZE: usize = 10;
const REPRODUCTIVE_CYCLE: usize = 6;
const SPAWN_GENERATION: usize = 8;
const OVERFLOW: usize = 9;
struct Population {
data: [i128; SIZE]
}
impl Population {
fn get_new_spawn(&self) -> i128 {
self.data[OVERFLOW]
}
fn reset_new_spawn(&mut self) {
self.data[OVERFLOW] = 0
}
fn iterate(&mut self, generations: i32) {
for _i in 0..generations {
self.data.rotate_left(1);
self.data[REPRODUCTIVE_CYCLE] += self.get_new_spawn();
self.data[SPAWN_GENERATION] = self.get_new_spawn();
self.reset_new_spawn();
}
}
fn calc_sum(&self) -> i128 {
let mut result = 0;
for item in self.data {
result += item;
}
result
}
}
fn main() {
let mut data = load_data("data.txt");
data.iterate(256);
let sum = data.calc_sum();
println!("lanternfish: {}", sum);
}
fn load_data(filename: &str) -> Population {
let content = std::fs::read_to_string(filename).unwrap();
let mut data: [i128; SIZE] = [0; SIZE];
for elements in content.split(",") {
println!("{:?}", elements);
let value = elements.parse::<i32>();
let x = match value {
Ok(x) => x as usize,
Err(_e) => break
};
data[x] += 1;
}
Population{data}
}
+1
View File
@@ -0,0 +1 @@
3,4,3,1,2
+7
View File
@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aoc07"
version = "0.1.0"
+8
View File
@@ -0,0 +1,8 @@
[package]
name = "aoc07"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+36
View File
@@ -0,0 +1,36 @@
fn main() {
let data = vec![16,1,2,0,4,2,7,1,2,14];
let (fuel, position) = optimise(&data);
println!("fuel: {}, position: {}", fuel, position);
}
fn fuel_for_pos(data: &Vec<i32>, position: i32) -> i32 {
let mut result = 0;
for item in data {
result += (item - position).abs();
}
return result
}
fn get_min(vec: &Vec<i32>) -> i32 {
return vec.iter().min().unwrap().to_owned();
}
fn get_max(vec: &Vec<i32>) -> i32 {
return vec.iter().max().unwrap().to_owned();
}
fn optimise(data: &Vec<i32>) -> (i32, i32) {
let mut positions: Vec<i32> = Vec::new();
let mut fuel: Vec<i32> = Vec::new();
for i in get_min(&data)..get_max(&data) {
positions.push(i);
fuel.push(fuel_for_pos(&data, i));
}
let min = get_min(&fuel);
let index = fuel.iter().position(|&val| val == min).unwrap();
return (min, index as i32)
}
+7
View File
@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aoc09"
version = "0.1.0"
+8
View File
@@ -0,0 +1,8 @@
[package]
name = "aoc09"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+5
View File
@@ -0,0 +1,5 @@
2199943210
3987894921
9856789892
8767896789
9899965678
+100
View File
@@ -0,0 +1,100 @@
2101234567895421012347899878510986566799987654567986456995439976789764347996567899989943245679988767
3212349678989533234556998965329877345689976563459874348789598865678953256789456798878894556798765657
4323458989678944345678987895498767234567896432398763212678987674679954103598967987658789987899854545
5434767895587895456889456976987654395978954321239654343459976543458893212367899876545678998999973234
6545878954376789569994345897898765989899765210198785454598865432346789323457998765434789109498765123
7976989654245679678965456789999879878789874334239876565987654321257895444567899876523489912349871014
8998999762156789789876667896899998767678998485346989879876543210456989555678976987634567893497932123
9349678954367899897987788945679999654567897576457899999989655421245678967899895498547679989986544254
3234569765478934956799899235989986543456789986568998789998765434657889878976789999967899878987654345
2124758976589320245698932149898997632367893497978989678999887565767899989765679899878987654598776576
3012347897695434346987894298787998321299912999899876567899998676778998795634598787989876573649887989
4124456789789545459876796987676799420189909898789765456789639897989459654523987656699985432233998992
4245678999899957867965789876545678921467898769678932367896510998992398993212396543549876321012349891
5678989656989899979854345997634567892379999654567891246789439889901987889323987992023986535123556789
6789994349878789998765234598745679943489998323478910345899998767899876778934799889934597943234589897
9899989499765676899632123987658789874799986214599321234567897656789965567895699778899679874445678976
1999878989974565696543034798769899965678974323678932356679976545678954357679986565798989965567899765
0998967976543234987654245679978969878789765434567893458989989324679843234568975434667899876678987654
9876756899432123998768368789989345989898978545679954589998998939798756457789964323456901998789998653
9965645678921019899989479897591299999987989757889765679877887698999887568899877634789212349899987654
8654235677932298789996589976410987999876599968999899898766976587899998679976976545695443959998999765
9843123456893989678987898765329876898763459878945910999954383456789898789897987656789659898787899877
9732012587899878576798939876498985989652345999936929998843212345998769896789699787898798767656789998
7643223456798765435899319987987699876543478999899898987652101236789652945679569898909899854345698999
7654654577899871012978998798998789997667569998778767898767432345678941036789323979212998768767897899
8765766689978989153567897649459898998778878987654546989889543766789632123499214567993459979878956798
9878878794569993254568989432398987899899989876543235678987679879896543334568923456789567989989548987
3989989893498754385678978940987856799923498965432123989198789989987656545678994567899678991098659896
2399899932198765476899567891976545689212567896551029893239899998698769756989389878998999542989798785
1298789431019896589912377899875434978909678989762998789347999879549978897895478989987889659878987674
0987678993234987692101256798754323569998989878979897678956989965432989998976568999896578998967896543
9876543789945698789322345679654212398987898769998756567969978986544997899987679999789489997856987654
5987674599896789896543456898732101987876797657987643456798767897959876791098989987654349876545999765
4398765689789893987664687999654219876565789543498732345689458999899865689989899876543234997657898977
5499876895567965998778789998765698765454698955569821234592345698789654578978789989432145698769987899
6985987953456899879899898999876798654323457896898930123591234987699943589965679998743456789898986789
9876798912347998765912997898989987743212348997987651234989959877578892397894998999656567899987775679
0997989543458999894323986967999876532103456789999875445978898766456789456953877898767678999876544589
1299878994569789989499875456892997944212347897899986599865799954345678967892366789998989988987323699
2986566789689679978989764345891299894333458956789797987954689876756789898901345998999393976543212789
9875445999794598867878973226789989789546789545395679876543499989767898769219459897893212987654304589
8764329898943987654569543015678975689987998733234598765432359999898998654398998776794343499875423678
9865498787892198733678952123459954578998999710123499876745678931999999965987687675989954569765434567
9876997676789989421789965234599869699129987621234589987956799210198989878996576534679895679878545689
1999876545678976562597894345789998989012398542349678998767989321987878989965432323456789989997657894
0989987434579987673456789456899987678994987643498989989899878999876567899875321012348995699989769943
9876543212389998984567896567929876567989998754987899976998765689765459901987632123467894329878998952
6987654343578919987678987678909765458678999869876789765459976789979878929876543248978965499768987893
5398765454689101298799998989996543234567892978975678975234989892989989439998656767899996987659436789
4239898765678914459899999899987652123478931999854567894125999943498999997899767978999889996543125697
4146999876789323567999898798998783235789949876743569993235799857567899896989978999998778987621013456
3256789998895437678998765656989854345897898765412398989349979768978998765679899123987656798842134567
4345892349976545789899654345678965656976999989324987878998768979989997654568788939876545987653257878
5457943456997656896798775457789876767899895495439876769987657899995986543234567894998756998764567899
6798954599789767965679897668999987898998789989598765459896545989894398732125679943239867899998778989
7899769987678978976789998789789998909989677679999894398765435679789496521014678994956978910199899678
8978998458569989989899869895698999319876586567899989209876546789678987433123489589897989329987934569
9567987353478999999998756934597998998765432456789979912989656892569876554439593476799998998996549978
3459876212347898989765542129986767899965321245999867893498778921456987675678902345678967987897997899
2999965423456987679654321098795456799876442456898979994589989210378998988789213456989456976799876989
9889876834579876567999532197664345678998593567987696987678994321289549099895324567894349865798765678
6778998748698795457888943498543235689999989679996545698789976562458932134965435898999298954987654599
5465987657798654323567894999860134999899878989988434569999897673567893245976546789998987893498785699
4344598788987643212456989876543239898768768999876523459998789994678954369899677899987656932349896789
2123459899999876301345678998654398767654356789985434578999656789789765498788998999876547899456987899
3234569998998765212346799029795459654321237898899565989986545678993987989657989998932123468967898989
4349678987999654357856893234987598765434548997698977899995434578932799878945678987659994567899989878
5458789456898765678977954345698679876576656789567898909876523457891698767236789898798789698945678969
6569996568999879789798995458798789987989789895457899212987612356943497652179998799987689789996989654
9878997979899989897629789567899890198999996942368994323998723457899998741067896678976578999989899543
5989998989789992987545678998976931239789965321236789439876534569998765432359954599987429678976798954
4597999896599891297656789679999842345679876433345696559987676678999876543878963989987634567895657895
3456898765456789398767896543987653458789989874576789698798787889898988764989879879876545898943246789
2367999854346789999878987632198785669894498765689898987659898998787899895699998765997656789532135892
1567998765235899898989876544349997978912349876996967898749999879656689987789889954398767898643234943
0346898754346798767899987655498899899103567987895456789898789765434578998996779893239878987655345694
1234789765457987656789999776987768789214678998954323899987678976655689999545656789123989699867458789
2345679876567898778999976989886547678925889329765834789986567897896999987432345679954694569878569899
4456989989678929989129965498765636569996789409876745678976458999989899876541234578895893456989678978
5768993599899219991098799597654325458987993212988876799986569998978798765410123456789932597898789567
6978987678923998932998678998765212346798954323499989895697678987665659898321239967897643789999899434
7989698799019897899897567979854303467899766534567897914598989976544446996543998798987654679989978921
8996549997998786789765434567963214679998987645698976723459391985433234987659887689999767789876567892
9989239876892675778976323678977634567897898756989895434569210986321013498798756579899898899983456943
9879959865781534667895434589876547679946789869876799547678999875432125799987644467799999999832169659
6567898774210123456789645699987658789434578998965978956789987986543234589876532345678989398743298998
5456987654321434597998756789198767896545689987654568987895456599654565679874321567889879197654997987
5323498767434545989899767899239878987956789898543459998954343498765696789965452378998968989769896596
4212999998545669878799879998945989599897898765432368989768212349876789891986565459987957679898789498
4345899987656989765678998987896792498789999974321245678965401234989899932987876567896543599987678989
5786799998767998954789897676989899989678898765210176989876212345698999743498987878965432789876564879
6997898989878987323456789565678998878545789896325237891995454597897898654569898989998543679876433768
7898967878999976212345689434989987567438699965434346990987566989986799765699789995987654567984321057
8989655567898765303458796579898765432123578986557656789799679879865689897987678954398786788985432348
9876544466789873214567899698798765321012459987868867897698799767654799989876567943219899899876543456
8765433345678984323678998987659876432133569998978879933569987654423689876987678955323999976987656567
9654321234589995644567897898243998873244567899989989212456798743314578965699899876456789765398797678
8765430355679876789678976543012397654356798935697694302345989832105689654556998987987897573239898789
9974321366789987899899987632123498767459899224599543213459876543216798532348967898998965432145999890
9765432377890198975910199743435679878567892101987656323567998765323897621012345999249986551026789921
+131
View File
@@ -0,0 +1,131 @@
use std::{collections::HashSet, fs};
const NEIGHBOUR_IDXS: [isize; 3] = [-1, 0, 1];
struct Grid {
data: Vec<Vec<u32>>,
low_points: Vec<(usize, usize)>,
basin_sizes: Vec<usize>,
nx: usize,
ny: usize,
}
fn main() {
let mut grid = Grid::from_file("input.txt");
grid.find_low_points();
println!("{}", grid.get_result_1());
grid.find_basins();
grid.basin_sizes.sort_unstable();
grid.basin_sizes.reverse();
let result = grid
.basin_sizes
.into_iter()
.take(3)
.reduce(|x, y| x * y)
.unwrap();
println!("{result}");
}
impl Grid {
fn from_file(filename: &str) -> Self {
let input = fs::read_to_string(filename).unwrap();
let data: Vec<Vec<_>> = input
.split_whitespace()
.map(|line| line.chars().map(|c| c.to_digit(10).unwrap()).collect())
.collect();
let low_points = vec![];
let basin_sizes = vec![];
let ny = data.len();
let nx = data.get(0).unwrap().len();
Self {
data,
low_points,
basin_sizes,
nx,
ny,
}
}
fn get_neighbours(&self, x: usize, y: usize) -> Vec<(usize, usize)> {
let mut result = vec![];
for dx in NEIGHBOUR_IDXS {
for dy in NEIGHBOUR_IDXS {
if dx.abs() == dy.abs() {
continue;
}
let new_x = x as isize + dx;
let new_y = y as isize + dy;
if 0 <= new_x && new_x < self.nx as isize && 0 <= new_y && new_y < self.ny as isize
{
result.push((new_x as usize, new_y as usize));
}
}
}
result
}
fn get(&self, x: usize, y: usize) -> u32 {
*self.data.get(y).unwrap().get(x).unwrap()
}
fn find_low_points(&mut self) {
for x in 0..self.nx {
for y in 0..self.ny {
if self
.get_neighbours(x, y)
.iter()
.all(|(ox, oy)| self.get(x, y) < self.get(*ox, *oy))
{
self.low_points.push((x, y));
};
}
}
}
fn get_result_1(&self) -> u32 {
self.low_points
.iter()
.map(|lp| self.get(lp.0, lp.1) + 1)
.reduce(|x, y| x + y)
.unwrap()
}
#[allow(clippy::never_loop)]
fn find_basins(&mut self) {
for (x, y) in &self.low_points {
let mut basin = HashSet::new();
basin.insert((*x, *y));
loop {
let old_len = basin.len();
let extend: HashSet<(usize, usize)> = basin
.iter()
.flat_map(|(x, y)| self.get_neighbours(*x, *y))
.filter_map(|(x, y)| {
if basin.contains(&(x, y)) {
None
} else if self.get(x, y) != 9 {
Some((x, y))
} else {
None
}
})
.collect();
for item in extend {
basin.insert(item);
}
if basin.len() == old_len {
break;
}
}
self.basin_sizes.push(basin.len());
}
}
}
+7
View File
@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aoc10"
version = "0.1.0"
+8
View File
@@ -0,0 +1,8 @@
[package]
name = "aoc10"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+10
View File
@@ -0,0 +1,10 @@
[({(<(())[]>[[{[]{<()<>>
[(()[<>])]({[<{<<[]>>(
{([(<{}[<>[]}>{[]{[(<()>
(((({<>}<{<{<>}{[]{[]{}
[[<[([]))<([[{}[[()]]]
[{[{({}]{}}([{[{{{}}([]
{<[[]]>}<{[{[{[]{()[[[]
[<(<(<(<{}))><([]([]()
<{([([[(<>()){}]>(<<{{
<{([{{}}[<[[[<>{}]]]>[]]
+90
View File
@@ -0,0 +1,90 @@
({{[<{[{[[<([[<><>][{}()]]<([]{})[<>()]>)><<<([]())>>{([()<>][{}{}])<[<>()]{()()}>}>]<(<[[[]<>]({}{})]{{{}<>}
[(<<<[[[{({(([{}{}]<()<>>)[<()<>>[[]<>]])}([{{<>()}}]))}<[{{[<[]()>][[<>()][[][]]]}{{(()()){{}{})}}
<(<(<<{<(({(<[()()]<<>[]>>((()[])(<><>)))}))[{<{<[(){}]>}><[<{{}[]}{[][]}>(<{}{}>)]([(<><>)(<>{})]{{(){}}[<>(
[{<[<{[[[<{<(<[]<>><<><>>)<{{}()}>>{[<()>{[]<>}]}}>[(<{[{}()]<<>()>}{(<>[])({}<>)}>)]]}{({<
[<[(<<<([[[<(<<>{}><()>)(<[]<>>([][]))>(<{{}<>}{{}[]}>)]](<(<({}){<>()}>{<{}<>>})[{[{}<>][<
[([(<([[[([([<[]()>[()()]]{{<>{}}([][])})<{<[][]>{()[])}[{<>{}}<<>()>]>][<[<{}<>>[[]{}]](<()
{({{<{[[(<[[{[()()][()[]]}<{<>[]}<{}()>>]({<<>>}(({}{})(<>)))]<([[()[]]<(){}>]{{{}<>}([]<>)})([{[][]}{<>
<(<{{([[<<({[<(){}>[()<>]]{[{}<>]<(){}>}}(<[[]{}]<[][]>>))<<[{{}()}][{<>[]}[{}<>]]>[([<>{}]{[]{}})
[<{<((([{<(<(<{}>[{}()])>)<{[{<>()}[{}[]]][{[]}<{}()}]}((<{}>{[]<>})({()()}<[]{}>))>>}])))
<{(<{((([((({({}<>)([]<>)}({()<>}[{}<>])){(<[][]><()[]>)<[{}[]]<[][]>>})[[{{<>{}}(<><>)}{[()()][()[]]}]])[[{
{<[<[[[(({(<<{[][]}(<>{})>{<[]()><()()>}>(({[]<>}{()<>}){[()()]}))})<({{<(<>{})>[(<>[]){()
([[[{{[<([<<({[]{}}{(){}})(<[]<>><[]<>>)>{{{[]}{[]{}}}}><[[[<><>]{{}[]}]]<<[{}{}][(){}]>{<()[]>[{}[]]}>>]
{[[[<<({([<<(((){})<{}()>)[({}[])<()[]>]><<[[]{}]<<>()>>{(<>())}>](<<{[]{}}[<>()]>>[{({}<>)[()[]]}
[<{{<({<<[(<<<{}<>>{[]{}}><((){})((){})>>(<[[]<>][(){}]>[<{}()>({}<>)]])]<[<{[[]()]({}[])}>(<
<{(({{(({({<[<{}<>><[][]>]<{(){}}>>}[(<[[]{}](()())>[[()()][{}[]]])])<[(<[<><>](()[])><{{}{}
[[(({(({([[<{<{}<>>{(){}}}{(<>[])<[]()>}>][[[<[]()>[()]][<<>{}><{}()>]]([([][])])]]<({[[<>[]][{}<>]]<{
<<[{{{<<[<{<<[<><>]{[]}>>{<([]<>)<<>[]>>[(<>{})(()())]}}{<[{{}<>}(<>[])]>[[{{}{}}<{}<>>](([
((({{[[<[([[<{()<>}({}{})><<<>[]><()<>>>]{{<[]{}>[{}()]}(<[]<>><()[]>)}][{{<[][]>}<[()[]]<[]()>>}])]>]]}([{{[
[([{({<<[{[[{{()()}}[[<>()]([]())]]{{(()<>)[{}()]}{[(){}][<>()]}}]}]>(([{[{[[]{}]{[]<>]}]{{<[]<
<(<(<({{<<([<{[]{}}[{}<>]>{<<><>>{{}{}}}])({(<<><>>((){}))({[]<>}<{}<>>)})>{{[<<()<>><[]()>><((){})<{}<>>>]<
([{<{{[{([{[<[(){}]{(){}}><{[]()}[{}{}]>]({<<>{}>({}[])})}[{[{[]()}<(){}>]({[]{}}{()()})}(
(<[([[(<([([<[[]()]>][<[()<>]([][]}>[(()[])<[]()>]])<{[<[][]>(()[])](([]()))}{[<{}<>>[[]()]]((()<>)
[([((([<<{[({{{}()}(<>())}([()<>]<{}()>))<<(()[])[()<>]>{<()>}>]}{{((({}{})[{}<>])[{[]()}([]{})])<<{<>[]}(
<([{(<<[(<({<<{}{}>{{}[]})<<<>[]><[]<>>>}(<([]<>)<(){}>>[[{}{}]<[]{}>]))>{([<<{}{}>>({[]()})])})](
[(<[((<{[(((([[]]<<>[]>){<{}<>><()()>}))<[[{{}()}<<>()>]]>)]<<(<{(<>{})}{({}<>){{}()}}>([{()<>}{{}()
<{(<<[{<<[({<<{}{}><()[]>>}){[([{}{}]([]{}])(((){})[[]<>])]<<<()<>><<><>>>([{}()]<[][]>)>}]>>(<(<({({}
(([{{<{{(<{[{{<>[]}}({{}[]}<(){}>)]{(([]())[()<>])<[[]][[]()]>}}{<({(){}})<[{}{}]>>[((()()))]}
[(({[<{([{[[{[[]()]<[]{}>}]{{<{}[]>{<>{}}}{(()<>)<()<>>}}][<<<{}()>(()())>>]}(([{{<>[]}[(){}]}[([]<>)<
[<(<{{(<[(<<({()<>}<()()>)([<><>]<(){}>)>>)]>{<<[([[(){})<<>[]>])({<()>(()[])})][[({{}}(<>())){
(((<<[<(([<[<[()()][{}()]>[([]())[[]()]]][<<<><>>{<><>}>{<<>[]>[<>()]}]>}{<{[[()[]]<()()>]{[[][]][[]
{{(<<{(<[<({{<<>()>{{}[]}}([[]{}]<[][]>)}<(({}()){()()})>)>({<[[<><>]{[]}]{(<>())([]{})}>[[<<>[]>({}
<<<[[[<[<{[[<({}())[[]()]><{(){}}{{}}>]][[[[{}[]]<(){}>]]<[{()()}<[]{}]]<(<>())[<><>]>>]}<[[({<>}<{}{}>)<
(<(<[<<{<[({([[]{}]{[]}){[(){}](<>{})}}{[[[]][[]()]]})<[<[<>()]{{}()}>][((()[]){(){}})[[()
<[{{{({(([{[{<{}<>>}<[{}<>]>]}]<([{{(){}}{[][]}}[<<>()><{}<>>]][{<{}[]>{()()}}])[({[{}<>][[][]]}{[{
<[<{[([<<[[<[<()[]>[<>{}]]{([][]){[]<>}}>({{()<>}[()<>]}[{<>()}{<>{}}])]<<{({}())(<>)}[[()()][[]{}]]>>]>><(
{[{<([(<<[[<[[<>{}}{{}{}}][{{}{}}[{}{}]]>][{([[][]](()[]))(([][])(()<>))}{[<<>{}>{[]}]{((){})(<>)}}]]{[<<<
(({<<[<[<(<[<[<>{}]([]{})>(<[]<>><()>)][<[<>()]{()[]}>]>)[[[({{}{}){<><>})[(<>()){{}<>}]]]<<[<()[]>([][])]<<[
<[({<<<<[{<<([{}[]][[]<>])[({}())<()<>>]>{<{{}<>}{[]}>[([]())({}()))}>}]{[<<<(<>())(()<>)>>{[<<><>>({}{})][
<<((<{<{[{<<{<()<>>{<>{}}}>>}{<{{<<>>(()())}<{[]()}[()<>]>}{[<()>{<><>}]}>([<{[]<>}[{}<>]>{[[]<>]<<
([<[<[<<{{{{{(()<>)[<>()]}[({}{})[{}{}]]}(((()()))<{<><>}>)}<[<[<>()]{<><>}>[{<><>}{()}]](
[([[((<({{([[([]{}){{}<>}]([[]()]<<>[]>)][[<()()>{{}}]<((){})[<>()]>])<<{<[][]>}[[[]<>]<{}<>>]>{{({}[])(
<(<<[<{{[([([(<>()}{()()}][{{}{}}[<>]]){<{<><>}<[]<>>>{{<><>}[[]()]}}]<<[([]<>)<()[]>]><[<[][]>]<[{}{}]{{}[]}
[({(({(({{([{[{}()]<()<>>}(([][])({}))])({(<{}()>)}{{([]<>)(<>{})}})}({<[<{}<>>{[][]}]({<>[]}<{
[([[<[[{{<(<{<()<>><<>()]}<<[]{}>([]())>><<[[]<>]{{}<>}>>)><([<({}())>((<>{})[(){}])][<<[]()>(
[[(((<{({<<(<({}[])({}[])>[{[]{}}{<>{}}])[<<[][]>{<><>}>([[]()](<>[]))]>({<<<><>><[]()>>[([][])[[]{}]]})>}<
{[<(<<[{{{{<<{{}{}}{()<>}>(<[]()>({}<>))>}({<{{}()}{()()}>(<[]{}>({}<>))}([[<>()]({}())}((
{[{([{<([({{{[{}<>){{}}}(<<>{}>{<><>})}[<<()()><<>[]>>]})(<<{([]<>)<{}()>}>>)])(<{<((<()[]>{{}{}})){({[]<>}(
<[[<(<{{{{{((<()<>>(<>{}))([{}{}]<<>[]>))<{<[]>(<>{})}>}}[(((([]<>){<>[]}))(({<>()}({}[]))))]}([{[<{[]
{<({[{[{<{{{[[<><>]{{}<>})<<[]()>([])>}[{{<>[]}{()<>}}]}<<{<{}<>>({}<>)}[<{}()>[[]<>]]>[<<<>{}>[(){}
<({({{([<[[[<{<>{}}([])>](<{[]}<[]<>>>)]]>)<[[<([<{}[]>[()()]]<(<>{})<{}[]>>){{({}<>)[[]<>]}<{{}{}}{()<
<{<{({<<{{{(<{[]{}}(()())>({<>[]}(<>[])))<{([][]){{}()}}(<{}[]>)>}{[<<()<>><{}[]>>{{[]{}}[[]()
{[<({<<{[(<{([{}[]]<[]<>>)<<{}[]>[()()]>}<<{()[]}{<>{}}>[[{}()]([]<>)]>>({[([]{})]<<[]{}><[][]>>}))][{[[<{[]
[<<{<({<(<{{[{()()}<[][]>]{{<><>}({}{})}}}<<(<()[]>{[][]})[[{}()]]><[<<>[]>(()<>)][(<>{})([]())]>>>[[<({
{{((([[<[<[[[{()<>}(<><>)](<{}()>({}()))][{<{}()>[{}[]]}{<{}[]>[{}<>]}]]({{[<>[]]<<><>>}([<>]{[]})}([({
<<([[{[{[<(({[{}{}]<()<>}}[(<>{})[(){}]])([[{}{}]]{[()<>]<()<>>})){{[([][])<()[]>]}}>]((<(<({
(<({[{[{(<<<<([]<>)<[]()>><{()[]}{{}()}>>{{[[]()](<><>)}({<><>}{<>{}})}>{([((){})(<>())]{<<><>>({}()}})([[{}
<<<{(([(((({{<()())}<((){})[<>{}]>})(<[{<>{}}<{}()>]([{}[]][{}()])>[{{[]{}}{[]<>}}{{<><>}({}<>)}])
(<{{[{{<([[[[([]()){(){}}][<[]()>{<><>}]]{[([])(<><>)]}]])((<[[({}{})[[]{}]]<({}){()}>][[(
{(((({([<[<<<[()()](<>{})>[{{}[]}<<>{}>]>>([{<{}()>[[]()]}{({}())<{}()>}])]{({<{[]{}}<[][]>><[[]
(<{[[<{{[((((<(){}><{}[]>)<{()()}<{}()>>)<<[(){}]>>}){(({{[]()}}<<<>[]>([]<>)>){{[<>{}]{[]()
<<{{{<<[{<[<[{[]{}}[{}<>]]<([]{})[<>{}]>>({[()()]}(([][])[<>()]>)][{<<<><>>(()<>)>[{<>()}<[]{
[[<[[[<([((({{{}<>}[()[]]}{([]<>)<{}()>})[(<<>()>{<><>})]))<[({(()()){{}<>}}(({}())<[]{}>))({<<>{}>[[]{}]}(
{[[({[(<<{<((<{}()>({}<>))<<{}()><{}[]>>)>(<[<()()>(<>)]{([][])[(){}]}){([{}()]({}()))[[()
(<({[{<{<({{{({}<>)}{[()<>]<{}{}>}}{<<{}{}>([]<>)>(<{}<>>{<>{}})}}{[{<(){}>><[[]()](<>())>
[<{(({[{[<(<({[]{}}[<>])[{()()}(()<>)]>{{[[]{}]}<({}[])[{}{}]>})[{[[()]{<>{}}][<()[]>[[]()]]
(<[{{<(<({{{({[][]}{()()}){([]())<[]{}>}}}{(<<{}()>{{}{}}>[(<>[])])}]{[<[<(){}><{}<>>]<(<>[])(()[])>>[[{()
<<[<{[(<{({(((<><>))[[{}{}}(()())])<{<()[]>}>})<[{<<{}<>>(()())>[<<>[]>[{}()]]}{{<{}()>[<>{}
<({((<([{{<[(<{}[]><<>()>)(({}()){[]{}})]{{<()[]>[()<>]}<{()[]}{[]<>}>}><<{((){})[{}<>]}{(()[]
<{{[[<{<{<{{{[[]<>]}{{{}{}}<[]()>}}[<{()<>}(()())><<{}<>><()[]>>]}<([[<>()]([]{})]{{()}{<>[]}}){<<[]<>>>((
<<({<{{[<{(((<(){}>{[]<>}){[(){}]})<({[]})[<()<>>{{}<>}]>)}><[({{<[][]>}}<{{[][]}<[]{}>}>>([(<{}<>>(()(
{<([{((({<((<<[]{}><{}<>>>[{{}()}[[][]]]))[<{[{}[]][<>[]]}>[<<{}()>{{}<>}>((<><>))]]>}(((<([[][]){[](
[[<{(<(([<[{(<{}[]>[{}<>])<<{}<>><<>()>>]<{[()<>]{{}{}}}{<<><>>[[]{}]}>]<(<{()()}<<>[]>><({
([{<(<<<([[{[[[]()]({}[])]({[]{}}(<>[]))}<{[[]{}]({}())}>]{([([][])[{}<>]]([<>()]<()[]>))}](<({({}[])((){})}
[<[{{[[[[[<({({}<>){{}{}}}{{[]<>}{<>}}){((<>())<[]<>>)[<()>[{}[]]]}>[{[[{}<>]]{{()<>}<()()>}>[[{[]<
(<{{{<([([([[([]())<[]{}>]<<()><[]()>>])])(<{[<(<>())<{}>>]<{[{}<>]<{}[]>}(([]<>)(<>()))>}(({<<>{}>[[]
{[(<[<{<<[{<<(()<>)<(){}>>[[<>[]](<>())]>{{[[]()][<>[]]}({{}[]}<<><>>)}}<<(([]<>)[()()])[[()<>][
(<<[(([<([<<[[()[]]([]<>)]{(<>())([][])}>>{((<{}[]>{<>{}})[<<>()>])}]{([{{()()}<<>()]}{[[][]][(){}]}]
{{<[<(((<(<{{([]())<{}()>}<<<>[]>{{}<>}>}<<<[]{}>>(<[]{}>({}<>))>><<{[(){}][[]()]}{{<>[]}([]
[{{<[[([{[({[({}<>){{}()}]([{}<>]({}()))}(({()<>}<[][]>)({{}{}}{<>[]})))([([()[]][[]{}])]<<([
([<(({<<(<([<([]()){(){}}>[([]{}){()[]})]{{(<>())(<>[])}<[<>[]][{}[]]>})><[[{(()<>){{}()}}{
<((<<[{[[[[{[[<>{}][[]()]]{{[][]}<[][]>}>]]{({{[()[]]<{}[]>}<[<><>]<[]<>>>}<<<()<>>>[{()()}]>)(<<{<>()}>([{}
([(([{([((<{<([]<>)<{}<>>>(<{}{}>)}{{[{}]{<><>}}[[{}<>]([][]}]}>){<<<[{}()][{}{}]>>>[[{<{}{}>[{}[]]}]<[
{[[(<([([{([<[()[]]<(){}>>[(<><>)[()()]]])}<{[[(<>[])([]{}>]{[[]<>]<{}[]>}]}[(((()<>)<()[]>
{{<(({([<<<<{([]()){()[]}}({()()})><(<()[]>[<>[]]}({<>{}}{[][]})>>{({<()[]>[{}<>]}((()[])(()<>)))}>>[(
{([[(({{{[[{<[{}()]<<>()>>[[()<>]{[]()}]}<[<{}()>{{}()}]{[[]<>](<>())}>]{({<(){}>(<>{})}{<[]>[[]()]}){<(
(<{<[(<[[<[{[[(){}][()()]]<[()<>]((){})>}[(<(){}>{<>{}>)]]{({{()}}{[{}<>]<<><>>})(({<>()}<{}()>)[{()}<<
[([([<{<((([[<[][]>{[]()}][<()<>><{}()>]]({[{}()]}[<()()>{[]<>}]))<[(<()()><()[]>){{[]<>}[()()]}]
{([<({[<((({(<{}<>>[{}()])<[<>{}]>})){{<<<[]()><[]<>>>{{<>()}[<>()]}>{[<()[]>[(){}]]})})<({
<<<[[[((<{[{[([])[()<>]]((<>{}}(()()))}<{<[][]><[]()>}{<(){}>{{}<>}}>]<([<<>>]<([][])({}{})>
<[[({[<([{<<<(()[])>{{(){}}<[]<>>)>>[{{{()<>}[{}()]}<{()[]}[()<>]>}<<{()()}([]())>[(<>()){<>{}}]>]}])[((((<{
+146
View File
@@ -0,0 +1,146 @@
use core::panic;
use std::fs;
const CLOSING: [char; 4] = [')', ']', '}', '>'];
type Result = u64;
struct Line<'a> {
input: &'a str,
illegal_char: Option<char>,
opened: Vec<char>,
value: Result,
is_incomplete: Option<bool>,
}
impl<'a> Line<'a> {
fn new(input: &'a str) -> Self {
Self {
input,
illegal_char: None,
opened: vec![],
value: 0,
is_incomplete: None,
}
}
pub fn run(&mut self) {
self.illegal_char = self.find_illegal_character();
self.value += match self.illegal_char {
Some(c) => get_value_for_illegal_character(c),
None => 0,
};
if self.is_incomplete == Some(true) {
self.value = self.evaluate_opened_chars();
}
}
fn evaluate_opened_chars(&mut self) -> Result {
let mut value = 0;
self.opened.reverse();
for character in &self.opened {
value *= 5;
value += get_value_for_unclosed_delimiter(character);
}
for c in &self.opened {
print!("{c}");
}
println!(" {value}");
value
}
fn find_illegal_character(&mut self) -> Option<char> {
let mut chars = self.input.chars();
self.opened.push(chars.next().unwrap());
for character in chars {
if CLOSING.contains(&character) {
if self.opened.last().unwrap() == &get_matching_delimiter(&character) {
self.opened.pop();
} else {
self.is_incomplete = Some(false);
return Some(character);
}
} else {
self.opened.push(character);
}
}
self.is_incomplete = Some(true);
None
}
}
fn main() {
let file = fs::read_to_string("input1.txt").unwrap();
let mut instances = vec![];
for line in file.split_whitespace() {
let mut instance = Line::new(line);
instance.run();
instances.push(instance);
}
let result1 = instances
.iter()
.filter(|item| !item.is_incomplete.unwrap())
.map(|item| item.value)
.reduce(|accum, item| accum + item)
.unwrap();
println!("\n{result1}");
let mut result2: Vec<_> = instances
.iter()
.filter(|item| item.is_incomplete.unwrap())
.map(|item| item.value)
.collect();
result2.sort_unstable();
let index = (result2.len() - 1) / 2;
println!("{}", result2.get(index).unwrap());
}
fn get_matching_delimiter(input: &char) -> char {
if input == &')' {
'('
} else if input == &'(' {
')'
} else if input == &']' {
'['
} else if input == &'[' {
']'
} else if input == &'{' {
'}'
} else if input == &'}' {
'{'
} else if input == &'<' {
'>'
} else if input == &'>' {
'<'
} else {
panic!()
}
}
fn get_value_for_illegal_character(character: char) -> Result {
if character == ')' {
3
} else if character == ']' {
57
} else if character == '}' {
1197
} else {
25137
}
}
fn get_value_for_unclosed_delimiter(character: &char) -> Result {
match character {
'(' => 1,
'[' => 2,
'{' => 3,
'<' => 4,
_ => panic!(),
}
}
+7
View File
@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aoc11"
version = "0.1.0"
+8
View File
@@ -0,0 +1,8 @@
[package]
name = "aoc11"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+10
View File
@@ -0,0 +1,10 @@
5483143223
2745854711
5264556173
6141336146
6357385478
4167524645
2176841721
6882881134
4846848554
5283751526
+10
View File
@@ -0,0 +1,10 @@
4112256372
3143253712
4516848631
3783477137
3746723582
5861358884
4843351774
2316447621
6643817745
6366815868
+169
View File
@@ -0,0 +1,169 @@
use std::{collections::HashSet, fs};
const NEIGHBOUR_INDICES: [isize; 3] = [-1, 0, 1];
struct Grid {
data: Vec<Vec<u32>>,
nx: usize,
ny: usize,
flashes: usize,
}
impl Grid {
fn from_file(filename: &str) -> Self {
let data: Vec<Vec<_>> = fs::read_to_string(filename)
.unwrap()
.split_whitespace()
.map(|line| line.chars().map(|c| c.to_digit(10).unwrap()).collect())
.collect();
let nx = data[0].len();
let ny = data.len();
let flashes = 0;
Grid {
data,
nx,
ny,
flashes,
}
}
fn iterate(&mut self) {
self.increment_all_by_one();
self.flash_where_level_above_nine();
self.reset_flashed();
}
fn print(&self) {
for y in 0..self.ny {
for x in 0..self.nx {
let val = self.data[y][x];
if val > 9 {
print!(".");
} else {
print!("{}", val);
}
}
println!();
}
}
fn reset_flashed(&mut self) {
for y in 0..self.ny {
for x in 0..self.nx {
if self.data[y][x] > 9 {
self.data[y][x] = 0;
}
}
}
}
fn increment_all_by_one(&mut self) {
for y in 0..self.ny {
for x in 0..self.nx {
self.data[y][x] += 1;
}
}
}
#[allow(clippy::all)]
fn flash_where_level_above_nine(&mut self) {
let mut have_flashed = HashSet::new();
loop {
let mut increased = 0;
for y in 0..self.ny {
for x in 0..self.nx {
if self.data[y][x] > 9 && !have_flashed.contains(&(y, x)) {
have_flashed.insert((y, x));
self.flashes += 1;
increased += self.increase_neighbors_by_one(&x, &y);
}
}
}
if increased == 0 {
break;
}
}
}
fn increase_neighbors_by_one(&mut self, x: &usize, y: &usize) -> i32 {
let mut increased = 0;
for (ox, oy) in self.get_neighbours(x, y) {
self.data[oy][ox] += 1;
increased += 1;
}
increased
}
fn get_neighbours(&self, x: &usize, y: &usize) -> Vec<(usize, usize)> {
let mut result = vec![];
for dx in NEIGHBOUR_INDICES {
for dy in NEIGHBOUR_INDICES {
let ox = (*x as isize) + dx;
let oy = (*y as isize) + dy;
if ox < 0
|| oy < 0
|| ox >= self.nx as isize
|| oy >= self.ny as isize
|| (dx == 0 && dy == 0)
{
continue;
}
result.push((ox as usize, oy as usize));
}
}
result
}
}
fn main() {
let mut grid = Grid::from_file("input.txt");
grid.print();
println!();
let mut i = 0;
for _ in 0..100 {
grid.iterate();
i += 1;
}
grid.print();
println!();
println!("{}", grid.flashes);
loop {
let before = grid.flashes;
grid.iterate();
let after = grid.flashes;
i += 1;
if after - before == grid.nx * grid.ny {
println!();
println!("{i}");
break;
}
}
}
#[cfg(test)]
mod test {
use crate::Grid;
#[test]
fn get_neighbours() {
let data = vec![
vec![1, 1, 1, 1],
vec![1, 1, 1, 1],
vec![1, 1, 1, 1],
vec![1, 1, 1, 1],
];
let nx = 4;
let ny = 4;
let grid = Grid {
data,
nx,
ny,
flashes: 0,
};
println!("{:?}\n", grid.get_neighbours(&1, &1));
println!("{:?}\n", grid.get_neighbours(&2, &2));
println!("{:?}\n", grid.get_neighbours(&0, &0));
}
}
+7
View File
@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aoc12"
version = "0.1.0"
+8
View File
@@ -0,0 +1,8 @@
[package]
name = "aoc12"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+7
View File
@@ -0,0 +1,7 @@
start-A
start-b
A-c
A-b
b-d
A-end
b-end
+131
View File
@@ -0,0 +1,131 @@
use std::collections::HashSet;
use std::fs;
#[derive(Eq, PartialEq, Hash, Clone, Debug)]
enum CaveType {
Start,
End,
Big,
Small,
}
#[derive(Eq, PartialEq, Hash, Clone, Debug)]
struct Cave {
cave_type: CaveType,
name: String,
}
struct Map {
caves: HashSet<Cave>,
connections: HashSet<(Cave, Cave)>,
}
impl Map {
fn from_file(filename: &str) -> Self {
let data = fs::read_to_string(filename).unwrap();
let mut caves = HashSet::new();
let mut connections = HashSet::new();
for line in data.split_whitespace() {
let (before, after) = line.split_once('-').unwrap();
let cave1 = Cave::from_string(before);
let cave2 = Cave::from_string(after);
caves.insert(cave1.clone());
caves.insert(cave2.clone());
connections.insert((cave1, cave2));
}
Self { caves, connections }
}
fn get_connected_caves(&self, cave: &Cave) -> Vec<&Cave> {
self.connections
.iter()
.filter_map(|conn| {
if &conn.0 == cave {
Some(&conn.1)
} else if &conn.1 == cave {
Some(&conn.0)
} else {
None
}
})
.collect()
}
}
impl Cave {
fn new(cave_type: CaveType, name: String) -> Self {
Self { cave_type, name }
}
fn from_string(value: &str) -> Self {
if value == "start" {
Self::get_start()
} else if value == "end" {
Self::get_end()
} else {
assert_eq!(value.len(), 1);
let char = value.chars().next().unwrap();
if char.is_uppercase() {
Self::new(CaveType::Big, value.to_string())
} else if char.is_lowercase() {
Self::new(CaveType::Small, value.to_string())
} else {
panic!()
}
}
}
fn get_start() -> Self {
Self::new(CaveType::Start, "start".to_string())
}
fn get_end() -> Self {
Self::new(CaveType::End, "end".to_string())
}
}
struct Solver<'a> {
map: Map,
start: Cave,
end: Cave,
routes: Vec<Vec<&'a Cave>>,
}
impl<'a> Solver<'a> {
fn new(map: Map, start: Cave, end: Cave) -> Self {
let routes = vec![];
Solver {
map,
start,
end,
routes,
}
}
fn find_routes(&mut self) {
let mut route: Vec<Cave> = self.new_route();
let mut connected = self.map.get_connected_caves(&self.start);
let cave = connected.get(0).unwrap();
route.push(**cave);
self.routes.push(route);
}
fn new_route(&self) -> Vec<Cave> {
vec![self.start]
}
}
fn main() {
let map = Map::from_file("example1.txt");
let start = Cave::get_start();
let end = Cave::get_end();
let mut solver = Solver::new(map, start, end);
solver.find_routes();
}