Skip to content

Commit 8a080a5

Browse files
committed
fix 32-bit
1 parent 94d2f33 commit 8a080a5

File tree

10 files changed

+98
-82
lines changed

10 files changed

+98
-82
lines changed

2016/day9/day9.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ fn main() {
99
}
1010

1111
/// Do part 1 of the puzzle
12-
fn part1(data: &str) -> usize {
13-
data.split('\n').fold(0, |acc, line| acc + expand_v1(line).len())
12+
fn part1(data: &str) -> u64 {
13+
data.split('\n').fold(0, |acc, line| acc + expand_v1(line).len() as u64)
1414
}
1515

1616
/// Do part 2 of the puzzle
17-
fn part2(data: &str) -> usize {
17+
fn part2(data: &str) -> u64 {
1818
data.split('\n').fold(0, |acc, line| acc + expand_v2(line))
1919
}
2020

@@ -53,12 +53,12 @@ fn expand_v1(s: &str) -> String {
5353
}
5454

5555
/// ``expand_v2`` returns the length of expanded string according to the format v2.
56-
fn expand_v2(s: &str) -> usize {
56+
fn expand_v2(s: &str) -> u64 {
5757
expand(s, 2)
5858
}
5959

6060
/// ``expand`` returns the length of the expanded string (v1 or v2).
61-
fn expand(s: &str, version: u8) -> usize {
61+
fn expand(s: &str, version: u8) -> u64 {
6262
let mut new_len = 0;
6363
let mut chars = s.chars();
6464

@@ -76,13 +76,13 @@ fn expand(s: &str, version: u8) -> usize {
7676
.by_ref()
7777
.take_while(|c| *c != ')')
7878
.collect::<String>()
79-
.parse::<usize>()
79+
.parse::<u64>()
8080
.unwrap();
8181

8282
let taken = chars.by_ref().take(take);
8383

8484
let count = if version == 1 {
85-
taken.count()
85+
taken.count() as u64
8686
} else {
8787
expand(taken.collect::<String>().as_str(), version)
8888
};
@@ -101,7 +101,7 @@ fn expand(s: &str, version: u8) -> usize {
101101
fn test_expand_v1() {
102102
fn test_v1(s: &str, expected: &str) {
103103
assert_eq!(expand_v1(s), expected);
104-
assert_eq!(expand(s, 1), expected.len());
104+
assert_eq!(expand(s, 1), expected.len() as u64);
105105
}
106106
test_v1("ADVENT", "ADVENT");
107107
test_v1("A(1x5)BC", "ABBBBBC");

2020/day23/day23.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl Puzzle {
2020
.collect();
2121
}
2222

23-
fn solve(&self, nb_cups: usize, nb_moves: usize) -> usize {
23+
fn solve(&self, nb_cups: usize, nb_moves: usize) -> u64 {
2424
// cups are labeled from 1 to 9 (and from 10 to ncups), cup no. 0 is *unused*
2525

2626
let cups = |i: usize| {
@@ -68,26 +68,26 @@ impl Puzzle {
6868

6969
if nb_cups > self.cups.len() {
7070
// answer for part 2
71-
(pos[pos[1]]) * (pos[1])
71+
(pos[pos[1]] as u64) * (pos[1] as u64)
7272
} else {
7373
// answer for part 1
7474
let mut result = 0;
7575
let mut cup = pos[1];
7676
while cup != 1 {
77-
result = result * 10 + cup;
77+
result = result * 10 + cup as u64;
7878
cup = pos[cup];
7979
}
8080
result
8181
}
8282
}
8383

8484
/// Solve part one.
85-
fn part1(&self) -> usize {
85+
fn part1(&self) -> u64 {
8686
self.solve(self.cups.len(), 100)
8787
}
8888

8989
/// Solve part two.
90-
fn part2(&self) -> usize {
90+
fn part2(&self) -> u64 {
9191
self.solve(1_000_000, 10_000_000)
9292
}
9393
}

2022/day11/day11.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void solve(const std::vector<Monkey> &monkeys_orig, int rounds)
9191
{
9292
return a.inspections > b.inspections;
9393
});
94-
uint64_t monkey_business_level = monkeys[0].inspections * monkeys[1].inspections;
94+
uint64_t monkey_business_level = (uint64_t)monkeys[0].inspections * (uint64_t)monkeys[1].inspections;
9595

9696
std::cout << monkey_business_level << std::endl;
9797
}

2022/day11/day11.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,17 @@ impl Puzzle {
112112
}
113113

114114
/// Solves part one
115-
fn part1(&self) -> usize {
115+
fn part1(&self) -> u64 {
116116
self.solve(20)
117117
}
118118

119119
/// Solve part two
120-
fn part2(&self) -> usize {
120+
fn part2(&self) -> u64 {
121121
self.solve(10000)
122122
}
123123

124124
/// Solve the puzzle
125-
fn solve(&self, rounds: usize) -> usize {
125+
fn solve(&self, rounds: usize) -> u64 {
126126
// we need to clone the monkeys array since we modify the starting item lists
127127
let mut monkeys = self.monkeys.clone();
128128

@@ -161,7 +161,7 @@ impl Puzzle {
161161
.map(|x| x.inspections)
162162
.collect::<Vec<usize>>();
163163
monkey_business.sort_by(|a, b| b.cmp(a));
164-
monkey_business[0] * monkey_business[1]
164+
(monkey_business[0] as u64) * (monkey_business[1] as u64)
165165
}
166166
}
167167

2022/day15/day15.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl Puzzle {
5757
}
5858

5959
// Solves part one
60-
fn part1(&self) -> usize {
60+
fn part1(&self) -> u32 {
6161
let bx_min = self.beacons.iter().map(|x| x.0).min().unwrap() - self.max_d;
6262
let bx_max = self.beacons.iter().map(|x| x.0).max().unwrap() + self.max_d;
6363

@@ -90,7 +90,7 @@ impl Puzzle {
9090
}
9191

9292
// Solve part two
93-
fn part2(&self) -> usize {
93+
fn part2(&self) -> i64 {
9494
for y in 0..=self.field_size {
9595
// each sensor defines a zone where there is only one beacon
9696
// this zone is all points at a distance less than or equal to the Manhattan distance to its beacon
@@ -140,7 +140,7 @@ impl Puzzle {
140140

141141
if column.len() > 1 {
142142
let x = column.first().unwrap().1;
143-
return (x * 4_000_000 + y) as usize;
143+
return x * 4_000_000 + y;
144144
}
145145
}
146146

2022/day20/day20.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,37 +38,37 @@ impl Puzzle {
3838
fn decrypt(&self, key: i64, rounds: usize) -> i64 {
3939
let mut q = VecDeque::new();
4040

41-
q.extend(self.numbers.iter().map(|x| (*x) * key).enumerate());
41+
q.extend(self.numbers.iter().map(|x| (*x) * key).zip(0..));
4242

43-
let nb = self.numbers.len();
43+
let nb = self.numbers.len() as i64;
4444

4545
for _ in 0..rounds {
4646
for i in 0..nb {
47-
let mut shift = (0usize, 0i64);
47+
let mut shift = (0, 0);
4848

4949
while let Some(e) = q.pop_front() {
50-
if e.0 == i {
50+
if e.1 == i {
5151
shift = e;
5252
break;
5353
}
5454
q.push_back(e);
5555
}
5656

57-
match shift.1 {
58-
o if o > 0 => q.rotate_left((o as usize) % (nb - 1)),
59-
o if o < 0 => q.rotate_right((-o as usize) % (nb - 1)),
57+
match shift.0 {
58+
o if o > 0 => q.rotate_left(((o) % (nb - 1)) as usize),
59+
o if o < 0 => q.rotate_right(((-o) % (nb - 1)) as usize),
6060
_ => (),
6161
}
6262

6363
q.push_back(shift);
6464
}
6565
}
6666

67-
for (i, v) in q.iter().enumerate() {
68-
if v.1 == 0 {
69-
return q.get((i + 1000) % nb).unwrap().1
70-
+ q.get((i + 2000) % nb).unwrap().1
71-
+ q.get((i + 3000) % nb).unwrap().1;
67+
for (v, i) in q.iter().zip(0..) {
68+
if v.0 == 0 {
69+
return q.get(((i + 1000) % nb) as usize).unwrap().0
70+
+ q.get(((i + 2000) % nb) as usize).unwrap().0
71+
+ q.get(((i + 3000) % nb) as usize).unwrap().0;
7272
}
7373
}
7474
0

2023/day11/day11.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! [Day 11: Cosmic Expansion](https://adventofcode.com/2023/day/11)
22
33
struct Puzzle {
4-
galaxies: Vec<(usize, usize)>,
5-
empty_rows: Vec<usize>,
6-
empty_cols: Vec<usize>,
4+
galaxies: Vec<(u64, u64)>,
5+
empty_rows: Vec<u64>,
6+
empty_cols: Vec<u64>,
77
}
88

99
impl Puzzle {
@@ -21,9 +21,9 @@ impl Puzzle {
2121

2222
let mut grid = vec![];
2323

24-
for (y, line) in data.lines().enumerate() {
24+
for (line, y) in data.lines().zip(0..) {
2525
let row: Vec<_> = line.chars().collect();
26-
for (x, &c) in row.iter().enumerate() {
26+
for (&c, x) in row.iter().zip(0..) {
2727
if c == '#' {
2828
self.galaxies.push((x, y));
2929
}
@@ -38,12 +38,12 @@ impl Puzzle {
3838

3939
for x in 0..grid[0].len() {
4040
if (0..grid.len()).all(|y| grid[y][x] == '.') {
41-
self.empty_cols.push(x);
41+
self.empty_cols.push(x as u64);
4242
}
4343
}
4444
}
4545

46-
fn solve(&self, expansion_factor: usize) -> usize {
46+
fn solve(&self, expansion_factor: u64) -> u64 {
4747
let expansion_factor = expansion_factor - 1;
4848
let mut result = 0;
4949
for (i, &(x1, y1)) in self.galaxies.iter().enumerate() {
@@ -55,15 +55,15 @@ impl Puzzle {
5555
.empty_cols
5656
.iter()
5757
.filter(|&&col| x1.min(x2) <= col && col <= x1.max(x2))
58-
.count()
58+
.count() as u64
5959
* expansion_factor;
6060

6161
// expand empty spaces vertically
6262
distance += self
6363
.empty_rows
6464
.iter()
6565
.filter(|&&row| y1.min(y2) <= row && row <= y1.max(y2))
66-
.count()
66+
.count() as u64
6767
* expansion_factor;
6868

6969
result += distance;
@@ -74,12 +74,12 @@ impl Puzzle {
7474
}
7575

7676
/// Solve part one.
77-
fn part1(&self) -> usize {
77+
fn part1(&self) -> u64 {
7878
self.solve(2)
7979
}
8080

8181
/// Solve part two.
82-
fn part2(&self) -> usize {
82+
fn part2(&self) -> u64 {
8383
self.solve(1_000_000)
8484
}
8585
}

2023/day21/day21.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl Puzzle {
6363
self.garden[usize::try_from(i).unwrap()]
6464
}
6565

66-
fn count(&self, n: i32) -> usize {
66+
fn count(&self, n: i32) -> u64 {
6767
// nota: still not really optimized, could probably memoize something
6868
let mut p = HashSet::new();
6969
p.insert((self.start_x, self.start_y));
@@ -116,10 +116,10 @@ impl Puzzle {
116116
p = np;
117117
}
118118

119-
p.len()
119+
p.len() as u64
120120
}
121121

122-
fn big_count(&self, n: i32) -> usize {
122+
fn big_count(&self, n: i32) -> u64 {
123123
// the step count curve is parabolic
124124
let (t, x0) = n.div_rem(&self.n);
125125

@@ -138,18 +138,18 @@ impl Puzzle {
138138
let a = y2 - 2 * y1 + y0;
139139
let b = y1 - y0;
140140

141-
let t = usize::try_from(t).unwrap();
141+
let t = u64::try_from(t).unwrap();
142142

143143
a * t * (t - 1) / 2 + b * t + y0
144144
}
145145

146146
/// Solve part one.
147-
fn part1(&self) -> usize {
147+
fn part1(&self) -> u64 {
148148
self.count(64)
149149
}
150150

151151
/// Solve part two.
152-
fn part2(&self) -> usize {
152+
fn part2(&self) -> u64 {
153153
self.big_count(26_501_365)
154154
}
155155
}

2023/day8/day8.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use num::Integer;
44
use std::collections::HashMap;
55

6-
fn lcm(values: &Vec<usize>) -> usize {
6+
fn lcm(values: &Vec<u64>) -> u64 {
77
let mut m = 1;
88
for x in values {
99
m = m.lcm(x);
@@ -44,7 +44,7 @@ impl Puzzle {
4444
}
4545
}
4646

47-
fn solve(&self, part1: bool) -> usize {
47+
fn solve(&self, part1: bool) -> u64 {
4848
let start = u32::from_str_radix(if part1 { "AAA" } else { "A" }, 36).unwrap();
4949
let stop = u32::from_str_radix(if part1 { "ZZZ" } else { "Z" }, 36).unwrap();
5050
let mask = if part1 { 36 * 36 * 36 } else { 36 };
@@ -81,10 +81,10 @@ impl Puzzle {
8181
*node = new_node;
8282

8383
if new_node % mask == stop {
84-
z.insert(i, n);
84+
z.insert(i, n as u64);
8585

8686
if z.len() == size {
87-
let z = z.values().copied().collect::<Vec<usize>>();
87+
let z: Vec<_> = z.values().copied().collect();
8888
return lcm(&z);
8989
}
9090
}
@@ -93,12 +93,12 @@ impl Puzzle {
9393
}
9494

9595
/// Solve part one.
96-
fn part1(&self) -> usize {
96+
fn part1(&self) -> u64 {
9797
self.solve(true)
9898
}
9999

100100
/// Solve part two.
101-
fn part2(&self) -> usize {
101+
fn part2(&self) -> u64 {
102102
self.solve(false)
103103
}
104104
}

0 commit comments

Comments
 (0)