First couple of days in 2016 in Rust
This commit is contained in:
parent
1e70780b66
commit
9ff2561199
14 changed files with 232 additions and 0 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1 +1,3 @@
|
|||
__pycache__/
|
||||
*.lock
|
||||
target/
|
||||
|
|
8
.idea/.gitignore
vendored
Normal file
8
.idea/.gitignore
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Datasource local storage ignored files
|
||||
/../../../../../../:\Users\Terrana\Documents\advent-of-code\.idea/dataSources/
|
||||
/dataSources.local.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
6
.idea/misc.xml
Normal file
6
.idea/misc.xml
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7" project-jdk-type="Python SDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
10
.idea/modules.xml
Normal file
10
.idea/modules.xml
Normal file
|
@ -0,0 +1,10 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/2015/2015.iml" filepath="$PROJECT_DIR$/2015/2015.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/2019/2019.iml" filepath="$PROJECT_DIR$/2019/2019.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/2016/aoc2016.iml" filepath="$PROJECT_DIR$/2016/aoc2016.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
9
2015/2015.iml
Normal file
9
2015/2015.iml
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
9
2016/Cargo.toml
Normal file
9
2016/Cargo.toml
Normal file
|
@ -0,0 +1,9 @@
|
|||
[package]
|
||||
name = "advent-of-code"
|
||||
version = "0.1.0"
|
||||
authors = ["Terrana Ninetailed <terrana@ninetailed.net>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
11
2016/aoc2016.iml
Normal file
11
2016/aoc2016.iml
Normal file
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="RUST_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
5
2016/resources/day02.input
Normal file
5
2016/resources/day02.input
Normal file
|
@ -0,0 +1,5 @@
|
|||
RLRLLLULULULUUDUULULRDDLURURDDLDUUDDLRDDUUUDDRUDLRRDDUDUUDULUDRDULRUDRULRDRUDLDDULRRDLDRLUDDLLDRDDDUDDLUDUDULDRLLDRLULRLURDLULRUUUDRULLUUDLRDLDDUDRRRLDLRUUURRLDDRRRURLLULDUULLDRLRDLLDURDLDDULLDDLDLUURRRURLRURLLRRDURLDUDDLULUUULULLLDRRRRRLULRDUDURURLULRURRRLLUURDURULRRUULDRDLULDLLUDLUDRLUDLRRLDLLDLDUDDLULLDRULRLRULDURRDLDLLUDRLLDRRDLDUDUURUURDUUDDDLDLDDRDLUDLDUUUUDLDRLRURDLURURDLLLUURURDRDLUDLLRUDULLLDLULLULLDLDDRDRRRUDDDUDDDDRULLLLRLDDLLRDRLLLRRLDRRUDRUUURLLLRULRRDURDLDRLDDUUDUUURRLRRUDLDLDDRUDLULLUUDUUUDLUDDRUULLLURUDDDDLRUDDLLLRUR
|
||||
LDLRLDDDLUDRDRRUDUURLRULLUDDRLURLUULDLLRLLUDLRLRUDLULRLRRLRURLDDDURUDUUURDRLDDLUUUDRUDUDDDLLURLLULRUULLUDRULUDDULDUDUDULLDRUUUULRDUUDLUDURDLLRLLRLUUDUUDRLLLRULUURUDLDRLLDUDLDDRULDULDURRLDDDUDUDDRUDUDRDURLLLLLULDRDDLLUDULLLUDRURLDLDLDULLDDRURRLUDDRLURLULRLDDDUUUURLRDLRURDDURLDLRRLLRLRLUURRLLDDLDRLRDUDDLLDDDURUUDURLRRDUULRRDDRRUULDRLRUDRRLDDRLDRULLDLDURRULDURRRDLRRLRLLLRLDRLLULRRLLLLLDLDDULDLLDLLDUUDDRLURUUUUULRDDLRDLRDRDRDLUDDLDDRULLUDDRLDLLUDRLUURRLUDURURLLRURRURRLRLLRLURURDDDDRRLURDUULLUU
|
||||
LLRRDURRDLDULRDUDLRDRDRURULDURUDRRURDDDRLDLDRDRDRDRULDUURLULDDUURUULUDULLDUDLLLLDLLLDRLUUULLULDDRRUDDULLLULRDRULDDULDUDRDDLUUURULDLLUDUUUUURUDLLDRDULLRULLDURDRLLDLDRDDURUULUDURRRUULLDUUDDURDURLDLRRLLDURDDLRRRUDLRRRDLDRLUDLUDRDRLDDLLLRLLRURDLRDUUUURRLULDDLDLLLUDRDRLRRDURDDLURDLDDDULLLRRLDDDRULDDDLRRDULUUUDRRULDDLLLURDRRLLLUULDRRRUURRDDLULDRLULDDDLDULDRRRULRULLURLURULLLLRUDRRRDRDRDLDULURLRRRRLRUDDRRRUURUURLLRURURUURRURRDLDLLUDRRRDUDDRDURLLRLRRULD
|
||||
DULRRDRLRLUDLLURURLLRLRDLLDLLDRDUURLRUUUDLLDUUDDUULDUULLRUDRURLUDRDLRUDDDLULUDLLDRULULLLDRRULDLLUURLRRRLDRDLDRURRRRDLRUUDULLRLLLDLRUDLDUUDRLDLRDRLRDLDDDUDLRUDLDDLLLDRLLRRUUDRDDUUURURRRUUDLRRDDRUDLDDULULDLRRLRDDUDRUURRUULURLURUDRRURRRULDDDDURDLUUULUULULRDLRRRRRURURRLRUULDUUURRDRRDLDUUUULLULLLLUDLUUDUURRDLDLRRRLUUURULDULDLDRLLURDRUULLLLLULLLDRURURRUDRRRRUDUDUDRUDUDRDRULUUDRURDDUUDLDLDUURUDURLRLRRDRDRDLLDUDDULLRDLDDRLLDLRDURDDULLLDLLLULDLUUUDLDRDLURUURDDLRDLLLLLRLURDLLLULLRRLU
|
||||
DUULULUUDUDLLRLRURULLDLRRLURDLLDUDUDDRURRLUDULULDRRDRLUULUDDLUURURDLDDDRDRUDURLDDLUDUURULRRUUDRLURRLRLDURRRULRLDDDRUDDDDDUDDULLLRRLLDULDRULUDLRRDLLUDRDLDULRLLLUULLRULRLLLLUDDRRDRLULDLDLURDDRUDDLDLDLDRULDLLDDUUDULUULULLURDURRLLUDRULLRDUDRDRURDRDRDURUUDULDDRURUDLLUUDUUDURDLRDRURUDRUURLUUURLRLUDRUDRUURLLUDRLURDDURRUDRDRLRRLDDDRDDLUUUDDLULDUURUDUDLLDRURDURRDULRLURRDLDDRLUDRLDLRLDDUURRULDDLDUDDLRDULLDDDLDUUUUDLRUDUDLDRDLRDDLDLRLLUDDRRLUDLDUUULLDDRLRRDLRRRRUDDLRLLULRLRDURDUDDRRULLDDLDLRRDLLULDURURDDURLRLULULURRUDUDRDLURULDUDLUULDUUURLLRUDLLRDLRUDRLULDUDRRDUUDUUULUUUDDRUD
|
69
2016/src/day01.rs
Normal file
69
2016/src/day01.rs
Normal file
|
@ -0,0 +1,69 @@
|
|||
use std::str::Chars;
|
||||
use std::num::ParseIntError;
|
||||
use std::collections::HashSet;
|
||||
|
||||
const INPUT_DATA: &'static str = "L5, R1, R3, L4, R3, R1, L3, L2, R3, L5, L1, L2, R5, L1, R5, R1, \
|
||||
L4, R1, R3, L4, L1, R2, R5, R3, R1, R1, L1, R1, L1, L2, L1, R2, L5, L188, L4, R1, R4, L3, R47, R1, \
|
||||
L1, R77, R5, L2, R1, L2, R4, L5, L1, R3, R187, L4, L3, L3, R2, L3, L5, L4, L4, R1, R5, L4, L3, L3, \
|
||||
L3, L2, L5, R1, L2, R5, L3, L4, R4, L5, R3, R4, L2, L1, L4, R1, L3, R1, R3, L2, R1, R4, R5, L3, \
|
||||
R5, R3, L3, R4, L2, L5, L1, L1, R3, R1, L4, R3, R3, L2, R5, R4, R1, R3, L4, R3, R3, L2, L4, L5, \
|
||||
R1, L4, L5, R4, L2, L1, L3, L3, L5, R3, L4, L3, R5, R4, R2, L4, R2, R3, L3, R4, L1, L3, R2, R1, \
|
||||
R5, L4, L5, L5, R4, L5, L2, L4, R4, R4, R1, L3, L2, L4, R3";
|
||||
|
||||
pub(crate) fn part1() {
|
||||
println!("{}", "Day 1 part 1");
|
||||
let chars: Chars = INPUT_DATA.chars();
|
||||
let mut position: (i32, i32) = (0, 0);
|
||||
let mut direction: (i32, i32) = (0, 1);
|
||||
let mut distance_str: String = String::new();
|
||||
for char in chars {
|
||||
if char == 'L' {
|
||||
direction = (-direction.1, direction.0);
|
||||
} else if char == 'R' {
|
||||
direction = (direction.1, -direction.0)
|
||||
} else if char == ',' {
|
||||
position = do_move(position, direction, distance_str);
|
||||
distance_str = String::new();
|
||||
} else if char >= '0' && char <= '9' {
|
||||
distance_str.push(char);
|
||||
}
|
||||
}
|
||||
position = do_move(position, direction, distance_str);
|
||||
println!("({}, {}) {}", position.0, position.1, position.0.abs() + position.1.abs());
|
||||
}
|
||||
|
||||
pub(crate) fn part2() {
|
||||
println!("{}", "Day 1 part 2");
|
||||
let mut visited: HashSet<(i32, i32)> = HashSet::new();
|
||||
visited.insert((0,0));
|
||||
let mut position: (i32, i32) = (0, 0);
|
||||
let mut direction: (i32, i32) = (0, 1);
|
||||
for instruction in INPUT_DATA.split(", ") {
|
||||
let (dir_str, distance_str): (&str, &str) = instruction.split_at(1);
|
||||
if dir_str == "L" {
|
||||
direction = (-direction.1, direction.0);
|
||||
} else if dir_str == "R" {
|
||||
direction = (direction.1, -direction.0)
|
||||
}
|
||||
let distance: i32 = distance_str.parse::<i32>().unwrap();
|
||||
for _ in 0..distance {
|
||||
position = add(position, direction);
|
||||
if visited.contains(&position) {
|
||||
println!("{:?} {}", position, position.0.abs() + position.1.abs());
|
||||
return;
|
||||
}
|
||||
visited.insert(position);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn do_move(position: (i32, i32), direction: (i32, i32), distance_str: String) -> (i32, i32) {
|
||||
let result: Result<i32, ParseIntError> = distance_str.parse::<i32>();
|
||||
let distance: i32 = result.unwrap();
|
||||
let new_position = (position.0 + distance * direction.0, position.1 + distance * direction.1);
|
||||
new_position
|
||||
}
|
||||
|
||||
fn add(t1: (i32,i32), t2: (i32, i32)) -> (i32, i32) {
|
||||
(t1.0 + t2.0, t1.1 + t2.1)
|
||||
}
|
10
2016/src/day02.rs
Normal file
10
2016/src/day02.rs
Normal file
|
@ -0,0 +1,10 @@
|
|||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
|
||||
pub fn part1() {
|
||||
println!("{}", "Day 2 part 1");
|
||||
let mut input = File::open("resources/day02.input").expect("File open: ");
|
||||
let mut contents = String::new();
|
||||
input.read_to_string(&mut contents).expect("File read: ");
|
||||
println!("Input\n{}", contents);
|
||||
}
|
69
2016/src/main.rs
Normal file
69
2016/src/main.rs
Normal file
|
@ -0,0 +1,69 @@
|
|||
use std::io;
|
||||
use std::collections::HashMap;
|
||||
use std::option::Option;
|
||||
use std::io::Write;
|
||||
use std::hash::Hash;
|
||||
|
||||
mod day01;
|
||||
mod day02;
|
||||
|
||||
struct NavItem<'a> {
|
||||
map: Option<HashMap<&'a str, NavItem<'a>>>,
|
||||
function: Option<fn()>
|
||||
}
|
||||
impl<'a> NavItem<'a> {
|
||||
pub fn new(map: Option<HashMap<&'a str, NavItem<'a>>>, function: Option<fn()>) -> Self {
|
||||
Self { map, function }
|
||||
}
|
||||
}
|
||||
|
||||
fn navmap<'a>(map: HashMap<&'a str, NavItem<'a>>) -> NavItem<'a> {
|
||||
NavItem::new(Some(map), None)
|
||||
}
|
||||
|
||||
fn navfn<'a>(function: fn()) -> NavItem<'a> {
|
||||
NavItem::new(None, Some(function))
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut solutions = HashMap::new();
|
||||
let mut day1 = HashMap::new();
|
||||
day1.insert("1", navfn(day01::part1));
|
||||
day1.insert("2", navfn(day01::part2));
|
||||
solutions.insert("1", navmap(day1));
|
||||
let mut day2 = HashMap::new();
|
||||
day2.insert("1", navfn(day02::part1));
|
||||
solutions.insert("2", navmap(day2));
|
||||
let mut selection= String::new();
|
||||
print!("{}", "> ");
|
||||
match io::stdout().flush() {
|
||||
Ok(_) => {},
|
||||
Err(e) => {
|
||||
println!("{:?}", e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
match io::stdin().read_line(&mut selection) {
|
||||
Ok(_) => {},
|
||||
Err(e) => {
|
||||
println!("{:?}", e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
let mut nav = &NavItem::new(Some(solutions), None);
|
||||
for part in selection.split(' ') {
|
||||
match &nav.map.as_ref() {
|
||||
Some(map) => {
|
||||
nav = &map[part.trim()];
|
||||
},
|
||||
None => {}
|
||||
}
|
||||
match nav.function {
|
||||
Some(f) => {
|
||||
f();
|
||||
break;
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
}
|
9
2019/2019.iml
Normal file
9
2019/2019.iml
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
9
advent-of-code.iml
Normal file
9
advent-of-code.iml
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
Loading…
Reference in a new issue