Correctness.

From::from may never fail, so I added an invalid state.
This commit is contained in:
2018-12-18 12:12:29 +01:00
parent 1c19f4c514
commit 1e26ad1168
2 changed files with 25 additions and 22 deletions

View File

@@ -32,10 +32,10 @@ z
<defs>
<path d="M 0 0
L 0 3.5
" id="m73b8dde3be" style="stroke:#000000;stroke-width:0.8;"/>
" id="m7029ccf672" style="stroke:#000000;stroke-width:0.8;"/>
</defs>
<g>
<use style="stroke:#000000;stroke-width:0.8;" x="60.305455" xlink:href="#m73b8dde3be" y="307.584"/>
<use style="stroke:#000000;stroke-width:0.8;" x="60.305455" xlink:href="#m7029ccf672" y="307.584"/>
</g>
</g>
<g id="text_1">
@@ -71,7 +71,7 @@ z
<g id="xtick_2">
<g id="line2d_2">
<g>
<use style="stroke:#000000;stroke-width:0.8;" x="127.941818" xlink:href="#m73b8dde3be" y="307.584"/>
<use style="stroke:#000000;stroke-width:0.8;" x="127.941818" xlink:href="#m7029ccf672" y="307.584"/>
</g>
</g>
<g id="text_2">
@@ -110,7 +110,7 @@ z
<g id="xtick_3">
<g id="line2d_3">
<g>
<use style="stroke:#000000;stroke-width:0.8;" x="195.578182" xlink:href="#m73b8dde3be" y="307.584"/>
<use style="stroke:#000000;stroke-width:0.8;" x="195.578182" xlink:href="#m7029ccf672" y="307.584"/>
</g>
</g>
<g id="text_3">
@@ -139,7 +139,7 @@ z
<g id="xtick_4">
<g id="line2d_4">
<g>
<use style="stroke:#000000;stroke-width:0.8;" x="263.214545" xlink:href="#m73b8dde3be" y="307.584"/>
<use style="stroke:#000000;stroke-width:0.8;" x="263.214545" xlink:href="#m7029ccf672" y="307.584"/>
</g>
</g>
<g id="text_4">
@@ -153,7 +153,7 @@ z
<g id="xtick_5">
<g id="line2d_5">
<g>
<use style="stroke:#000000;stroke-width:0.8;" x="330.850909" xlink:href="#m73b8dde3be" y="307.584"/>
<use style="stroke:#000000;stroke-width:0.8;" x="330.850909" xlink:href="#m7029ccf672" y="307.584"/>
</g>
</g>
<g id="text_5">
@@ -193,7 +193,7 @@ z
<g id="xtick_6">
<g id="line2d_6">
<g>
<use style="stroke:#000000;stroke-width:0.8;" x="398.487273" xlink:href="#m73b8dde3be" y="307.584"/>
<use style="stroke:#000000;stroke-width:0.8;" x="398.487273" xlink:href="#m7029ccf672" y="307.584"/>
</g>
</g>
<g id="text_6">
@@ -286,10 +286,10 @@ z
<defs>
<path d="M 0 0
L -3.5 0
" id="mc9d31d9449" style="stroke:#000000;stroke-width:0.8;"/>
" id="m9772f4f0de" style="stroke:#000000;stroke-width:0.8;"/>
</defs>
<g>
<use style="stroke:#000000;stroke-width:0.8;" x="57.6" xlink:href="#mc9d31d9449" y="295.488"/>
<use style="stroke:#000000;stroke-width:0.8;" x="57.6" xlink:href="#m9772f4f0de" y="295.488"/>
</g>
</g>
<g id="text_8">
@@ -302,7 +302,7 @@ L -3.5 0
<g id="ytick_2">
<g id="line2d_8">
<g>
<use style="stroke:#000000;stroke-width:0.8;" x="57.6" xlink:href="#mc9d31d9449" y="247.488"/>
<use style="stroke:#000000;stroke-width:0.8;" x="57.6" xlink:href="#m9772f4f0de" y="247.488"/>
</g>
</g>
<g id="text_9">
@@ -316,7 +316,7 @@ L -3.5 0
<g id="ytick_3">
<g id="line2d_9">
<g>
<use style="stroke:#000000;stroke-width:0.8;" x="57.6" xlink:href="#mc9d31d9449" y="199.488"/>
<use style="stroke:#000000;stroke-width:0.8;" x="57.6" xlink:href="#m9772f4f0de" y="199.488"/>
</g>
</g>
<g id="text_10">
@@ -331,7 +331,7 @@ L -3.5 0
<g id="ytick_4">
<g id="line2d_10">
<g>
<use style="stroke:#000000;stroke-width:0.8;" x="57.6" xlink:href="#mc9d31d9449" y="151.488"/>
<use style="stroke:#000000;stroke-width:0.8;" x="57.6" xlink:href="#m9772f4f0de" y="151.488"/>
</g>
</g>
<g id="text_11">
@@ -346,7 +346,7 @@ L -3.5 0
<g id="ytick_5">
<g id="line2d_11">
<g>
<use style="stroke:#000000;stroke-width:0.8;" x="57.6" xlink:href="#mc9d31d9449" y="103.488"/>
<use style="stroke:#000000;stroke-width:0.8;" x="57.6" xlink:href="#m9772f4f0de" y="103.488"/>
</g>
</g>
<g id="text_12">
@@ -361,7 +361,7 @@ L -3.5 0
<g id="ytick_6">
<g id="line2d_12">
<g>
<use style="stroke:#000000;stroke-width:0.8;" x="57.6" xlink:href="#mc9d31d9449" y="55.488"/>
<use style="stroke:#000000;stroke-width:0.8;" x="57.6" xlink:href="#m9772f4f0de" y="55.488"/>
</g>
</g>
<g id="text_13">
@@ -577,7 +577,7 @@ z
</g>
</g>
<g id="line2d_13">
<path clip-path="url(#p01ec4ed421)" d="M 73.832727 282.048
<path clip-path="url(#p6b7ab71ead)" d="M 73.832727 282.048
L 87.36 277.248
L 100.887273 270.528
L 114.414545 269.568
@@ -605,7 +605,7 @@ L 398.487273 289.728
" style="fill:none;stroke:#1f77b4;stroke-linecap:square;stroke-width:1.5;"/>
</g>
<g id="line2d_14">
<path clip-path="url(#p01ec4ed421)" d="M 73.832727 295.488
<path clip-path="url(#p6b7ab71ead)" d="M 73.832727 295.488
L 87.36 293.568
L 100.887273 295.488
L 114.414545 295.488
@@ -633,7 +633,7 @@ L 398.487273 295.488
" style="fill:none;stroke:#ff7f0e;stroke-linecap:square;stroke-width:1.5;"/>
</g>
<g id="line2d_15">
<path clip-path="url(#p01ec4ed421)" d="M 73.832727 232.128
<path clip-path="url(#p6b7ab71ead)" d="M 73.832727 232.128
L 87.36 218.688
L 100.887273 208.128
L 114.414545 191.808
@@ -650,7 +650,7 @@ L 249.687273 205.248
L 263.214545 53.568
L 276.741818 81.408
L 290.269091 176.448
L 303.796364 146.688
L 303.796364 143.808
L 317.323636 277.248
L 330.850909 277.248
L 344.378182 277.248
@@ -870,7 +870,7 @@ L 348.72625 83.926688
</g>
</g>
<defs>
<clipPath id="p01ec4ed421">
<clipPath id="p6b7ab71ead">
<rect height="266.112" width="357.12" x="57.6" y="41.472"/>
</clipPath>
</defs>

Before

Width:  |  Height:  |  Size: 25 KiB

After

Width:  |  Height:  |  Size: 25 KiB

View File

@@ -12,10 +12,11 @@ enum Tile {
Tree,
Lumber,
Open,
Invalid,
}
impl Tile {
pub fn next(self, counts: &[usize; 3]) -> Self {
pub fn next(self, counts: &[usize; 4]) -> Self {
match self {
Tile::Open => {
if counts[Tile::Tree as usize] >= 3 {
@@ -38,6 +39,7 @@ impl Tile {
Tile::Open
}
}
Tile::Invalid => Tile::Invalid,
}
}
}
@@ -48,7 +50,7 @@ impl From<char> for Tile {
'|' => Tile::Tree,
'#' => Tile::Lumber,
'.' => Tile::Open,
_ => panic!("Invalid tile '{}'", c)
_ => Tile::Invalid
}
}
}
@@ -59,6 +61,7 @@ impl From<Tile> for char {
Tile::Tree => '|',
Tile::Lumber => '#',
Tile::Open => '.',
Tile::Invalid => 'X',
}
}
}
@@ -98,7 +101,7 @@ impl Day18 {
let ymax = if y < height - 1 { y + 1 } else { y };
for x in 0..self.width {
let mut counts = [0; 3];
let mut counts = [0; 4];
let xmin = if x > 0 { x - 1 } else { x };
let xmax = if x < width - 1 { x + 1 } else { x };