Initial commit and day 1

This commit is contained in:
Smaug123
2021-05-08 11:33:12 +01:00
commit c1527220e5
9 changed files with 123 additions and 0 deletions

22
.github/workflows/rust.yml vendored Normal file
View File

@@ -0,0 +1,22 @@
name: Rust
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
env:
CARGO_TERM_COLOR: always
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
**/target

5
Cargo.lock generated Normal file
View File

@@ -0,0 +1,5 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "advent-of-code-2017"
version = "0.1.0"

9
Cargo.toml Normal file
View File

@@ -0,0 +1,9 @@
[package]
name = "advent-of-code-2017"
version = "0.1.0"
authors = ["Smaug123 <patrick+github@patrickstevens.co.uk>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

5
day_1/Cargo.lock generated Normal file
View File

@@ -0,0 +1,5 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "day_1"
version = "0.1.0"

9
day_1/Cargo.toml Normal file
View File

@@ -0,0 +1,9 @@
[package]
name = "day_1"
version = "0.1.0"
authors = ["Smaug123 <patrick+github@patrickstevens.co.uk>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

1
day_1/input.txt Normal file
View File

@@ -0,0 +1 @@
77736991856689225253142335214746294932318813454849177823468674346512426482777696993348135287531487622845155339235443718798255411492778415157351753377959586612882455464736285648473397681163729345143319577258292849619491486748832944425643737899293811819448271546283914592546989275992844383947572926628695617661344293284789225493932487897149244685921644561896799491668147588536732985476538413354195246785378443492137893161362862587297219368699689318441563683292683855151652394244688119527728613756153348584975372656877565662527436152551476175644428333449297581939357656843784849965764796365272113837436618857363585783813291999774718355479485961244782148994281845717611589612672436243788252212252489833952785291284935439662751339273847424621193587955284885915987692812313251556836958571335334281322495251889724281863765636441971178795365413267178792118544937392522893132283573129821178591214594778712292228515169348771198167462495988252456944269678515277886142827218825358561772588377998394984947946121983115158951297156321289231481348126998584455974277123213413359859659339792627742476688827577318285573236187838749444212666293172899385531383551142896847178342163129883523694183388123567744916752899386265368245342587281521723872555392212596227684414269667696229995976182762587281829533181925696289733325513618571116199419759821597197636415243789757789129824537812428338192536462468554399548893532588928486825398895911533744671691387494516395641555683144968644717265849634943691721391779987198764147667349266877149238695714118982841721323853294642175381514347345237721288281254828745122878268792661867994785585131534136646954347165597315643658739688567246339618795777125767432162928257331951255792438831957359141651634491912746875748363394329848227391812251812842263277229514125426682179711184717737714178235995431465217547759282779499842892993556918977773236196185348965713241211365895519697294982523166196268941976859987925578945185217127344619169353395993198368185217391883839449331638641744279836858188235296951745922667612379649453277174224722894599153367373494255388826855322712652812127873536473277

68
day_1/src/lib.rs Normal file
View File

@@ -0,0 +1,68 @@
fn input() -> Vec<u32> {
let input = include_str!("../input.txt");
input
.trim()
.chars()
.map(|l| l.to_digit(10).expect(&format!("{} wasn't a valid u32", l)))
.collect::<Vec<u32>>()
}
pub fn part_1(numbers: &[u32]) -> u32 {
let mut sum = 0;
let mut previous = numbers[0];
let len = numbers.len();
for i in 1..len {
if numbers[i] == previous {
sum += previous;
}
previous = numbers[i];
}
if len > 1 {
if previous == numbers[0] {
sum += previous;
}
}
sum
}
pub fn part_2(numbers: &[u32]) -> u32 {
let mut sum = 0;
let len = numbers.len();
for i in 0..len / 2 {
if numbers[i] == numbers[len / 2 + i] {
sum += 2 * numbers[i];
}
}
sum
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn part1_known() {
assert_eq!(part_1(&[1, 1, 2, 2]), 3);
assert_eq!(part_1(&[1, 1, 1, 1]), 4);
assert_eq!(part_1(&[1, 2, 3, 4]), 0);
assert_eq!(part_1(&[9, 1, 2, 1, 2, 1, 2, 9]), 9);
}
#[test]
fn part2_known() {
assert_eq!(part_2(&[1, 2, 1, 2]), 6);
assert_eq!(part_2(&[1, 2, 2, 1]), 0);
assert_eq!(part_2(&[1, 2, 3, 4, 2, 5]), 4);
assert_eq!(part_2(&[1, 2, 3, 1, 2, 3]), 12);
assert_eq!(part_2(&[1, 2, 1, 3, 1, 4, 1, 5]), 4);
}
#[test]
fn test_day_1() {
let input = input();
assert_eq!(part_1(&input), 1223);
assert_eq!(part_2(&input), 1284);
}
}

3
src/main.rs Normal file
View File

@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}