@@ -38,17 +38,13 @@ function part1!(blocks::Vector{MVector{4,Int}})
38
38
freeindex = blocks[i][1 ] + blocks[i][3 ]
39
39
free = blocks[i][2 ] - blocks[i][3 ]
40
40
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
44
44
blocks[i][3 ] += transfer
45
45
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 )
52
48
end
53
49
return data |> checksum
54
50
end
@@ -61,8 +57,8 @@ function part2!(blocks::Vector{MVector{4,Int}})
61
57
for i ∈ 1 : j- 1
62
58
free = blocks[i][2 ] - blocks[i][3 ]
63
59
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
66
62
blocks[i][3 ] += transfer
67
63
blocks[j][3 ] -= transfer
68
64
break
75
71
function generate_disk (blocks:: Vector{MVector{4,Int}} )
76
72
data = - 1 * ones (Int, blocks[end ][1 ] + blocks[end ][2 ] - 1 )
77
73
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 ]
79
75
end
80
76
return data
81
77
end
82
78
83
79
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 )
89
84
end
90
- s += data[i] * (i - 1 )
91
- i += 1
92
85
end
93
86
return s
94
87
end
0 commit comments