intial commit uwu
This commit is contained in:
commit
7b645a9ace
9 changed files with 2107 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/target
|
8
.idea/.gitignore
vendored
Normal file
8
.idea/.gitignore
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
11
.idea/aoc2021.iml
Normal file
11
.idea/aoc2021.iml
Normal file
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="CPP_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$/../aoc2021">
|
||||
<sourceFolder url="file://$MODULE_DIR$/../aoc2021/src" isTestSource="false" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/../aoc2021/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/aoc2021.iml" filepath="$PROJECT_DIR$/.idea/aoc2021.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="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
|
@ -0,0 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "aoc2021"
|
||||
version = "0.1.0"
|
10
Cargo.toml
Normal file
10
Cargo.toml
Normal file
|
@ -0,0 +1,10 @@
|
|||
[package]
|
||||
name = "aoc2021"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[[bin]]
|
||||
name = "day01"
|
||||
path = "src/day01/main.rs"
|
||||
|
||||
[dependencies]
|
2000
input/day01.txt
Normal file
2000
input/day01.txt
Normal file
File diff suppressed because it is too large
Load diff
56
src/day01/main.rs
Normal file
56
src/day01/main.rs
Normal file
|
@ -0,0 +1,56 @@
|
|||
use std::io;
|
||||
|
||||
/// full-ascending circular stack
|
||||
struct ShiftReg {
|
||||
vals: [i32; 4],
|
||||
pos: usize,
|
||||
}
|
||||
|
||||
impl ShiftReg {
|
||||
pub const fn new() -> ShiftReg {
|
||||
ShiftReg {
|
||||
vals: [i32::MIN, i32::MIN, i32::MIN, i32::MIN],
|
||||
pos: 3,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push(&mut self, val: i32) {
|
||||
self.pos += 1;
|
||||
self.pos %= 4;
|
||||
self.vals[self.pos] = val;
|
||||
}
|
||||
|
||||
pub fn diff(&self) -> i32 {
|
||||
// i0 A
|
||||
// i1 A B
|
||||
// i2 A B
|
||||
// i3 B
|
||||
//
|
||||
// A = (i0 + (i1 + i2) )
|
||||
// B = ( (i1 + i2) + i3)
|
||||
// B - A = i3 - i0
|
||||
let first = self.vals[self.pos]; // i3
|
||||
let last = self.vals[(self.pos + 1) % 4]; // i0
|
||||
first.wrapping_sub(last)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let stdin = io::stdin();
|
||||
let mut sr = ShiftReg::new();
|
||||
let mut count = 0;
|
||||
loop {
|
||||
let mut buffer = String::new();
|
||||
match stdin.read_line(&mut buffer).unwrap() {
|
||||
0 => break,
|
||||
_ => {
|
||||
let val = buffer.trim().parse::<i32>().unwrap();
|
||||
sr.push(val);
|
||||
if sr.diff() > 0 {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
println!("{}", count);
|
||||
}
|
Loading…
Reference in a new issue