-
Notifications
You must be signed in to change notification settings - Fork 0
/
AoC2015_06.jl
70 lines (59 loc) · 1.82 KB
/
AoC2015_06.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#! /usr/bin/env julia
#
include("aoc_main.jl")
if abspath(PROGRAM_FILE) == @__FILE__
include("aocd.jl")
using .Aocd
end
module AoC2015_06
include("aoc.jl")
const REGEXP = r"([ a-z]+) ([0-9]+),([0-9]+) through ([0-9]+),([0-9]+)"
function solve(input, lights, f)
for line ∈ input
m = match(REGEXP, line)
start_x, end_x, start_y, end_y = [parse(Int, x) + 1
for x ∈ m.captures[2:5]]
command = m.captures[1]
f(lights, command, start_x:start_y, end_x:end_y)
end
return sum(lights)
end
function part1(input)
function process1(lights, command, start, _end)
if command == "turn on"
lights[start, _end] .= true
elseif command == "turn off"
lights[start, _end] .= false
else
lights[start, _end] .= .!lights[start, _end]
end
end
return solve(input, falses(1000, 1000), process1)
end
function part2(input)
function process2(lights, command, start, _end)
if command == "turn on"
lights[start, _end] .+= 1
elseif command == "turn off"
lights[start, _end] .-= 1
lights[lights .< 0] .= 0
else
lights[start, _end] .+= 2
end
end
return solve(input, zeros(Int, 1000, 1000), process2)
end
TEST1 = @aoc_splitlines("turn on 0,0 through 999,999")
TEST2 = @aoc_splitlines("toggle 0,0 through 999,0")
TEST3 = @aoc_splitlines("turn off 499,499 through 500,500")
TEST4 = @aoc_splitlines("turn on 0,0 through 0,0")
TEST5 = @aoc_splitlines("toggle 0,0 through 999,999")
function samples()
@assert part1(TEST1) == 1_000_000
@assert part1(TEST2) == 1000
@assert part1(TEST3) == 0
@assert part2(TEST4) == 1
@assert part2(TEST5) == 2_000_000
end
end # module AoC2015_06
aoc_main(@__FILE__, ARGS, 2015, 6)