Skip to content

Commit 6edbb3f

Browse files
committed
[Day 9] Improve
1 parent b27238d commit 6edbb3f

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ This Julia package contains my solutions for [Advent of Code 2024](https://adven
1818
| 6 | [:white_check_mark:](https://adventofcode.com/2024/day/6) | 549.253 ms | 50.91 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day06.jl) |
1919
| 7 | [:white_check_mark:](https://adventofcode.com/2024/day/7) | 30.710 ms | 1.53 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day07.jl) |
2020
| 8 | [:white_check_mark:](https://adventofcode.com/2024/day/8) | 94.050 μs | 46.30 KiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day08.jl) |
21-
| 9 | [:white_check_mark:](https://adventofcode.com/2024/day/9) | 28.732 ms | 9.58 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day09.jl) |
21+
| 9 | [:white_check_mark:](https://adventofcode.com/2024/day/9) | 28.311 ms | 9.58 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day09.jl) |
2222
| 10 | [:white_check_mark:](https://adventofcode.com/2024/day/10) | 578.868 μs | 633.67 KiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day10.jl) |
2323
| 11 | [:white_check_mark:](https://adventofcode.com/2024/day/11) | 15.667 ms | 12.60 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day11.jl) |
2424
| 12 | [:white_check_mark:](https://adventofcode.com/2024/day/12) | 12.204 ms | 10.00 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day12.jl) |

src/day09.jl

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,13 @@ function part1!(blocks::Vector{MVector{4,Int}})
3838
freeindex = blocks[i][1] + blocks[i][3]
3939
free = blocks[i][2] - blocks[i][3]
4040
maxtransfer = blocks[j][3]
41-
transfer = free >= maxtransfer ? maxtransfer : free
42-
data[freeindex:freeindex+transfer-1] .= blocks[j][4]
43-
data[blocks[j][1]+blocks[j][3]-transfer:blocks[j][1]+blocks[j][3]-1] .= -1
41+
transfer = min(free, maxtransfer)
42+
@views data[freeindex:freeindex+transfer-1] .= blocks[j][4]
43+
@views data[blocks[j][1]+blocks[j][3]-transfer:blocks[j][1]+blocks[j][3]-1] .= -1
4444
blocks[i][3] += transfer
4545
blocks[j][3] -= transfer
46-
if blocks[i][2] == blocks[i][3]
47-
i += 1
48-
end
49-
if blocks[j][3] == 0
50-
j -= 1
51-
end
46+
i += (blocks[i][2] == blocks[i][3])
47+
j -= (blocks[j][3] == 0)
5248
end
5349
return data |> checksum
5450
end
@@ -61,8 +57,8 @@ function part2!(blocks::Vector{MVector{4,Int}})
6157
for i 1:j-1
6258
free = blocks[i][2] - blocks[i][3]
6359
if free >= transfer
64-
data[blocks[i][1]+blocks[i][3]:blocks[i][1]+blocks[i][3]+transfer-1] .= blocks[j][4]
65-
data[blocks[j][1]:blocks[j][1]+transfer-1] .= -1
60+
@views data[blocks[i][1]+blocks[i][3]:blocks[i][1]+blocks[i][3]+transfer-1] .= blocks[j][4]
61+
@views data[blocks[j][1]:blocks[j][1]+transfer-1] .= -1
6662
blocks[i][3] += transfer
6763
blocks[j][3] -= transfer
6864
break
@@ -75,20 +71,17 @@ end
7571
function generate_disk(blocks::Vector{MVector{4,Int}})
7672
data = -1 * ones(Int, blocks[end][1] + blocks[end][2] - 1)
7773
for block blocks
78-
@inbounds data[block[1]:block[1]+block[3]-1] .= block[4]
74+
@views data[block[1]:block[1]+block[3]-1] .= block[4]
7975
end
8076
return data
8177
end
8278

8379
function checksum(data::Vector{Int})
84-
s, i = 0, 1
85-
@inbounds while i < length(data)
86-
if data[i] == -1
87-
i += 1
88-
continue
80+
s = 0
81+
@inbounds for i eachindex(data)
82+
if data[i] != -1
83+
s += data[i] * (i - 1)
8984
end
90-
s += data[i] * (i - 1)
91-
i += 1
9285
end
9386
return s
9487
end

0 commit comments

Comments
 (0)