60 lines
1.4 KiB
Plaintext
60 lines
1.4 KiB
Plaintext
func check[report: Array] : bool
|
|
increasing := report->nth(0) < report->nth(1)
|
|
|
|
for i in 0..report->size-1
|
|
if report->nth(i) > report->nth(i + 1) && increasing
|
|
return false
|
|
if report->nth(i) < report->nth(i + 1) && !increasing
|
|
return false
|
|
|
|
diff := (report->nth(i) as i64 - report->nth(i + 1) as i64)->abs()
|
|
if diff < 1 || diff > 3
|
|
return false
|
|
|
|
return true
|
|
|
|
func part1[data: Array] : void
|
|
out := 0
|
|
|
|
for i in 0..data->size
|
|
if check(data->nth(i))
|
|
out += 1
|
|
|
|
io.println_i64(out)
|
|
|
|
func part2[data: Array] : void
|
|
out := 0
|
|
|
|
for i in 0..data->size
|
|
if check(data->nth(i))
|
|
out += 1
|
|
else
|
|
arr : Array = data->nth(i)
|
|
for j in 0..arr->size
|
|
sliced := arr->slice(0, j)->concat(arr->slice(j + 1, arr->size - (j + 1)))
|
|
|
|
if check(sliced)
|
|
out += 1
|
|
break
|
|
|
|
io.println_i64(out)
|
|
|
|
func main[] : i64
|
|
~input, ok := io.read_text_file("input.txt")
|
|
if !ok
|
|
panic("failed to open input.txt")
|
|
|
|
lines := input->split("\n")
|
|
|
|
data := []
|
|
for i in 0..lines->size
|
|
line : str = lines->nth(i)
|
|
|
|
report := line->split(" ")
|
|
for i in 0..report->size
|
|
report->set(i, (report->nth(i) as str)->parse_i64())
|
|
data->push(report)
|
|
|
|
part1(data)
|
|
part2(data)
|