Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit87fe64d

Browse files
author
Lukasz
committed
Day06
1 parent6032e41 commit87fe64d

File tree

7 files changed

+88
-42
lines changed

7 files changed

+88
-42
lines changed

‎advent-of-code/2021/input/day06.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3,5,3,5,1,3,1,1,5,5,1,1,1,2,2,2,3,1,1,5,1,1,5,5,3,2,2,5,4,4,1,5,1,4,4,5,2,4,1,1,5,3,1,1,4,1,1,1,1,4,1,1,1,1,2,1,1,4,1,1,1,2,3,5,5,1,1,3,1,4,1,3,4,5,1,4,5,1,1,4,1,3,1,5,1,2,1,1,2,1,4,1,1,1,4,4,3,1,1,1,1,1,4,1,4,5,2,1,4,5,4,1,1,1,2,2,1,4,4,1,1,4,1,1,1,2,3,4,2,4,1,1,5,4,2,1,5,1,1,5,1,2,1,1,1,5,5,2,1,4,3,1,2,2,4,1,2,1,1,5,1,3,2,4,3,1,4,3,1,2,1,1,1,1,1,4,3,3,1,3,1,1,5,1,1,1,1,3,3,1,3,5,1,5,5,2,1,2,1,4,2,3,4,1,4,2,4,2,5,3,4,3,5,1,2,1,1,4,1,3,5,1,4,1,2,4,3,1,5,1,1,2,2,4,2,3,1,1,1,5,2,1,4,1,1,1,4,1,3,3,2,4,1,4,2,5,1,5,2,1,4,1,3,1,2,5,5,4,1,2,3,3,2,2,1,3,3,1,4,4,1,1,4,1,1,5,1,2,4,2,1,4,1,1,4,3,5,1,2,1

‎advent-of-code/2021/src/days/day06.rs

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,80 @@
1-
usecrate::solution::{self,Solution};
1+
usesuper::solution::{self,Solution};
22

33
pubstructDay06{}
44

5+
implDay06{
6+
fnparse_input() ->Vec<Lanternfish>{
7+
solution::load_input("06").lines()
8+
.next()
9+
.unwrap()
10+
.split(',')
11+
.map(|x|Lanternfish::new(x.parse().unwrap()))
12+
.collect()
13+
}
14+
15+
fnparse_input2() ->String{
16+
solution::load_input("06")
17+
}
18+
}
19+
520
implSolutionforDay06{
6-
fnday_number(&self) ->String{
7-
"06".to_owned();
21+
fnday_number(&self) ->&str{
22+
"06"
823
}
924

1025
fnpart_1(&self) ->String{
11-
let input =parse_input();
12-
let res =Lanternfish::simulation(input,80);
13-
res.to_string();
26+
letmutinput =Day06::parse_input();
27+
let res =Lanternfish::simulation(&mutinput,80);
28+
res.to_string()
1429
}
1530

1631
fnpart_2(&self) ->String{
17-
32+
let input =Day06::parse_input2();
33+
let res =Lanternfish::simulation2(&input,256);
34+
res.to_string()
1835
}
1936
}
2037

21-
22-
fnparse_input() ->Vec<Lanternfish>{
23-
solution::load_input(Day06::day_number()).lines()
24-
.next()
25-
.unwrap()
26-
.split(',')
27-
.map(|x|Lanternfish::new(x.parse().unwrap()))
28-
.collect()
29-
}
30-
31-
3238
structLanternfish{
33-
timer:u32,
39+
timer:i32
3440
}
3541

3642
implLanternfish{
37-
fnnew(timer:u32) _>Lanternfish{
38-
Lanternfish{timer};
43+
fnnew(timer:i32) ->Self{
44+
Lanternfish{timer}
3945
}
4046

41-
fnsimulation(lanternfishes:&Vec<Lanternfish>,days:u32) ->usize{
47+
fnsimulation(lanternfishes:&mutVec<Lanternfish>,days:u32) ->usize{
4248
for _in0..days{
43-
for&fishin lanternfishes{
49+
letmut v:Vec<Lanternfish> =Vec::new();
50+
for fishin lanternfishes.iter_mut(){
4451
if fish.timer ==0{
45-
fish.timer ==6
46-
lanternfishes.push(Lanternfish::new(8));
52+
fish.timer =6;
53+
v.push(Lanternfish::new(8));
54+
}
55+
else{
56+
fish.timer -=1;
4757
}
4858
}
59+
lanternfishes.extend(v);
60+
}
61+
62+
lanternfishes.len()
63+
}
64+
65+
// Based on: https://github.com/akaritakai/AdventOfCode2021-Rust/blob/main/src/puzzle06.rs
66+
fnsimulation2(input:&String,days:usize) ->u64{
67+
letmut fish:[u64;9] =[0;9];
68+
input
69+
.trim()
70+
.split(',')
71+
.map(|x| x.parse::<usize>().unwrap())
72+
.for_each(|x| fish[x] +=1);
73+
letmut base =0;
74+
for _in0..days{
75+
fish[(base +7) %9] += fish[base];
76+
base =(base +1) %9;
4977
}
78+
fish.iter().sum()
5079
}
5180
}

‎advent-of-code/2021/src/days/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
usecrate::solution::Solution;
2-
3-
use day06::Day06;
4-
1+
pubmod solution;
52
pubmod day06;
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use std::fs;
2-
use std::io;
3-
2+
use std::path::Path;
43

54
pubtraitSolution{
6-
fnday_number(&self) ->String;
5+
fnday_number(&self) ->&str;
76
fnpart_1(&self) ->String;
87
fnpart_2(&self) ->String;
98
}
109

1110
pubfnload_input(day_number:&str) ->String{
12-
let file_path =format!("input/day{}.txt", day_number);
13-
fs::read_to_string(&file_path).unwrap_or_else(|_|panic!("Error while reading file: {}", file_path));
11+
let path =format!("../input/day{}.txt", day_number);
12+
let file_path =Path::new(&path);
13+
fs::read_to_string(&file_path).unwrap_or_else(|_|panic!("Error while reading file: {}", file_path.display()))
1414
}

‎advent-of-code/2021/src/main.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
11
mod days;
2-
mod solution;
2+
mod utils;
3+
4+
structDaySolutions{
5+
days:Vec<Box<dyn days::solution::Solution>>,
6+
}
7+
8+
implDaySolutions{
9+
fnadd(&mutself,day_solution:Box<dyn days::solution::Solution>){
10+
self.days.push(day_solution)
11+
}
12+
13+
fnrun(&self){
14+
for dayinself.days.iter(){
15+
println!("Day:{} -> part_1={}", day.day_number(), day.part_1());
16+
println!("Day:{} -> part_2={}", day.day_number(), day.part_2());
17+
}
18+
}
19+
}
20+
321

422
fnmain(){
523
run();
624
}
725

8-
//println!("part_1={}", submarine.h_pos * submarine.depth);
9-
1026
fnrun(){
11-
let solutions =25;
12-
let day =Day06::new();
13-
println!("part_1={}", day.part_1());
27+
letmut solutions =DaySolutions{days:Vec::new()};
28+
// Add all the days
29+
solutions.add(Box::new(days::day06::Day06{}));
30+
// Run all the soulutions
31+
solutions.run();
1432
}

‎advent-of-code/2021/src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pubmod timer;

‎advent-of-code/2021/src/timer.rsrenamed to ‎advent-of-code/2021/src/utils/timer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use core::f32;
22
use std::time::{SystemTime};
33

44
pubstructTimer{
5-
timer:SystemTime;
5+
timer:SystemTime,
66
}
77

88

@@ -14,7 +14,7 @@ impl Timer {
1414
}
1515

1616
fnprint(&self){
17-
let elapsed =(self.timer.elapsed().unwrap().as_millis()asf32);
17+
let elapsed =self.timer.elapsed().unwrap().as_millis()asf32;
1818
println!("Elpased: {} [ms]", elapsed)
1919
}
2020
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp