Compare commits

...

18 Commits

Author SHA1 Message Date
magical d84cac47a0 day 25 solution 2023-12-27 07:43:40 +00:00
magical 318adbd307 day 24 part 1 2023-12-24 05:56:48 +00:00
magical a940dff830 day 21 cleanup 2023-12-24 04:51:38 +00:00
magical c5d4f796e0 day 21 part 2 solve!
this is messy because i went in like three unsuccessful directions
before hitting on an approach that worked
2023-12-24 03:24:35 +00:00
magical d3d175a038 day 23 cleanup part 1 2023-12-23 08:36:14 +00:00
magical 7c2e376938 day 23 part 2 2023-12-23 07:30:37 +00:00
magical 219cde7d0a day 23 part 1 2023-12-23 06:04:49 +00:00
magical c291508b82 no turning back 2023-12-22 09:16:15 +00:00
magical dae0597628 day 20 part 2 answer 2023-12-22 09:11:41 +00:00
magical fd8de9c7c2 ignore more samples 2023-12-22 09:00:58 +00:00
magical 3dff6a2247 day 22 cleanup 2023-12-22 08:54:07 +00:00
magical efd7f887b8 day 22 part 2 2023-12-22 06:47:19 +00:00
magical 2635e69315 day 22 optimization 2023-12-22 06:27:36 +00:00
magical f805d9d373 day 22 part 1 2023-12-22 06:23:52 +00:00
magical 52a2184a3c day 21 part 1 2023-12-21 05:20:42 +00:00
magical f3b38a3ece day 20 part 2, sorta
finish this up later
2023-12-21 05:02:12 +00:00
magical 7178a11eee day 20 part 2 attempt
i don't think brute force is going to work
2023-12-20 06:03:08 +00:00
magical 9165dd7d86 day 20 part 1 2023-12-20 06:02:43 +00:00
22 changed files with 4263 additions and 1 deletions

15
.gitignore vendored
View File

@ -27,3 +27,18 @@
/day17/sample2.in
/day18/sample2.in
/day18/sample3.in
/day20/sample2.in
/day20/sample4.in
/day20/sample5.in
/day20/sample6.in
/day20/sample7.in
/day21/sample2.in
/day21/sample3.in
/day21/sample4.in
/day21/sample5.in
/day21/sample6.in
/day22/sample2.in
/day22/sample3.in
/day22/sample4.in
/day22/sample5.in
/day22/sample6.in

View File

@ -11,12 +11,22 @@ def solve(map,part=1):
goal = (len(map)-1,len(map[-1])-1)
def is_goal(x):
return x[:2] == goal
def heuristic(state):
# manhattan distance. this is correct because the cost
# at every point is >= 1 and we can only move one square
# at a time NSEW.
if part == 2 and state[3] < 4:
y,x,d,count = state
# account for forced moves away from the goal
if d[0] < 0 or d[1] < 0:
return (4-count)*2 + abs(y-goal[0]) + abs(x-goal[1])
return abs(state[0]-goal[0]) + abs(state[1]-goal[1])
def neighbors(state):
y,x,dir,count = state
next = []
def go(dx,dy):
if (-dx,-dy) == dir:
# can't turn around
# no turning back
return
newx = x + dx
newy = y + dy
@ -40,6 +50,7 @@ def solve(map,part=1):
go(0,+1)
return next
# turns out it's actually faster to not use the heuristic, so don't
cost, node, path = astar.search(start, is_goal, neighbors)
print(cost)
print([(x,y) for (y,x,_,_) in path])

58
day20/input 100644
View File

@ -0,0 +1,58 @@
%jx -> rt, rs
&cc -> cd, fc, qr, nl, gk, zr
%qs -> cl, rs
%zr -> cq
%mx -> nr, pm
%mj -> qr, cc
%cj -> cc, nt
%jv -> sp
%dj -> bd, zc
%kt -> lt
broadcaster -> gz, xg, cd, sg
&dn -> rx
%br -> nf, bd
%cd -> cc, nl
%zc -> jq, bd
%xg -> cf, pm
%nz -> gm, bd
&dd -> dn
%nb -> sl
&pm -> kt, xg, xp, jv, sp
&fh -> dn
%rt -> qq
%qq -> rs, hd
%hd -> qs, rs
&xp -> dn
%pj -> cc, mj
%gz -> bd, kb
%zd -> jv, pm
%cq -> cj, cc
%qr -> gk
%ng -> jk, bd
%kb -> bd, sv
%cl -> zx, rs
%gj -> zd, pm
%sl -> kx
%sv -> br
%nf -> bd, nz
%zx -> rs
%nt -> mn, cc
%rh -> nb, rs
%gk -> ln
&bd -> gm, gz, fh, sv
%jq -> ng, bd
%sp -> pc
%sg -> rs, rh
%kx -> jx
&fc -> dn
%cf -> gj, pm
%pc -> kt, pm
%jk -> bd
%vf -> pm
&rs -> sg, dd, sl, kx, nb, rt
%nr -> vf, pm
%ln -> zr, cc
%lt -> pm, mx
%gm -> dj
%nl -> pj
%mn -> cc

5
day20/sample1.in 100644
View File

@ -0,0 +1,5 @@
broadcaster -&gt; a, b, c
%a -&gt; b
%b -&gt; c
%c -&gt; inv
&amp;inv -&gt; a

5
day20/sample3.in 100644
View File

@ -0,0 +1,5 @@
broadcaster -> a
%a -> inv, con
&amp;inv -> b
%b -> con
&amp;con -> output

153
day20/sol.tcl 100755
View File

@ -0,0 +1,153 @@
#!/usr/bin/env tclsh
source ../prelude.tcl
proc read-input input {
global next
global type
global flip
global conj
global when
while {[gets $input line] >= 0} {
# oops
set line [replace $line "&gt;" > "&amp;" &]
#puts $line
must_regexp {^([%&]?)([a-z]+) -> (.*)$} $line _ sigil name to
set next($name) [replace $to "," ""]
if {$sigil eq ""} {
set type($name) ""
} elseif {$sigil eq "%"} {
set type($name) "flip"
set flip($name) 0
} elseif {$sigil eq "&"} {
set type($name) "conj"
set conj($name) [dict create]
set when($name) [dict create]
} else {
error "unknown sigil $sigil"
}
}
foreach n [array names type] {
foreach to $next($n) {
if {![info exists type($to)]} {
set type($to) ""
}
if {$type($to) eq "conj"} {
dict set conj($to) $n 0
dict set when($to) $n 0
}
}
}
}
proc pulse {init i {debug 0}} {
global next
global type
global flip
global conj
global count
global when
set n $init
set pulses {}
#puts [array get next]
incr count(0) ;# button
foreach to $next($init) {
#incr count(0)
lappend pulses $init $to 0
}
while {[llen $pulses]} {
if {$debug} {
puts "#[llen $pulses] : $pulses"
}
set copy $pulses
set pulses {}
foreach {from n value} $copy {
incr count($value)
if {$n eq "rx" && $value == 0} {
global solved
set solved 1
}
switch $type($n) {
flip {
if {$value == 0} {
set flip($n) [expr {!$flip($n)}]
foreach to $next($n) {
lappend pulses $n $to $flip($n)
}
}
}
conj {
dict set conj($n) $from $value
# remember the last time we saw a high bit from each input
if {$value} {
dict set when($n) $from $i
}
set all 1
foreach v [dict values $conj($n)] {
if {!$v} {
set all 0
break
}
}
set send [expr {!$all}]
foreach to $next($n) {
lappend pulses $n $to $send
}
}
}
}
}
#puts $count(0)
#puts $count(1)
}
proc solve input {
read-input $input
global count
#puts [array get next]
#array set count {0 0 1 0}
set count(0) 0
set count(1) 0
global when
for {set i 1} {$i <= 1000} {incr i} {
pulse broadcaster $i
}
puts "$count(0)"
puts $count(1)
puts [expr {$count(0) * $count(1)}]
global type
if {![info exists type(rx)]} return
set last $when(dn)
while {1} {
pulse broadcaster $i
if {$when(dn) ne $last} {
# rx is connected to a conj of 4 circuits
# which each emit 1 high pulse on a periodic cycle.
#
# this could be more difficult but it turns out
# that the step when the first output is produced
# is also equal to the cycle length,
# and all the cycles are a prime number so we don't
# even need to do an lcm
set a [lmul [dict values $when(dn)]]
if {$a != 0} {
puts $a
break
}
#puts $when(dn)
set last $when(dn)
}
incr i
}
}
solve stdin

131
day21/input 100644
View File

@ -0,0 +1,131 @@
...................................................................................................................................
..#...#....#.......#.#..##......#...............#..#....#......................................#....##.........#...##..#.#.....#...
.....#..#............#....#.#...##..#.................#..#..................#...#.#.......#.#..#....#.....#...#.#..............#...
.#........#...........#......................#..........#................#...#..............#......#...........#..#.....#.#........
..#.#.#..........#.......#....#....#......#.......#...#..................#.###...........#...........##...#..........#.##.#.#.#.#..
.#.#.........#.....#.#...........#.......##.###.....#.#.........#.#.........##.................##.........##.#.............##.#....
...#.#...#.#...........#.#.####....................##.#........##..........#......#.............##.#...............#...............
......##...#.##....#.......#....#...#....#..................................#......#.#.#..#.........#..........#..#...#............
.#..#..#...#........................#......#...##.................#...............#..........##.........#..........................
......#...#..#...#................#....#.....................#..................#...#.............#..#..#.......##.#.....#.........
.#........#..........#...#........#...........##..............#...#..#.................#............................#.....#....#...
...............##..####...........###..#...##.#................#.......#.................#......#........................#......#..
....#............#.........#...........#..#.#.#..........#..........#...............#....#....###...##.........#...................
.#.....#..#....#.#..............#..#.....#.#........................................##..#.....#....#...#..##.##...#................
.....#.....#....#..#......#...............#....#.......#..#..............#...................#............................#........
................#........#................#.#.........................#.....#.......#.....#.................#.....#.........#......
......##....#..#...#....#..#............#....#...........#..##............#..#.........#.......#...#.#..........#.....#...#....#...
....#.#...#.........#..#....#...#......#................#..##.......##.................#..#....#..........#......#.................
..........#..#..#..#..........#........................#.................###..............##.#..#.#......#...#.##...........#......
.###...#.......#..................#....#...............#.......#..#..#...#..#.......................#.#...#..#........#...#......#.
.......#...........#.#..##......#................#............#.#.#......#................#.........#.....#..........#..#..#...#...
....#.....................##....#......##...................#...............#.....#...............#..#........#.#....##.....#..#...
.......#..#.##..#...###...#........#.............##........#......................##.........#...............#....#.#..............
..###.......#..#......#........#...#...............###.###.........#.#.#........#...........#.........#...........#....#....#.#....
.....#..........#......#...#.........#........#........#...#...........#.....#.#.............................#.#.......###.##.#.##.
..#.................#.............#................#.#............#.#.....#........................#........#.................#....
.#..##..#.#......##..........................#..#.........#.....#......#.....#.....#............................###.....##....#..#.
.......#.#.......#............#...........#....#.......#....#...............#.#..##..##.........#....#..............#....###...#...
...#.......#..##..#..#......................#......##......#........#...#........##.##.#..................#...#.#....##..##......#.
...#.......#.........#.........................#...................#......#.......#...#...#.......#............#........###........
.#.#.#...#.#........##..................#...#.#.........................#......#.....#..#.#........##............#...........#.....
.#.#...#.....###.#........##...........#..#...##.........#...###..#.#...#....##........#...#.............#...#........##...#.....#.
..............#..........#...#........#.......##..##........................#................#........##.........#.................
....##.................#..................................#.............##..#.#....#...#..#...................#.....#....#.#.#.....
....#####...##...#..##..#..#.......###.....#.#......#..#..#.##.....#..#.................#..........................#.#.##.#........
..........#..#....##.#.................#......#...#.#.......#......##.........#..#.#.....................................#..#......
..##..##.....#.....#....#...........#.#..........#............#.......###....................#..#..........#....#............#...#.
......#..........#.......................#...#......#...#...#..#....#..#.....#...........#..#....#............#.....#....#.#.......
.#..#......#...#..##................##..#...#.........#.........#.#..#..................#..........................#...#..#.##.....
..#......#..#..................................#.....#.###.#.........#......#.#...#................#..........#....#...............
........#..##....#...................#........#....#...#..........#...#...#..........####.....#..##.#................#.........###.
....#..............................#..#..#..#.##......#...#...................#.##..........#..#...#..................#....###.##..
.#..........................#.#..#...........#..#......#..#..#........................#........................##...#.#..#......#..
............................#..#...#..................#................#..............##.#.#.#..................#...........##.#...
.........#..###............#.....##...#..#...#.##.....##....#.....#.#.......#.#...#........##.........#.#..............#.#.....#...
...........#............#.........#.....##...##......#.........#...##...............#.##................#............#.....#...#...
.#####.#..#...............................#..#...............#...................#.....#.#............#................#...#.......
.#..#...#....#....................#...........#.................................................#......##................#....##.#.
....#......#..........#..##...#..#.##....#...#.....#.#........#...#.....#.....#......#...#..#..#..#........................#....#..
...#.....#..........##......#..#.....##...#...#...#.#..........#..............#.....#..........#..#.....###.................###....
.....................##...#....#....#......#...#.#.......#...........#..........#..............#.......##.................#..#.....
..##.................#.......#...........#.##........#..##.#...........#.#....#.#.......#..##....#.#..#....#....#..................
.....#...........#.#........#..........#..#.##.....#............#.......#..#.....##....#.#...........##.......#............#.......
.....##.#.............##........##...........#..#.#..##.#.......#.......#..................#.#..#..#.......#.................#.#...
.#.#..............##....#.......#......#..#........#..................#.#.##..#...#....#.#...........##.....#..#..#.........##.....
..............##................#....#....#........#.......#...##...#.#.#........#.#...#...#..#.#.......#.#...#....................
.............####.....#..........................#.#..................................#........#.#..##...#...#..#..................
.#......................#.....#####.#...........#.#............#.............##....#....#.#...........##..........##...........##..
...........#.#.#..#...##...##........#.......#.......#.#........#.......#......#............#..###....#.......#..#.....#........#..
............#....#...............#.##............#...##....#.#...........#........#...#...##...........#..........#................
......................#............#..##......#......#....#.......#..##......#...#....#....#....#..#..#..........#.....#.#.........
................#.....##.......#....##..#.......#......###......#.......#....#.......#....##...........#.#....#....##....#.........
................#..#...#.##...#...#....#.........#....#...##......#.#...#....#.............#.....#............##...#...............
......................#....#......#.#....##...#...#...#.#.......#.....#..#.......#...#.#.##..#........#.....#....#.........#.......
.....##..#..#......................#.#........#....#......#....#..#....#.........#..#.......#.......#.......#.#.......#.###..#.....
.................................................................S.................................................................
..................#...#.....#....#...........#..#.#..........#............................#....................##.....#....#.......
.........#....#......#..#......#...#.#......#...#...#..............#....##.##........#..........##...##...#.......##.#....#........
................#.##.#..#..............#.....#..#......#....#............#.#...###..#......#......#....#........#..##..............
.........#.....................................#...#.#.............#.....#...........#........###.....................#.#..........
.........#....#...........#...##..#........#..........#.#.....#.................#.#.#.##..#...#.#................#....#............
...................#...................#...#..............#.....#.#.......#..............#......#..........###.......#..#..........
...........#.#.............##...#...#....#.#.....#....................#.....#..#.....#........##.#....#....#...................##..
.#................#.#.#.##.............#..#...#....#.#..####.#.......##......................#.......#....#....##................#.
...##..............###...#.#..#..#.#...................#....#...#..#.#....##.....#.....#........#...#...#.#.#...................##.
................................##.#..........#........#..........#.#....#.........##.......##........#............................
...#............##......#.........#.....#.#................##......#.......#....##...#....#.....#.#..#....#.#.....#.............#..
......#.........#.....###.##...#..#...#..#.##....#.#.....#.#.#........##...#..#..........#.............#...................#.......
.#....#............#.....#..##....##...##.....#.#......##..#........#..........#.................#.......#......#.............#....
...#.....#........#......#...........#..#......#.#.#...#....#.###.....##...##..#...#......#..#..........#..#..#..........#.....#...
..#.#..#...........#.###...#.......#..........#...#.........#.#....#......#.........#...#....#...#.##....#....#..........##....#...
......#.#...#............##.......#.......##...............#..................#.#....#..#..#..#..#...#...#...#.....................
....#.......................#........##..#........##.....#.............#........#.#...........#.....#....##...........#..#.........
...#.......#..#.......#......#....#......#..........#.#.#.#.......##....#....................##.........#................#..#......
..#....................###.......##.#.....#.....#.#............##..#....#..#.................#...#..##.###............#...#..##....
.....#......#..#...............#.....##......#.....#.......#..#.#..#....#..##.................##....#....##................#....#..
.............##..#.........#..#..#........#..#..........#...#................###..........##.#.#.........#........#....##..#..#....
.#.#.#.......#..............#....#...#............#.......#....#.........#........#....#............#............#...#.#..#........
.............#.....#.........................#....#.#...........#.................#.#..#....#..........................#.##...#....
...#......#........#.............#..................#.###....................#.#.....#........#.....#.........#.#.#.....#.#........
.#.............#...##...........#.....#.....#.............#.........#...............###....#...#..#.#............#.#.............#.
............##..#................#....#.......#............##.........##..................#......................#.#..#....#.......
............#......#.....................#.#.#.#..#.#..#......#.............#.#....##.....#....#...............#.#............#....
..#..#.#.#.#....#......#............##..............#....##.........#.......#.....#...##.....#....................#.......#.....#..
...#...........#.......#...................#......#...#.##....##....#..#.#...##..#..........#...##............#..#........#.....#..
.....#....#..#..##................#...#...#....#.#....#.#.............#...#..............#.##.............#..#..#....#.#...#...#.#.
.......#..#.........#.....#..........#......#.#...#.........#.#.....#...#..........#....................#....#..........#..........
..#.#.#.....#.........#..###...........#.##.#.......#..#..........#.#.......#......#......#.#.........#.#....#..#..#.......#....#..
.#.........#...#......#...............#......#.#.....#.....................#.......#.#......#..................#...............##..
..#....#..............................#.....#.##.....#........#....#......#....#....#..#....#........###..#..#..........#....#..#..
....#.......##........##..............................#.........#...###.#........#...#.##.............##.....#.#......#..........#.
...#.#.##.....#........#.................#...#..#........#............#..#.#........#....................#.....#........#..........
.........#....#.........#.......##...........#.#.#.#..............#......#..#.......................#.#......................#.....
............#........#..##..#................#..#.............#.............####....#..#.........#............................#....
....#......#..##...#...#.#...................#.....#.....#.........#.............#...............#...............###.......#.#.....
.#.....#..........................#...........#...............#............##......#.#..........#.......#..........................
...##.....................#....#...#.........#...............#..#......##.#...#.#..................#.......#.......................
...#........#....#....................#............#.#...#...............#................................##..........##....#.#....
.#...##.....#........####....#......#.#......................#.............#..#....#.......#...........#..#.........#..............
......#..............#...........#..##.....................#.#.......#.#..###.............#......#.......#..#...#...............#..
...#....#...................##...................#.#.#.#...##.....#.#.......#.#..........#............#..............##.......#....
...........#.................##.........#..............#.......##..##..#.#................##.#.#..#.#.#.............#..............
........#..##.....#.#.......#.........................#....##.........###.....##........#..........#..#...#............#...#....#..
...................#.#.#......#..#.........#............#..#........#........#............#....#...........#.......................
.......#..........#.............#.#.#..#....#...............#..............#...........##..#...#..................#..........##..#.
..............##....#....#..#.....#..........#.........#.#...#.....###..#.............#...................#..#.#.#....##..#....#...
...##..........#.....#.#.#...##...#...#........#............#........#.#..............#.....#........#..##...........#.#........#..
.#..#.#....#..##.##....##.#....##....#...#...#..........#..##.......#...................#.......##..........#........#...#.#....#..
.#..#....#........#......#...............###.............#..........#...#...............#........#...#...###....#..#.#.........#...
.....#.........#.#..#....#...#.....#......#.................#...#....#...............#.........#.........#...........#.#...........
..##.....#..........#...#..#.....#....#.#.....................#...#.................#.....#..#...#.#................#.........##.#.
.........#..#.##........#...####......#.......#................#...#............#..#.#...........#....#.....#.#....#...###.........
.........#......#...#...#....#...#.....##..#..##.....#..............#........#........#.........#...........#.....#......#.#..#....
...##.......#.....#....#.............#.##...#.#....................................#.#...#...............#...................#.....
......................#.###.............#........#.................................#.##........#..#.#.............##...#...........
...#........#....#.....#.#...........#.#.......#....#.............#................#.....#..#..#..#.#.......#...#.....#.....##.....
.##.#....#.........#........##......#................##............................#...#..................#...##............##.....
......#.......#........#..#.#...#.........#............#.#........................#..#....#.#...##.........#...#..........##.......
............#.#...#.....#..#..........#.....#..........................###.#.......##..#......#...#......#.........##...###.....#..
.#.#......#.#..............#............#..........#.#....................#......#..#...#....#..#.#......#...............#.#.......
...................................................................................................................................

11
day21/sample1.in 100644
View File

@ -0,0 +1,11 @@
...........
.....###.#.
.###.##..#.
..#.#...#..
....#.#....
.##..S####.
.##..#...#.
.......##..
.##.#.####.
.##..##.##.
...........

191
day21/sol.py 100644
View File

@ -0,0 +1,191 @@
import sys
import numpy
def read_map(input):
map = [x.strip() for x in input]
if len(map) < 80:
print(map)
Y = len(map)
X = len(map[0])
assert all(len(row) == X for row in map)
assert X == Y
return map
def draw(mask, fill):
if len(fill) < 50:
for i, row in enumerate(fill):
print("".join(".O#!"[f + 2*mask[i,j]] for j,f in enumerate(row)))
print()
def solve(map):
Y = len(map)
X = len(map[0])
fill = numpy.zeros((Y,X+1), dtype='uint8')
mask = numpy.zeros((Y,X+1), dtype='uint8')
for i, row in enumerate(map):
for j, c in enumerate(row):
if c == '#':
mask[i,j] = 1
if c == 'S':
start = (i,j)
# add a border on the side of the map (to prevent wraparound)
mask[:, -1] = 1
# start at the start
fill[start] = 1
for i in range(64):
fill = step(mask, fill)
#draw(mask, fill)
if i == 6-1: # sample
print(fill.sum())
print("part1 = ", fill.sum())
def solve2(map):
Y = len(map)
X = len(map[0])
fill = numpy.zeros((Y,X), dtype='uint8')
mask = numpy.zeros((Y,X), dtype='uint8')
for i, row in enumerate(map):
for j, c in enumerate(row):
if c == '#':
mask[i,j] = 1
if c == 'S':
start = (i,j)
print(fill.shape, mask.shape)
fill[start] = 1
values, period, d2 = simulate(fill, mask, max_steps=500)
for n in 6,10,50,100,500,1000, 5000, 26501365:
print(n, extrapolate(n, values, period, d2))
def simulate(fill, mask, max_steps):
Y,X = mask.shape
assert Y == X, "need a square matrix"
period = X
tiles = 1
original_mask = mask
prev = [0]
diffs = []
for i in range(max_steps):
# expand the map if necessary
if fill[0].any() or fill[-1].any() or fill[:,0].any() or fill[:,-1].any():
draw(mask,fill)
print("expanding...")
tiles += 2
mask = numpy.tile(original_mask, (tiles,tiles))
fill = numpy.pad(fill, [(Y,Y), (X,X)])
# take one more step and count the number of reachable squares
fill = step(mask, fill)
n = int(fill.sum())
# look for patterns
# although the number of squares is somewhat unpreditable from step
# to step (due to the maze-like structure of the mask), the fact that
# the mask repeats in tiles (and the fact that there are unobstructed
# pathways in the orthogonal and diagonal directions) means that, in
# the long run, the flood fill will even out and -importantly- it should
# have some recognizable pattern every N steps (where N is the period
# of the tiling - 11 in the sample, 131 in the input). this is because
# we enter new tiles every N steps, and although it's hard to predict
# exactly how many of the squares in each tile we'll have visited,
# it should be the same number in every tile (or rather, there should
# be some small set of repeated tile-states, and we should be able to
# predict how many of each tile-state there will be).
#
# SO the first step is to get the difference between the current
# number of reachable squares and the number N steps ago.
#
# d1(i) = f(i) - f(i - N)
#
# we then have to discover some pattern in that sequence.
# we know the number of reachable squares will grow roughly as the
# square of the number of steps (because the map is 2D) so
# we should be looking for a quadratic relation.
# we can use second-order differences (see day 9) to do that.
# d1 is already a first-order difference, so take the difference
# between d1s to get a second-order difference. if our assumption is
# correct, then d1 should be a linear sequence and d2 should be a
# constant.
#
# d2(i) = d1(i) - d1(i-N)
# = (f(i) - f(i-N)) - (f(i-N) - f(i-N-N))
#
#
# there may be some unstability at the beginning of the simulation
# so we need to wait until the d2 values for every step in the period
# all agree.
#
# once it settles down, this gives us a set of N (11, 131, whatever)
# equations we can use to predict the number of squares after any
# future number of steps
#
d2 = 0
if len(prev) >= 2*period:
# find the second differece
d2 = (n - prev[-period]) - (prev[-period] - prev[-2*period])
diffs.append(d2)
prev.append(n)
print(i, n, d2, sep="\t", flush=True)
if len(diffs) > period and all_same_value(diffs[-period:]):
print("gotcha!")
return prev, period, diffs[-1]
assert False, "failed to find a stable pattern"
return prev, period, None
def all_same_value(list):
if len(list) < 1:
return False
x = list[0]
return all(x == y for y in list)
def extrapolate(n, values, period, d2):
if n < len(values):
return values[n]
quo, rem = divmod(n-len(values)+period, period)
x = len(values)-period+rem
y = values[-period+rem]
d1 = y - values[-2*period+rem]
while x < n:
d1 += d2
y += d1
x += period
assert x == n
return y
def step(mask, old):
# flood fill
#draw(fill)
fill = numpy.zeros(old.shape, dtype='uint8')
y,x = fill.shape
for i in range(y):
f = (old[i] == 1)
f = numpy.roll(f, 1) | numpy.roll(f, -1)
if i > 0: f |= (old[i-1] == 1)
if i < y-1: f |= (old[i+1] == 1)
f &= (mask[i] == 0)
#print(old, f)
if f.any():
fill[i, f] = 1
assert fill.any()
return fill
map = read_map(sys.stdin)
solve(map)
solve2(map)

1370
day22/input 100644

File diff suppressed because it is too large Load Diff

7
day22/sample1.in 100644
View File

@ -0,0 +1,7 @@
1,0,1~1,2,1
0,0,2~2,0,2
0,2,3~2,2,3
0,0,4~0,2,4
2,0,5~2,2,5
0,1,6~2,1,6
1,1,8~1,1,9

154
day22/sol.py 100644
View File

@ -0,0 +1,154 @@
def overlaps(brick1, brick2):
"""reports whether two bricks overlap with each other"""
# the bricks as a whole overlap if there is any overlap
# on all three axes
for (a,b),(x,y) in zip(brick1, brick2):
if (b < x or a > y):
# ranges don't overlap
return False
return True
assert overlaps([(0,0), (1,2), (2,2)], [(0,0),(1,1),(2,3)])
assert not overlaps([(0,0), (1,1), (2,2)], [(1,1),(1,1),(2,2)])
def read_input(f):
data = []
for line in f:
start, end = line.split("~")
start = [int(x) for x in start.split(',')]
end = [int(x) for x in end .split(',')]
brick = [tuple(sorted([a,b])) for a,b in zip(start,end)]
data.append(brick)
return data
def check(data):
print(data)
for x in data:
for y in data:
if x == y:
assert overlaps(x,y)
else:
assert not overlaps(x,y)
print("ok")
def solve(f):
data = read_input(f)
check(data)
settle(data)
check(data)
disintegrate(data)
cascade(data)
def settle(data):
def zindex(b):
return b[2]
data.sort(key=zindex)
top = 1
for i in range(len(data)):
# first, lower to just above the top of the highest block
b = data[i]
z = b[2][0]
if z > top:
#print("lowering block %d by %d" % (i, z-top))
b = lower(b, z-top)
# lower block one at a time
n = 0
under = data[:i]
while canlower(b, under):
b = lower(b)
n += 1
print("lowering block %d by %d+%d" % (i,z-top,n))
data[i] = b
top = max(top, b[2][1] + 1)
# FIXME: should probably re-sort the data here,
# but it seems to work even if we don't
def canlower(block, under):
if block[2][0] <= 1:
return False
x = lower(block)
for u in reversed(under):
if overlaps(u, x):
return False
return True
def disintegrate(data):
print(*data, sep="\n")
t = 0
#for i,x in enumerate(data):
# assert not canlower(x,data[:i])
for i,x in enumerate(data):
# disintegrate x
# see if any block above x can be lowered
# if yes, then x cannot be disintegrated
candisintegrate = True
for j in range(i+1, len(data)):
if canlower(data[j], data[:i] + data[i+1:j]):
candisintegrate = False
break
print(i, x, candisintegrate)
if candisintegrate:
t += 1
print(t)
return (t)
def cascade(data):
print(*data, sep="\n")
t = 0
#for i,x in enumerate(data):
# assert not canlower(x,data[:i])
for i,x in enumerate(data):
below = data[:i]
upper = data[i+1:]
moved = 0
if below:
top = max(z[1] for x,y,z in below) + 1
else:
top = 1
for b in upper:
# lower blocks one at a time
z = b[2][0]
if z > top:
b = lower(b, z-top)
while canlower(b, below):
b = lower(b)
if b[2][0] < z:
moved += 1
#print("lowering block %d by %d" % (i,n))
below.append(b)
top = max(top, b[2][1]+1)
print(i, x, "caused %d blocks to move" % moved)
t += moved
print(t)
return (t)
def lower(brick, n=1):
"""lower a brick n spaces in the z direction"""
x, y, z = brick
if z[0] > n:
z = z[0]-n, z[1]-n
return [x, y, z]
import sys
solve(sys.stdin)

141
day23/input 100644
View File

@ -0,0 +1,141 @@
#.###########################################################################################################################################
#.#...###...#...#...#...#.....###.....#...###...#...#...#.....#...#.......#...#...........#.......#.....#...#.....###.........#.....###...###
#.#.#.###.#.#.#.#.#.#.#.#.###.###.###.#.#.###.#.#.#.#.#.#.###.#.#.#.#####.#.#.#.#########.#.#####.#.###.#.#.#.###.###.#######.#.###.###.#.###
#...#.#...#.#.#.#.#.#.#.#...#.###.#...#.#...#.#.#.#.#.#.#...#.#.#.#.#.....#.#.#.......#...#.#.....#...#...#.#...#.....#...#...#.#...#...#...#
#####.#.###.#.#.#.#.#.#.###.#.###.#.###.###.#.#.#.#.#.#.###.#.#.#.#.#.#####.#.#######.#.###.#.#######.#####.###.#######.#.#.###.#.###.#####.#
#.....#...#.#.#...#.#.#...#.#.>.>.#...#.#...#.#...#.#.#.#...#.#.#...#...###.#.#...#...#.....#...#...#.#.....###.....#...#...###.#...#...#...#
#.#######.#.#.#####.#.###.#.###v#####.#.#.###.#####.#.#.#.###.#.#######.###.#.#.#.#.###########.#.#.#.#.###########.#.#########.###.###.#.###
#.#...#...#.#.....#.#.#...#.#...#...#...#...#.#...#.#.#...#...#.......#...#.#...#.#.....#.......#.#.#.#.........###.#.#...#...#.#...#...#...#
#.#.#.#.###v#####.#.#.#.###.#.###.#.#######.#.#.#.#.#.#####.#########.###.#.#####.#####.#.#######.#.#.#########.###.#.#.#.#.#.#.#.###.#####.#
#.#.#.#...#.>.....#...#...#.#.....#.#...#...#.#.#...#.....#.###...###.#...#...#...#...#.#.#...###.#.#.#.........#...#...#.#.#...#.###.#.....#
#.#.#.###.#v#############.#.#######.#.#.#.###.#.#########.#.###.#.###.#.#####.#.###.#.#.#.#.#.###.#.#.#.#########.#######.#.#####.###.#.#####
#.#.#.#...#.......#.....#.#.###...#...#.#...#.#...#####...#...#.#.#...#.#.>.>.#...#.#.#.#...#.#...#.#.#...>.>.#...#...#...#.#.....#...#.....#
#.#.#.#.#########.#.###.#.#.###.#.#####.###.#.###.#####.#####.#.#.#.###.#.#v#####.#.#.#.#####.#.###.#.#####v#.#.###.#.#.###.#.#####.#######.#
#.#.#...#####.....#.#...#...#...#.......###.#.#...#.>.>.###...#.#.#...#...#...#...#.#.#.....#.#.###...#.....#...#...#...###.#...###.#.......#
#.#.#########.#####.#.#######.#############.#.#.###.#v#####.###.#.###.#######.#.###.#.#####.#.#.#######.#########.#########.###.###.#.#######
#.#...#.......#...#.#.....#...#...#...#...#.#.#.....#.....#.....#...#.#.......#.....#...#...#.#.......#.#...###...#...###...#...#...#.......#
#.###.#.#######.#.#.#####.#.###.#.#.#.#.#.#.#.###########.#########.#.#.###############.#.###.#######.#.#.#.###.###.#.###.###.###.#########.#
#...#.#.#.....#.#...#.....#.....#...#...#.#...#.....#.....#...#####.#.#...#...###...###...###.....#...#...#...#.#...#.#...#...#...#...#...#.#
###.#.#.#.###.#.#####.###################.#####.###.#.#####.#.#####.#.###.#.#.###.#.#############.#.#########.#.#.###.#.###.###.###.#.#.#.#.#
###...#.#.###...###...#.........#.........#...#...#.#.......#.....#...###...#...#.#.#.......#...#...#...###...#.#...#.#.#...#...#...#...#...#
#######.#.#########.###.#######.#.#########.#.###.#.#############.#############.#.#.#.#####.#.#.#####.#.###.###.###.#.#.#.###.###.###########
#.......#.#.........#...#...#...#.......###.#.#...#...............#...#.........#.#.#.#.....#.#.......#.....###.....#...#.....#...#...#.....#
#.#######.#.#########.###.#.#.#########.###.#.#.###################.#.#.#########.#.#.#.#####.#################################.###.#.#.###.#
#.........#...........#...#...#.........#...#.#...#...............#.#...#.....###.#...#.....#.....................#.............###.#.#.#...#
#######################.#######.#########.###.###.#.#############.#.#####.###.###.#########.#####################.#.###############.#.#.#.###
#.........###...........#.....#...........#...###...#.........#...#.#...#.###.....#.........#...#.................#.........###...#.#.#.#.###
#.#######.###.###########.###.#############.#########.#######.#.###.#.#.#.#########.#########.#.#.#########################.###.#.#.#.#.#.###
#.......#.....#.....#...#.#...###.........#.#.......#.......#...###...#...#...#...#.......#...#.#.......#.....###.....#...#.....#...#...#...#
#######.#######.###.#.#.#.#.#####.#######.#.#.#####.#######.###############.#.#.#.#######.#.###.#######.#.###.###.###.#.#.#################.#
#.......#.......###...#.#.#.#...#.......#...#.....#.#.....#.#.......#.......#...#.........#...#...#...#...#...#...#...#.#.#.......#...#.....#
#.#######.#############.#.#.#.#.#######.#########.#.#v###.#.#.#####.#.#######################.###.#.#.#####.###.###.###.#.#.#####.#.#.#.#####
#.........#...#####.....#.#.#.#.#...#...#.....#...#.>.>.#...#...#...#.......#.............###.#...#.#.......###...#.###.#.#.#...#...#...#...#
###########.#.#####.#####.#.#.#.#.#.#.###.###.#.#####v#.#######.#.#########.#.###########.###.#.###.#############.#.###.#.#v#.#.#########.#.#
#...........#...#...#...#.#.#.#.#.#.#...#...#.#.#.....#...#...#.#.#...#...#...#...........#...#...#...........###.#.#...#.>.#.#...#.....#.#.#
#.#############.#.###.#.#.#.#.#.#.#.###v###.#.#.#.#######.#.#.#.#.#.#.#.#.#####.###########.#####.###########.###.#.#.#####v#.###.#.###.#.#.#
#...........#...#...#.#.#.#.#.#...#...>.>.#.#.#.#.......#...#.#.#.#.#.#.#.#...#.......#...#...#...#...#...#...#...#...#.....#.#...#.#...#.#.#
###########.#.#####v#.#.#.#.#.#########v#.#.#.#.#######.#####.#.#.#.#.#.#.#.#.#######.#.#.###.#.###.#.#.#.#v###.#######.#####.#.###.#.###.#.#
###...#...#.#.#...#.>.#.#.#...#.........#...#...#######.#.....#.#.#.#...#.#.#...#...#.#.#.....#.#...#.#.#.>.>.#.......#.....#.#...#.#.###.#.#
###.#.#.#.#.#.#.#.#v###.#.#####.#######################.#.#####.#.#.#####.#.###.#.#.#.#.#######.#.###.#.###v#.#######.#####.#.###.#.#.###.#.#
#...#...#...#...#...#...#...###.............###.........#.#####.#.#.#.....#...#.#.#.#.#.....###...#...#.###.#.........#.....#.#...#.#.....#.#
#.###################.#####.###############.###.#########.#####.#.#.#.#######.#.#.#.#v#####.#######.###.###.###########.#####.#.###.#######.#
#.#...#...#...#.....#...#...#...........#...#...#.......#...#...#.#.#.#.....#.#.#.#.>.>...#...#...#...#...#...........#.#...#.#...#.#.......#
#.#.#.#.#.#.#.#.###.###.#.###.#########.#.###.###.#####.###.#.###.#.#.#.###.#.#.#.###v###.###.#.#.###.###.###########.#.#.#.#.###.#.#.#######
#...#...#...#...#...###.#...#.......###...#...#...#####...#.#.#...#.#.#.#...#.#.#.###...#...#...#...#...#.#...........#.#.#.#...#...#.......#
#################.#####.###.#######.#######.###.#########.#.#.#.###.#.#.#.###.#.#.#####.###.#######.###.#.#.###########.#.#.###.###########.#
###...#.........#.....#.....#.......#...###.....#.........#...#.....#...#.....#...#####...#.....#...#...#.#...........#.#.#.....#...........#
###.#.#.#######.#####.#######.#######.#.#########.#######################################.#####.#.###.###.###########.#.#.#######.###########
###.#.#.#.....#.......#...#...#.......#.......###...................#...#.................#.....#...#.....#...........#...###...#...#.......#
###.#.#.#.###.#########.#.#.###.#############.#####################.#.#.#.#################.#######.#######.#################.#.###.#.#####.#
#...#.#...###...........#.#.....#.............#...###...............#.#.#.................#.........#...###...........#.....#.#...#...#...#.#
#.###.###################.#######.#############.#.###.###############.#.#################.###########.#.#############.#.###.#.###.#####.#.#.#
#...#...###...#...........#.....#.........#.....#.#...#.....#...###...#.#.................###...#...#.#.#.............#...#.#...#.......#...#
###.###.###.#.#.###########.###.#########.#.#####.#.###.###.#.#.###.###.#.###################.#.#.#.#.#.#.###############.#.###.#############
###...#.#...#...###...#...#...#.........#.#.....#.#.....###...#...#...#.#...#.......#.....###.#.#.#.#.#.#.....#...#...###.#...#.....#...#...#
#####.#.#.#########.#.#.#.###.#########.#.#####.#.###############.###.#.###.#.#####.#.###.###.#.#.#.#.#.#####v#.#.#.#.###.###.#####.#.#.#.#.#
#...#.#.#.....#...#.#.#.#...#.........#...#...#.#...#...#.......#.###.#.###...#.....#.#...#...#...#.#.#...#.>.>.#...#...#.#...#...#...#...#.#
#.#.#.#.#####.#.#.#.#.#.###.#########.#####.#.#.###.#.#.#.#####.#.###.#.#######.#####.#.###.#######.#.###.#.#v#########.#.#.###.#.#########.#
#.#...#...#...#.#...#.#...#...#.......#...#.#.#...#.#.#.#...###...#...#...#...#.....#.#...#.......#.#.###...#...#.......#.#.###.#.#...#...#.#
#.#######.#.###.#####.###.###.#.#######.#.#.#.###.#.#.#.###v#######.#####.#.#.#####.#.###.#######.#.#.#########.#.#######.#.###.#.#.#.#v#.#.#
#...#...#...###...#...###.#...#...#.....#.#.#...#.#...#...>.>...###...#...#.#...###...#...#...#...#...###.......#.......#.#.#...#.#.#.>.#...#
###.#.#v#########.#.#####.#.#####.#.#####.#.###.#.#########v###.#####.#.###.###.#######.###.#.#.#########.#############.#.#.#.###.#.###v#####
#...#.#.>.###...#.#.#...#.#.#...#.#.....#.#.#...#.......###...#...###.#.###.#...#...###.#...#.#.#####...#.........#.....#.#.#.###.#.#...#...#
#.###.#v#.###.#.#.#.#.#.#.#.#.#.#v#####.#.#.#.#########.#####.###.###.#.###.#.###.#.###v#.###.#.#####.#.#########.#.#####.#.#.###.#.#.###.#.#
#.....#.#.#...#.#.#.#.#.#.#...#.>.>.#...#.#.#.....#.....#...#...#.#...#.#...#.#...#.#.>.>.#...#.#.....#...........#.#...#.#.#.###...#.....#.#
#######.#.#.###.#.#.#.#.#.#######v#.#.###.#.#####.#.#####.#.###.#.#.###.#.###.#.###.#.#v###.###.#.#################.#.#.#.#.#.#############.#
#...#...#.#.#...#.#.#.#.#.....#...#...#...#.....#...#.....#.....#...###.#.#...#.###.#.#.###...#.#.....#...#.......#.#.#.#.#.#.###...#.......#
#.#.#.###.#.#.###.#.#.#.#####.#.#######.#######.#####.#################.#.#.###.###.#.#.#####.#.#####.#.#.#.#####.#.#.#.#.#.#.###.#.#.#######
#.#...###...#.....#...#.#...#.#.......#...#...#...###...............#...#.#.#...###...#...#...#...#...#.#.#.#.....#...#...#.#.###.#.#.......#
#.#####################.#.#.#.#######.###.#.#.###.#################.#.###.#.#.###########.#.#####.#.###.#.#.#.#############.#.###.#.#######.#
#.#...........###.....#...#...#.......###...#.....#.................#...#.#...#...#...#...#.......#.#...#...#.......###...#...#...#.#.......#
#.#.#########.###.###.#########.###################.###################.#.#####.#.#.#.#.###########.#.#############.###.#.#####.###.#.#######
#...#.........#...#...#...#...#.......###...#.....#.............#.....#.#.#.....#...#.#...........#.#.#.............#...#.###...###.#.......#
#####.#########.###.###.#.#.#.#######.###.#.#.###.#############.#.###.#.#.#.#########.###########.#.#.#.#############.###.###.#####.#######.#
#.....#######...#...###.#.#.#.#.......#...#...#...###...........#.#...#...#.......###.....#.....#.#...#...#.........#...#.....#####.....#...#
#.###########.###.#####.#.#.#.#.#######.#######.#####.###########.#.#############.#######.#.###.#.#######.#.#######.###.###############.#.###
#.#.......#...#...#...#.#.#.#.#.......#...#.....#...#.............#...#...###...#.......#.#...#...#...#...#.#.......###...........#...#.#...#
#.#.#####.#.###.###.#.#.#.#.#.#######v###.#.#####.#.#################.#.#.###.#.#######.#.###.#####.#.#.###.#.###################.#.#.#.###.#
#...#.....#...#...#.#.#.#.#.#.#...#.>.>.#.#...###.#.....###.....#.....#.#.#...#.........#.....###...#.#.....#.........#.........#...#.#.#...#
#####.#######.###.#.#.#.#.#.#.#.#.#.#v#.#.###.###.#####.###.###.#.#####.#.#.#####################.###.###############.#.#######.#####.#.#.###
#.....###.....###.#.#.#.#.#.#...#...#.#.#...#...#.#...#...#...#.#...###.#.#.......#...###...#...#...#.#...#...........#.......#.#...#.#.#...#
#.#######.#######.#.#.#.#.#.#########.#.###.###.#.#.#.###.###.#.###.###.#.#######.#.#.###.#.#.#.###.#.#.#.#.#################.#.#.#.#.#.###.#
#.......#...#####...#...#...#.....###.#.#...#...#.#.#.....#...#.....#...#...#.....#.#...#.#.#.#.#...#.#.#.#.........###...#...#...#...#.....#
#######.###.#################.###.###.#.#.###.###.#.#######.#########.#####.#.#####.###.#.#.#.#.#.###.#.#.#########.###.#.#.#################
#.......###...#...#...###...#...#.#...#...#...#...#...#...#...#.....#.#.....#.....#.#...#.#.#.#.#...#.#.#.#.........#...#...#...###...#...###
#.###########.#.#.#.#.###.#.###.#.#.#######.###.#####.#.#.###.#.###.#.#.#########v#.#.###.#.#.#.###.#.#.#.#.#########.#######.#.###.#.#.#.###
#.#...###...#...#...#.#...#.....#.#.......#.#...#.....#.#.#...#...#.#.#.#.......>.>.#.....#.#.#.#...#...#.#.......###.......#.#.....#...#...#
#.#.#v###.#.#########.#.#########.#######.#.#.###.#####.#.#.#####.#.#.#.#.#######v#########.#.#.#.#######.#######.#########.#.#############.#
#.#.#.>...#...#...#...#.#.......#.#.......#...#...#...#.#.#...#...#.#.#...#...###.#.........#.#.#...#.....#...#...#...#...#.#.#.......#...#.#
#.#.#v#######.#.#.#.###.#.#####.#.#.###########.###.#.#.#.###v#.###.#.#####.#.###.#.#########.#.###.#.#####.#.#v###.#.#.#.#v#.#.#####.#.#.#.#
#...#...#...#...#.#...#...#####.#.#.......#...#.....#...#...>.>.###...#...#.#...#.#...#.....#.#.....#.....#.#.>.>...#.#.#.>.#.#.#####...#.#.#
#######.#.#.#####.###.#########.#.#######.#.#.###############v#########.#.#.###.#.###.#.###.#.###########.#.###v#####.#.###v#.#.#########.#.#
#.....#...#.....#.....###...#...#.........#.#.###...#...#...#...#.......#.#...#.#.###...###.#.......#.....#...#.....#.#.#...#.#.........#...#
#.###.#########.#########.#.#.#############.#.###.#.#.#.#.#.###.#.#######.###.#.#.#########.#######.#.#######.#####.#.#.#.###.#########.#####
#...#...........#...###...#.#...#...#...###.#.###.#...#.#.#.#...#.......#.....#...#...#.....#.....#.#.#...#...###...#...#.#...#...#.....#...#
###.#############.#.###.###.###.#.#.#.#.###.#.###.#####.#.#.#.#########.###########.#.#.#####.###.#.#.#.#.#.#####.#######.#.###.#.#.#####.#.#
###.#.......#.....#...#...#...#...#...#.#...#...#.....#.#.#.#.#.........#.........#.#.#.#...#...#.#.#.#.#.#.#.....###...#.#.#...#...#...#.#.#
###.#.#####.#.#######.###.###.#########.#.#####.#####.#.#.#.#.#.#########.#######.#.#.#.#.#.###.#.#.#.#.#.#.#.#######.#.#.#.#.#######.#.#.#.#
#...#.#.....#.#...#...###...#...........#.....#...#...#...#...#.......#...#.......#.#...#.#.#...#...#...#...#.....###.#.#...#.....#...#...#.#
#.###.#.#####.#.#.#.#######.#################.###.#.#################.#.###.#######.#####.#.#.###################.###.#.#########v#.#######.#
#.#...#.......#.#...#...###.............#####...#.#.....#...#...#...#...###...#...#...#...#...#...#.......#.....#...#.#...#...#.>.#.#.......#
#.#.###########v#####.#.###############.#######.#.#####.#.#.#.#.#.#.#########.#.#.###.#.#######.#.#.#####.#.###.###.#.###.#.#.#.#v#.#.#######
#.#.#.....#...#.>.#...#.#.............#.#.......#.......#.#.#.#.#.#.#.....###...#...#...###...#.#.#.#.....#...#.....#...#.#.#.#.#...#...#...#
#.#.#.###.#.#.#v#.#.###.#.###########.#.#.###############.#.#.#.#.#.#.###.#########.#######.#.#.#.#.#.#######.#########.#.#.#.#.#######.#.#.#
#...#...#...#...#...#...#.#.....#...#...#.....#...#...#...#.#.#.#.#.#.#...#.........#.....#.#.#.#.#.#.#...#...#...#.....#.#.#.#.#.......#.#.#
#######.#############.###.#.###.#.#.#########.#.#.#.#.#.###.#.#.#.#.#.#.###v#########.###.#.#.#.#.#.#.#.#.#.###.#.#.#####.#.#.#.#.#######.#.#
###...#...........#...###...###...#...#...###...#...#.#...#...#...#.#.#.#.>.>.....###.#...#.#.#.#.#.#.#.#.#.....#.#.....#...#.#.#.#.......#.#
###.#.###########.#.#################.#.#.###########v###.#########.#.#.#.#v#####.###.#.###.#.#.#.#.#.#.#.#######v#####.#####.#.#.#.#######.#
#...#...#.......#.#...###...#.........#.#.###...###.>.>...###...###.#.#...#...###.....#...#.#.#.#.#.#.#.#.#...#.>.>...#.....#...#.#.#.......#
#.#####.#.#####.#.###.###.#.#.#########.#.###.#.###.#v#######.#.###.#.#######.###########.#.#.#.#.#.#.#.#.#.#.#.#v###.#####.#####.#.#.#######
#.....#.#...###.#.###.....#.#.....#...#.#...#.#.#...#.#.....#.#...#.#.#...###.......#.....#.#...#.#.#.#.#.#.#...#...#...#...#...#...#.......#
#####.#.###.###.#.#########.#####.#.#.#.###.#.#.#.###.#.###.#.###.#.#.#.#.#########.#.#####.#####.#.#.#.#.#.#######.###.#.###.#.###########.#
#.....#...#...#...#.........###...#.#.#...#...#.#.###.#...#...#...#.#.#.#...........#...#...#.....#.#...#...###...#...#...###.#.........#...#
#.#######.###.#####.###########v###.#.###.#####.#.###.###.#####.###.#.#.###############.#.###.#####.###########.#.###.#######.#########.#.###
#...#...#...#.....#.#...#.....>.>.#.#...#.....#.#...#.....#...#.###...#.#...........#...#...#.....#.#.....###...#...#.....#...#.......#...###
###.#.#.###.#####.#.#.#.#.#####v#.#.###.#####.#.###.#######.#.#.#######.#.#########.#.#####.#####.#.#.###.###.#####.#####.#.###.#####.#######
###.#.#...#.#.....#...#.#.#.....#.#...#.#.....#.....#.......#.#.....#...#.#.........#.....#.....#.#.#...#.....#...#.......#.....#...#.......#
###.#.###.#.#.#########.#.#.#####.###.#.#.###########.#######.#####.#.###.#.#############.#####.#.#.###.#######.#.###############.#.#######.#
###...#...#.#.#.......#...#.....#.....#.#.#...###...#...#...#...#...#.....#.............#...#...#...###.......#.#.....#...#...#...#.#...#...#
#######.###.#.#.#####.#########.#######.#.#.#.###.#.###.#.#.###.#.#####################.###.#.###############.#.#####.#.#.#.#.#.###.#.#.#.###
#.......###...#.....#.###.....#.......#...#.#.#...#...#...#...#.#.#.................#...###...#.............#...#.....#.#...#.#...#...#...###
#.#################.#.###.###.#######.#####.#.#.#####.#######.#.#.#.###############.#.#########.###########.#####.#####.#####.###.###########
#.#.......#.........#...#...#.........###...#.#...#...#.......#...#...............#.#.........#...........#.......###...#.....#...#.........#
#.#.#####.#.###########.###.#############.###.###.#.###.#########################.#.#########.###########.###########.###.#####.###.#######.#
#.#.###...#...........#...#.....###...###.#...#...#...#.........#.....#...#.......#...........#...#...###.....###...#.#...#...#.....#.......#
#.#.###.#############.###.#####.###.#.###.#.###.#####.#########.#.###.#.#.#.###################.#.#.#.#######.###.#.#.#.###.#.#######.#######
#...#...###...#...###.#...#...#.....#.....#.#...#.....#.........#...#.#.#.#.......#...#...###...#...#.#.....#...#.#.#.#.###.#.###...#.......#
#####.#####.#.#.#.###.#.###.#.#############v#.###.#####.###########.#.#.#.#######.#.#.#.#.###.#######.#.###.###.#.#.#.#.###.#.###.#.#######.#
#.....#...#.#.#.#.#...#...#.#...#...#...#.>.>.#...#...#.#...#...#...#...#.........#.#...#...#.#.....#...#...#...#.#.#.#...#.#.#...#.#.......#
#.#####.#.#.#.#.#.#.#####.#.###.#.#.#.#.#.#####.###.#.#.#.#.#.#.#.#################.#######.#.#.###.#####.###.###.#.#.###.#.#.#.###.#.#######
#.#...#.#.#.#.#.#...#.....#...#.#.#.#.#.#.#...#.....#.#...#...#.#...#.....#...#...#.#.......#...###.....#...#...#.#.#.#...#.#.#...#.#.....###
#.#.#.#.#.#.#.#.#####.#######.#.#.#.#.#.#.#.#.#######.#########.###.#.###.#.#.#.#.#.#.#################.###.###.#.#.#.#.###.#.###.#.#####v###
#.#.#...#...#.#.#...#.....#...#.#.#.#.#.#.#.#.........###.......###...###...#...#.#.#.....#...#.....#...#...#...#.#...#...#.#.#...#.#...>.###
#.#.#########.#.#.#.#####.#.###.#.#.#.#.#.#.#############.#######################.#.#####.#.#.#.###.#.###.###.###.#######.#.#.#.###.#.###v###
#.#.#.........#...#.#.....#.#...#.#.#.#.#.#.........#...#...#####...###...#.......#...#...#.#.#.#...#...#.###.###.....###.#.#.#.#...#.#...###
#.#.#.#############.#.#####.#.###.#.#.#.#.#########.#.#.###v#####.#.###.#.#.#########.#.###.#.#.#.#####.#.###v#######.###.#.#.#.#.###.#.#####
#...#...#...#...#...#.#...#.#...#.#.#.#...#.........#.#.#.>.>.###.#.###.#.#...#.....#.#...#.#...#.#...#.#.#.>.>.#...#...#.#.#...#...#.#.#...#
#######.#.#.#.#.#.###.#.#.#.###.#.#.#.#####.#########.#.#.###.###.#.###.#.###v#.###.#.###.#.#####.#.#.#.#.#.###.#.#.###.#.#.#######.#.#.#.#.#
#.......#.#.#.#.#...#.#.#.#.###.#.#.#.#.....#...#...#.#.#...#.#...#.#...#.#.>.>.###.#.#...#.#.....#.#.#.#.#...#.#.#.#...#.#.......#.#.#.#.#.#
#.#######.#.#.#.###.#.#.#.#.###.#.#.#.#.#####.#.#.#.#.#.###.#.#.###.#.###.#.#######.#.#.###.#.#####.#.#.#.###.#.#.#.#.###.#######.#.#.#.#.#.#
#.........#...#.....#...#...###...#...#.......#...#...#.....#...###...###...#######...#.....#.......#...#.....#...#...###.........#...#...#.#
###########################################################################################################################################.#

23
day23/sample1.in 100644
View File

@ -0,0 +1,23 @@
#.#####################
#.......#########...###
#######.#########.#.###
###.....#.>.>.###.#.###
###v#####.#v#.###.#.###
###.>...#.#.#.....#...#
###v###.#.#.#########.#
###...#.#.#.......#...#
#####.#.#.#######.#.###
#.....#.#.#.......#...#
#.#####.#.#.#########v#
#.#...#...#...###...>.#
#.#.#v#######v###.###v#
#...#.>.#...>.>.#.###.#
#####v#.#.###v#.#.###.#
#.....#...#...#.#.#...#
#.#########.###.#.#.###
#...###...#...#...#.###
###.###.#.###v#####v###
#...#...#.#.>.>.#.>.###
#.###.###.#.###.#.#v###
#.....###...###...#...#
#####################.#

109
day23/sol.py 100644
View File

@ -0,0 +1,109 @@
import sys
input = sys.stdin
map = []
for line in input:
map.append(list(line.strip()))
# find points where the path forks
def neighbors(x,y):
n = []
c = map[y][x]
for i,j in [(y-1,x),(y+1,x),(y,x-1),(y,x+1)]:
if c == '<' and not j < x: continue
if c == '>' and not j > x: continue
if c == '^' and not i < y: continue
if c == 'v' and not i > y: continue
if 0 <= i < len(map) and 0 <= j < len(map[i]):
if map[i][j] != '#':
n.append((j,i))
return n
spots = []
for i in range(len(map)):
for j in range(len(map[i])):
if map[i][j] == '.':
n = neighbors(j,i)
if len(n) not in (0,2):
spots.append((j,i))
# construct a graph of paths between fork points
def find_paths(start,spots):
q = [(0,start)]
dist = {}
r = []
while q:
q.sort()
n,(x,y) = q.pop(0)
if (x,y) in dist:
continue
dist[x,y] = n
#print(x,y,neighbors(x,y))
if (x,y) in spots and (x,y) != start:
r.append((n,(x,y)))
else:
for j,i in neighbors(x,y):
if (j,i) not in dist:
q.append((n+1,(j,i)))
return r
G = {}
for p in spots:
G[p] = find_paths(p,spots)
print(G)
# find start position
for i,c in enumerate(map[0]):
if c == '.':
start = (i,0)
break
# algorithm for finding the shortest path between points in a
# a weighted directed acyclic graph
#
# from wikipedia:
# https://en.wikipedia.org/w/index.php?title=Topological_sorting&oldid=1188428695#Application_to_shortest_path_finding
#
# this is a variant of the bellman-ford and shortest path faster algorithms
# except we can take some shortcuts because the graph is acyclic
#
# we implicitly invert the weights of the graph so that, in effect,
# it finds the longest path instead
def topo(G, start):
t = []
seen = set()
tmp = set()
def visit(n):
if n in seen:
return
if n in tmp:
raise Exception("cycle with %s %s" % (n,tmp))
tmp.add(n)
for _, p in G[n]:
visit(p)
tmp.remove(n)
seen.add(n)
t.append(n)
visit(start)
t.reverse()
return t
print("topo=",topo(G,start))
dist = {n: float('-inf') for n in G}
dist[start] = 0
T = topo(G, start)
for u in T:
for n,v in G[u]:
if dist[v] < dist[u] + n:
dist[v] = dist[u] + n
print(dist)
print(max(dist.values()))

84
day23/sol2.py 100644
View File

@ -0,0 +1,84 @@
import sys
input = sys.stdin
map = []
for line in input:
map.append(list(line.strip()))
# construct a graph
for i,c in enumerate(map[0]):
if c == '.':
start = (i,0)
break
def neighbors(x,y):
n = []
c = map[y][x]
for i,j in [(y-1,x),(y+1,x),(y,x-1),(y,x+1)]:
if 0 <= i < len(map) and 0 <= j < len(map[i]):
if map[i][j] != '#':
n.append((j,i))
return n
spots = []
for i in range(len(map)):
for j in range(len(map[i])):
if map[i][j] in '.<>v^':
n = neighbors(j,i)
if len(n) not in (0,2):
spots.append((j,i))
def reachable(start,spots):
q = [(0,start)]
dist = {}
r = []
while q:
q.sort()
n,(x,y) = q.pop(0)
if (x,y) in dist:
continue
#print(x,y,neighbors(x,y))
if (x,y) in spots and (x,y) != start:
r.append((n,(x,y)))
else:
dist[x,y] = n
for j,i in neighbors(x,y):
if (j,i) not in dist:
q.append((n+1,(j,i)))
return r
G = {}
for p in spots:
G[p] = reachable(p,spots)
from pprint import pprint
pprint(G)
for p in G:
if p[1] == len(map)-1:
goal = p
print("start=",start)
print("goal=",goal)
print("brute force...")
def longest(p, seen, depth):
best = -10000000
b = []
if p == goal:
return 0,[]
if p in seen:
raise Error("alredy seen")
seen.add(p)
for n,q in G[p]:
if q not in seen:
t,path = longest(q, seen, depth+1)
if t+n > best:
best = t+n
b = [q]+path
seen.remove(p)
return best, b
print(longest(start,set(),0))

300
day24/input 100644
View File

@ -0,0 +1,300 @@
197869613734967, 292946034245705, 309220804687650 @ 150, 5, -8
344503265587754, 394181872935272, 376338710786779 @ -69, 11, -46
293577250654200, 176398758803665, 272206447651388 @ -17, 101, 26
227838629808858, 367321356713508, 425398385268402 @ 65, 16, -116
297081053375721, 388848043410867, 296378505058047 @ -18, -39, 33
249754396059978, 259753263131310, 219900405025431 @ 91, -50, 168
286919351742593, 228761881251504, 290165658974486 @ 12, -276, -96
286311847445390, 152489168079800, 315302094634181 @ 29, -35, -385
288820028461659, 150144138610263, 536519846895513 @ -8, 281, -209
351363783085246, 431469241705574, 539320870624397 @ -78, -38, -232
520741901046000, 486075674341440, 369706315508673 @ -232, -39, -23
296908684971710, 177204123362510, 281142112744853 @ -31, 49, -22
464778874251030, 349646113957650, 294333434112573 @ -189, 77, 47
223080581415839, 347888203813250, 182953206412142 @ 62, 70, 164
248758455691314, 346762126256074, 336814976102123 @ 67, -112, -64
305759617719566, 278636683190318, 293811872846781 @ -37, 7, 13
261069455438014, 224906346121182, 312469904696747 @ 213, -558, -355
175747324785094, 297435412745142, 423525775964281 @ 111, 126, -90
288744178785162, 132122659522473, 258206348685239 @ 25, -21, 9
295813662568370, 254283641864230, 293357741330673 @ -22, -70, -20
154949963866962, 287525861043030, 236828184910989 @ 212, 30, 115
249405710994408, 288399639351476, 278889781077279 @ 40, 107, 58
261619805679875, 455908136153330, 353090693287483 @ 18, -10, -7
330456261567276, 292626086331062, 325102953175383 @ -98, -94, -74
299424195935038, 510703474048950, 292252992375285 @ -19, -90, 49
289238752547322, 290180922496209, 356685344768994 @ -7, 23, -82
280872765743550, 201108566398725, 258965390526423 @ 21, 41, 69
275979615869275, 148504223460275, 255005089561549 @ 134, -85, 59
387325456061188, 345202312209860, 329940617813946 @ -205, -160, -68
234283292154810, 377522770250222, 301277403416473 @ 64, -28, 26
392077346074701, 495085824379504, 530452270485388 @ -110, -56, -184
303921565996632, 184162452416208, 288112084900062 @ -77, -104, -106
279914507067786, 272090396870874, 137037141013257 @ 16, -57, 354
265091370503554, 259490553837145, 294987941742668 @ 41, 25, 6
277324636544734, 487331020226038, 542928614247113 @ 5, -98, -234
261897775442772, 280478694349944, 288319637735007 @ 23, 128, 49
282398761112085, 136973576351520, 265394454424968 @ 74, 24, -36
347154810802281, 412858527953970, 218797176940017 @ -72, -10, 127
342824292561169, 362027652434593, 377103448331376 @ -88, -59, -98
303342712558226, 143531465945890, 263215337554529 @ -87, 70, 15
275380876849407, 104449535339038, 273473922524719 @ 142, 322, -115
285636284070876, 35217949523578, 152994957457591 @ -5, 398, 189
294685250599224, 122742414916884, 261780925324521 @ -54, 57, -55
314169716569136, 156663661505480, 455739759596215 @ -64, 205, -390
300983303095740, 133957419148920, 261097837194423 @ -88, 79, 12
158215971846888, 339904610013018, 239642086049769 @ 146, 52, 105
274536031464654, 65723174697222, 354345054787149 @ 46, 456, -251
327841767697610, 339044435628830, 283110688052013 @ -66, -28, 43
330929514175462, 504607737339754, 162507010450204 @ -67, -242, 218
276948325793030, 149100573461750, 201857443244268 @ 77, 58, 388
260677243049794, 257886873526064, 401788966601771 @ 35, 101, -132
238764538535158, 239639934534678, 427263483474075 @ 45, 186, -95
294271217851860, 279973777207680, 317358232153488 @ -21, -348, -162
240944232332486, 140602546133218, 173583047033163 @ 233, 152, 466
299283097756530, 171124444230390, 232178177063823 @ -57, -78, 208
309506824723454, 193340344086617, 325677550626124 @ -43, 169, -42
313257508666173, 193901329414071, 312656919318366 @ -82, 33, -110
562238344970567, 521281584140464, 554003079517479 @ -274, -76, -203
298867675092622, 413106341656246, 319408082070669 @ -18, 29, 25
370336364999228, 303102716871354, 236944508864721 @ -108, 84, 109
280537764410923, 102390738316029, 173484039005415 @ 72, 339, 673
292740513454551, 187470172834775, 251899331250560 @ -14, 95, 90
311208406217498, 244876960258574, 384930971208158 @ -63, -46, -264
282757042955406, 134469559669654, 254384177119565 @ 58, 91, 69
287704452860064, 121558528808556, 254224870158150 @ 23, 176, 68
275586065064718, 113832932538934, 250412578315621 @ 231, 176, 108
280638278276355, 123874819227913, 281800323086541 @ 94, 137, -197
493700702104308, 528380743592954, 350707777563697 @ -284, -244, -43
254273430941810, 246127394458430, 305873005421113 @ 42, 128, 14
189195001805430, 327268786344945, 319530516675018 @ 93, 103, 22
325888830117990, 244873525901106, 384752213134803 @ -151, -248, -454
401086712195530, 418733514897265, 338101695130098 @ -303, -511, -140
230647849488649, 435350980629356, 268731710232233 @ 60, -56, 71
295369648182904, 93151204130694, 263077240181359 @ -45, 426, -15
418829662328400, 326986769471280, 418797738573353 @ -233, -60, -201
251815856690310, 194585137396830, 271694989005693 @ 254, -275, -43
320084250980490, 430363982553600, 272379689756425 @ -41, -15, 69
272457208243458, 108653772697770, 297045940728273 @ 103, 300, -179
255460977490872, 377835158364426, 281221792538313 @ 50, -132, 41
186732804203969, 179181818690891, 296305762653620 @ 132, 227, 30
363360218049360, 457270542750130, 287865571450673 @ -86, -40, 53
298636823643895, 142941004154310, 378997929287068 @ -28, 234, -220
382702722487998, 513245966052726, 543441464074293 @ -103, -84, -204
228608403903005, 303829275389505, 355570720480703 @ 126, -105, -135
260219312780930, 137397315917050, 266549922818113 @ 230, 71, -24
304643969715550, 224581934648158, 272949803301789 @ -51, -43, 25
268015204288250, 441520660598230, 343626399860368 @ 17, -70, -18
238174446949074, 332863636964670, 357987268083903 @ 87, -90, -104
228308485346835, 276785427440115, 170815415970033 @ 124, -39, 263
314650396447557, 123914003921973, 213241437994668 @ -80, 269, 206
297854289680094, 281401828797270, 371366863778853 @ -19, 100, -64
286065635483838, 197701133867714, 271157012746365 @ 13, -73, 7
250655739928110, 196904819501880, 302810006057913 @ 29, 239, 41
266668785798760, 448611859404060, 414549057547295 @ 14, -15, -72
322922202039681, 288346524052986, 321238043979297 @ -124, -338, -161
293057502181848, 156792975363520, 342205463462629 @ -17, 100, -301
308717176917930, 432612150091956, 137325020082819 @ -31, -56, 225
281337106418164, 131556887203378, 394376757970843 @ 6, 284, -144
330503962382908, 330818974149580, 537427943951777 @ -58, 55, -255
283277882485918, 140980016640358, 250970251275309 @ 77, -62, 97
263087598676614, 91780184771073, 292766760310263 @ 93, 373, -59
278997311530576, 271220323025157, 298776258201767 @ 11, 39, 9
290282187857252, 90415486230079, 469397950757119 @ -9, 349, -223
256747945850094, 323730245582688, 328245791702091 @ 45, -23, -32
401181849440357, 451034523457105, 234309567496232 @ -208, -295, 122
308723752435696, 115715106908199, 370471287969368 @ -49, 303, -174
350829907284000, 410154732609660, 529415646366563 @ -77, -12, -219
419009202317788, 512491677981588, 547814498224699 @ -210, -309, -372
311615054184480, 231690664406384, 357030646855481 @ -56, 41, -147
304792391244741, 337785561648414, 131083793921031 @ -50, -366, 449
206420199739566, 127278272054270, 146756457401286 @ 133, 290, 268
270910705904663, 228152407296912, 221757636662076 @ 12, 192, 124
300969936669270, 134288269050258, 499856005855185 @ -41, 231, -710
346853099200626, 400507954958115, 312124090163436 @ -103, -165, -10
295655922433878, 136151138335002, 251210251002099 @ -47, 36, 94
392730607704625, 423781125044385, 516945102596158 @ -118, -9, -190
69403706384776, 184624597681024, 184639589308473 @ 288, 221, 181
66252602357374, 281405359792914, 30983356670417 @ 221, 147, 318
287993442160185, 228594083593680, 261604045844313 @ 8, -316, 39
314015168824601, 218762406294299, 297215373888999 @ -102, -139, -95
326752500563416, 353810276040186, 488516122962329 @ -64, -48, -267
258926906195649, 297757458431104, 224543898991682 @ 46, -8, 138
335842594299150, 354523823783860, 369336538703853 @ -83, -79, -101
288526789357971, 121038469208217, 258752869918026 @ 40, 47, -26
108328693782431, 343573100192480, 173790439071324 @ 180, 80, 172
190081526928030, 380079554486550, 364097619506013 @ 165, -149, -104
250948371088939, 296401339516958, 136026529945132 @ 38, 98, 230
351639656315755, 418597373928239, 322874990426896 @ -106, -169, -22
256527707312850, 265251465370026, 92231595436701 @ 49, 54, 363
243482352003871, 262121028939334, 375839029327665 @ 96, -22, -185
298593292934625, 339458062356441, 214617146154333 @ -26, -181, 171
275150906378238, 311632676774493, 263415384348696 @ 21, -73, 68
320596173865085, 381290990425590, 219310185443898 @ -46, -6, 130
177898665297750, 226849892269620, 160180756743033 @ 253, 44, 303
279116256628275, 146651840207353, 282879798100090 @ 124, -133, -246
392635760919870, 333295381854834, 442746711116196 @ -171, -34, -212
292548629428628, 122772229153350, 255776774836727 @ -22, 90, 38
544799759489502, 337391467488474, 361407860304663 @ -299, 64, -34
292322626234629, 138777764194865, 265324258794703 @ -20, -155, -99
282965997263942, 200583086862102, 288222750366121 @ 17, 18, -27
184123754458512, 298801270442925, 312681619432842 @ 135, 65, 8
294040563055029, 390459835311429, 430929964653284 @ -14, -33, -136
277956000745130, 354560401734564, 304340564999986 @ 9, -36, 14
167135982400842, 222756086467026, 67877440945953 @ 128, 197, 296
242759244348998, 449964433573166, 378310609379989 @ 38, -17, -36
239198436794010, 251471676042330, 376042668463923 @ 99, 20, -170
208612278954525, 286324378939317, 337304149796031 @ 204, -143, -131
277799030839086, 399218651107929, 368577064839678 @ 5, -11, -44
322118735266236, 290641269989004, 383487802302959 @ -53, 72, -91
256593963025854, 334513771019582, 294656480526811 @ 37, 14, 32
329599248877570, 197922370698045, 404258355865923 @ -87, 141, -215
292246235199542, 336476575154758, 294198436012381 @ -12, -74, 17
223046830666095, 222297021590310, 331293449149998 @ 174, 10, -124
257128556061390, 316985972417478, 316019421290157 @ 34, 52, 7
296823104251365, 73266965970672, 208843642987443 @ -46, 536, 381
186762232661930, 414021046203320, 181890680360523 @ 111, -31, 172
281676511806954, 68066126010342, 261288970575417 @ 151, 906, -67
303757637395870, 217457431464550, 319780204418413 @ -42, 38, -84
293998547622634, 157696363761326, 266947971288341 @ -24, 28, 5
303236697587634, 75782198507708, 164239911128339 @ -53, 431, 408
451884353787348, 347734285669926, 260867084065575 @ -200, 42, 80
212107079473842, 222514154994394, 227588059381085 @ 175, 52, 147
297742727268150, 210177653776632, 266421092954811 @ -31, -21, 42
297742449578916, 460871887542983, 360850072296921 @ -17, -23, -17
246490205901585, 447966753158365, 404149065570053 @ 34, -13, -61
428537057381550, 259393531649550, 451019171242533 @ -199, 116, -183
230092531193395, 450084861735895, 444545815810863 @ 56, -47, -119
293207906123064, 160980917757360, 246454854637719 @ -24, -155, 134
330685508240028, 153706232993909, 389627424871162 @ -113, 199, -270
248047109715670, 337914006012422, 417308660816485 @ 59, -46, -175
216999103947558, 245599796562690, 223461139247949 @ 223, -115, 179
366297428313467, 413353588098657, 493826636982846 @ -84, 27, -146
231542611031360, 292569000471730, 274734344419188 @ 59, 110, 64
340347932799756, 315437837619245, 423718776869272 @ -65, 95, -100
293050173321162, 191021302380190, 246711130315731 @ -17, -50, 112
326025761363193, 172309744218396, 394171710776000 @ -44, 264, -46
271911632195370, 173127769509796, 287671653632109 @ 199, -409, -293
353537780577862, 239571404654630, 342304790868225 @ -193, -70, -176
324487646920386, 339596736303303, 353856883304097 @ -79, -159, -121
278896401492370, 63361613140749, 81017522141184 @ 9, 393, 358
225736349588673, 244172924845407, 261079971175602 @ 148, -9, 68
335019211033523, 549163897737994, 323974350512217 @ -90, -482, -41
273084828613785, 368247857134458, 327369121734939 @ 14, -26, -11
282919220491465, 238779234002470, 288053477363518 @ 18, -118, -30
354058605830958, 477930645861318, 260207539202753 @ -83, -102, 81
291051493163588, 302693482748314, 336250827851927 @ -10, 8, -46
302136599948718, 298978042966246, 296462432706439 @ -35, -118, -12
246182100012654, 246207921568038, 309324103978749 @ 137, -134, -97
317720999662541, 172802533356978, 227678735734293 @ -142, -18, 211
323699470218957, 169723592095398, 326967128690214 @ -42, 266, 18
279718134579816, 151098046069860, 260123112421701 @ 67, 20, 35
286560024839556, 183250245674160, 276490166392059 @ 6, 65, 8
284516964059384, 214466825018483, 236586492406895 @ 11, -11, 137
265538863149330, 285643372016864, 203200189566570 @ 49, -88, 202
315439634532616, 345983154688094, 337582523323315 @ -82, -394, -166
382298777074318, 477442346283588, 450602496466227 @ -123, -132, -155
297488738575314, 203351014020870, 348386146910451 @ -25, 90, -142
340290418301214, 449513248539522, 198870094032717 @ -107, -353, 195
306926403020702, 264085526810342, 92228312283885 @ -63, -217, 634
285753943112325, 127042159058415, 257239181283243 @ 70, 9, 13
285194367136032, 196772148977019, 211250828871261 @ 7, 71, 203
280186615268560, 149530576342430, 534995842588463 @ 8, 254, -378
304353922021305, 394393007848145, 384490789906568 @ -38, -289, -192
276932969721078, 169211821049682, 305109645772069 @ 115, -231, -367
289174220120902, 280784186657998, 207869733319273 @ -7, 46, 161
273892733265056, 471164879168023, 169977050453577 @ 23, -373, 247
420774824279725, 513808363492455, 512932397542668 @ -214, -315, -320
222723614649462, 95829242236326, 431190945690693 @ 60, 338, -94
290050919046082, 421708455777732, 355150978564205 @ -9, -63, -37
256163702364955, 338170915595795, 323136695446578 @ 50, -73, -32
283530824407774, 180918120140422, 282063596074173 @ 21, 26, -29
305330693609626, 194608897625492, 280156456003183 @ -85, -163, -64
350867211940286, 254762639798858, 208201467280177 @ -123, 44, 173
292658057001566, 186611191986534, 294531491764749 @ -15, -6, -83
205133188202526, 283882440241030, 254246696852025 @ 85, 130, 88
207527722033694, 313490709226982, 155380049798509 @ 80, 103, 195
286095459839592, 182518785595248, 276773663547705 @ 8, 61, 5
221173207039028, 270280455415551, 302323873644328 @ 107, 51, 6
202220873384748, 331733628056034, 356583372113517 @ 98, 52, -37
478872952765358, 427762105117869, 367856149594534 @ -283, -141, -78
264485082557022, 134558202466358, 221148400319641 @ 133, 166, 252
303574831239612, 413128570023282, 224985056190447 @ -24, -8, 120
303656492350806, 448215155580240, 404372721283287 @ -23, -15, -62
289183181233890, 225442276391010, 278066389466093 @ -5, 36, 27
179891064098649, 251976683289780, 209996347833339 @ 102, 180, 133
292285855083730, 117890243167414, 322881299000409 @ -16, 214, -488
263115786187030, 164000553942130, 168076841054333 @ 41, 220, 243
255478895619510, 456459170387580, 311860606041413 @ 49, -255, -9
366278946085848, 360761257352178, 193601284347042 @ -105, 6, 164
268588220434735, 210511715640375, 273516093923258 @ 114, -249, -28
296367330036812, 45519306432278, 352752790350447 @ -22, 466, -146
312839808368697, 484657929662796, 311370704669898 @ -34, -84, 26
404183206681040, 437508574234990, 324857613500219 @ -166, -130, -10
301278417732710, 383208525587490, 324567902247773 @ -21, 39, 15
221521858324342, 147871529369018, 271954329484075 @ 233, 176, 20
238331541797146, 307094887243647, 285748486243466 @ 81, -21, 32
387058453145490, 291264628310445, 413288400222753 @ -103, 149, -65
281806695050800, 102535645981412, 224229206595093 @ 54, 337, 271
382768489961101, 289799539317816, 120400361307382 @ -102, 144, 222
358629218649038, 370723156567230, 510119746864869 @ -105, -45, -272
241723396587097, 32171878227714, 97562574749518 @ 182, 606, 686
339133911341583, 247399977873168, 317804203144806 @ -112, 24, -50
294812929007430, 121827959649486, 263346613288437 @ -56, 69, -78
333406576073130, 195418728322074, 119839720417061 @ -56, 231, 233
332913436495620, 413104400758692, 214961117634465 @ -126, -533, 193
306657286122368, 62579450992366, 408516596607431 @ -49, 434, -307
258918519707530, 402800225244410, 479136088804613 @ 48, -209, -319
306574106004125, 173338717947470, 274039489310813 @ -64, 82, 11
251003584491650, 344822439082095, 106437967388908 @ 33, 69, 248
443995844449518, 291076032086886, 458793724900896 @ -219, 74, -192
252149015845950, 260857624307700, 393158580379653 @ 37, 139, -81
282206724330750, 134213668434630, 246532562655693 @ 74, 55, 136
201878646851890, 294522179137582, 287458889971301 @ 84, 128, 53
282686826780880, 79828713648940, 301068037995743 @ 102, 629, -532
308838180675575, 253775954545887, 269072245209172 @ -106, -501, -6
400535675511166, 345143982201823, 426505437599253 @ -235, -167, -269
241394577836181, 224211159579393, 261691886181732 @ 202, -181, 48
306077706063674, 284725755377606, 154803647614769 @ -25, 154, 185
365653589396198, 463153272873646, 312571854846221 @ -268, -919, -121
318686554371549, 151900504113227, 114002600725269 @ -44, 271, 259
191058082403166, 232741228913382, 102877348473645 @ 123, 158, 289
251854512913260, 416926846065180, 322517509362363 @ 35, -32, 9
73732096579230, 248889630850150, 160735050119213 @ 229, 170, 191
308010120634782, 162247896695142, 206898344596077 @ -123, -72, 397
367094950925958, 346430011480326, 208310940226305 @ -175, -198, 185
348415508704715, 438913362553805, 431065206792327 @ -66, 5, -83
243913767214746, 318499986277368, 301330634810601 @ 83, -93, -7
290577302424987, 274027862543418, 310173273280014 @ -8, -138, -70
206367355193595, 443890386024435, 368947946735853 @ 82, -41, -37
314653135165260, 180719675893200, 336836128302903 @ -101, 28, -243
288758467027095, 142092153424095, 259845125549678 @ 16, -27, 14
251264327216991, 458958803080533, 351002059495629 @ 28, -13, -5
418453792994686, 377148066828538, 435517022329401 @ -195, -67, -176
300663139345815, 210318755541796, 391838480041011 @ -26, 152, -143
407819019583242, 264973287489378, 316298185761345 @ -124, 173, 28
300135433810158, 223703968826661, 330554985106861 @ -59, -351, -357
256373700487946, 201202354218908, 190134413167444 @ 185, -212, 432
264501569368215, 231915482473611, 251960400470196 @ 54, 23, 90
336088957922234, 274440731677630, 233603922638286 @ -63, 130, 112
292857361973673, 283315137160764, 319233308865504 @ -14, -124, -80
321460440055107, 406017933749745, 531393869902203 @ -44, -5, -219
261930383598387, 187271446516332, 396711117278349 @ 48, 164, -197
309135970551222, 289175860707045, 375988873917336 @ -35, 75, -80
330678833381150, 483935876388870, 103308110887661 @ -51, -60, 243
261484400605515, 64021410780961, 2680557427552 @ 34, 389, 462
338283557846180, 156144241744018, 296461137199135 @ -121, 206, -15
309973256445432, 380764412783106, 385767755453441 @ -32, 12, -63
180763727091872, 337325506704120, 285455855714899 @ 110, 75, 54
295274855868646, 262670799891631, 301562541002100 @ -19, -23, -20
415066581259959, 422582696665690, 396764486826004 @ -158, -50, -82
380928659873894, 147316450744614, 271580130291589 @ -109, 282, 69
284992931832300, 147112605604440, 242678306042148 @ 47, -48, 168
207537338020749, 233431877160355, 221420927966864 @ 70, 205, 120
287890570470198, 167049146098542, 265267007686725 @ 8, 11, 22
205106249502228, 144601615135332, 281915604194933 @ 103, 276, 51
219264098307253, 204774009695507, 228526044811677 @ 140, 119, 139
271096476245745, 250811406877864, 297718115811410 @ 34, 8, -10
327093059895630, 334242590617238, 236585941259549 @ -56, 37, 110
302677117068011, 427043527270244, 342141092441752 @ -24, -52, -16
292578448121010, 177016380525750, 95618865530373 @ -16, -74, 949

5
day24/sample1.in 100644
View File

@ -0,0 +1,5 @@
19, 13, 30 @ -2, 1, -2
18, 19, 22 @ -1, -1, -2
20, 25, 34 @ -2, -2, -4
12, 31, 28 @ -1, -2, -1
20, 19, 15 @ 1, -5, -3

109
day24/sol.py 100644
View File

@ -0,0 +1,109 @@
#!/usr/bin/env python
def read_input(input):
data = []
for line in input:
x, y, z, dx, dy, dz = [int(v) for v in line.replace("@", "").replace(",", " ").split()]
data.append((x,y,z,dx,dy,dz))
return data
def intersect_2d(l1, l2):
x0, y0, _, dx0, dy0, _ = l1
x1, y1, _, dx1, dy1, _ = l2
# convert to ax+by = c form
#(x - x0)dx0 + x0 = (y-y0)dy0 + y0
#x*dx0 - dx0*x0 + x0 = y*dy0 - dy0*y0 + y0
#x*dx0 - y*dy0 = dx0*x0 - dy0*y0 + y0 - x0
# x0 + t dx0 = x(t)
# y0 + t dy0 = y(t)
# t = (y - y0)/dy0
# t = (x - x0)/dx0
# (y-y0)/dy0 = (x-x0{
# x0 + (y-y0)/dy0 * dx0 = x
# x0 + y*dx0/dy0 - y0*dx0/dy0 = x
# x0 - y0*dx0/dy0 = x - y*dx0/dy0
a0 = dx0
b0 = -dy0
c0 = dx0*x0 - dy0*y0
a0 = dy0
b0 = -dx0
c0 = x0*(y0+dy0) - y0*(x0+dx0)
assert a0*x0 + b0*y0 == c0
a1 = dx1
b1 = -dy1
c1 = dx1*x1 - dy1*y1
a1 = dy1
b1 = -dx1
c1 = x1*(y1+dy1) - y1*(x1+dx1)
assert a1*x1 + b1*y1 == c1
# cramer's rule
d = a0*b1 - a1*b0
if d == 0 :
# non-intersecting
return None
return (c0*b1 - c1*b0)/d, (a0*c1 - a1*c0)/d
def intersect_2d(l1, l2):
x0, y0, _, dx0, dy0, _ = l1
x1, y1, _, dx1, dy1, _ = l2
# convert to ax+by = c form
# (x-x0)/dx0 = (y-y0)/dy0
# dy0(x-x0) = (dx0)(y-y0)
# x*dy0 - dy0*x0 = y*dx0 - dx0*y0
# x*dy0 - y*dx0 = dy0*x0 - dx0*y0
a0 = dy0
b0 = -dx0
c0 = x0*dy0 - y0*dx0
assert a0*x0 + b0*y0 == c0
a1 = dy1
b1 = -dx1
c1 = x1*dy1 - y1*dx1
assert a1*x1 + b1*y1 == c1
# cramer's rule
d = a0*b1 - a1*b0
if d == 0 :
# non-intersecting
return None
return (c0*b1 - c1*b0)/d, (a0*c1 - a1*c0)/d
def future(l1, point):
x,y,z, dx,dy,dz = l1
x0, y0 = point
return (dx < 0 and x0 <= x) or (dx > 0 and x0 >= x)
def inbounds(point):
x,y = point
if 7 <= x <= 21 and 7 <= y <= 21: return True
return 200000000000000 <= x <= 400000000000000 and 200000000000000 <= y <= 400000000000000
def solve(input):
data = read_input(input)
t = 0
for i,h1 in enumerate(data):
for h2 in data[i+1:]:
p = intersect_2d(h1, h2)
print(h1,h2, p)
if p is not None:
if future(h1, p) and future(h2, p):
if inbounds(p):
print(h1, h2, p)
t += 1
print(t)
import sys
solve(sys.stdin)

1243
day25/input 100644

File diff suppressed because it is too large Load Diff

13
day25/sample2.in 100644
View File

@ -0,0 +1,13 @@
jqt: rhn xhk nvd
rsh: frs pzl lsr
xhk: hfx
cmg: qnr nvd lhk bvb
rhn: xhk bvb hfx
bvb: xhk hfx
pzl: lsr hfx nvd
qnr: nvd
ntq: jqt hfx bvb xhk
nvd: lhk
lsr: lhk
rzs: qnr cmg lsr rsh
frs: qnr lhk lsr

124
day25/sol.py 100644
View File

@ -0,0 +1,124 @@
import sys
import random
from pprint import pprint
def main():
G = {}
for line in sys.stdin:
n, to = line.split(": ")
to = to.split()
G[n] = to
pprint(G)
# mke connections symmetric
for n in list(G):
for t in G[n]:
G.setdefault(t,[])
if n not in G[t]:
G[t].append(n)
G = {n:tuple(t) for n,t in G.items()}
pprint(G)
print([len(g) for g in connected_components(G, set())])
def key(e):
v,t = e
return sort2(len(G[v]),len(G[t]))
E = sorted(set(sort2(v,t) for v in G for t in G[v]), key=key)
print(connected_components(G, {sort2('hfx','pzl'), sort2('bvb','cmg'), sort2('jqt','nvd')}))
num = 100
while num > 3:
num, fwd = karger_stein(G, E)
P = {}
for v,t in fwd.items():
P.setdefault(v,[])
P.setdefault(t,[])
P[v].append(t)
P[t].append(v)
cc = connected_components(P, set())
print([len(x) for x in cc])
print(len(cc[0]) * len(cc[1]))
def karger_stein(G,E):
num = len(G)
fwd = {}
while num > 2:
# choose a random e
v,t = random.choice(E)
# contract the edge
fwd[v] = t
F = []
for v,t in E:
v = fwd.get(v,v)
t = fwd.get(t,t)
if v != t:
F.append((v,t))
E = F
num -= 1
print(len(E), E)
return len(E), fwd
def brute_force(x):
print([len(g) for g in connected_components(G, set())])
def key(e):
v,t = e
return sort2(len(G[v]),len(G[t]))
E = sorted(set(sort2(v,t) for v in G for t in G[v]), key=key)
E = [(v,t) for v,t in E if len(G[t]) <= 3 and len(G[v]) <= 3]
print(connected_components(G, {sort2('hfx','pzl'), sort2('bvb','cmg'), sort2('jqt','nvd')}))
#assert set(E) > {sort2('hfx','pzl'), sort2('bvb','cmg'), sort2('jqt','nvd')}
for i,e1 in enumerate(E):
print("#", i, e1)
for j,e2 in enumerate(E[i+1:], start=i+1):
for e3 in E[j+1:]:
cc = connected_components(G, {e1,e2,e3})
if len(cc) > 1 and not any(len(g) == 1 for g in cc):
#print(cc)
print(e1,e2,e3)
print([len(x) for x in cc])
print(len(cc[0]) * len(cc[1]))
def connected_components(G, nope):
V = [v for v in G]
mark = set()
cc = []
#print(nope)
while V:
node = V.pop()
if node in mark:
continue
group = []
def visit(node):
queue = [node]
while queue:
n = queue.pop()
if n in mark:
continue
mark.add(n)
group.append(n)
if n in G:
for t in G[n]:
if t not in mark and sort2(n,t) not in nope:
queue.append(t)
visit(node)
assert len(group) > 0
cc.append(group)
return cc
def sort2(a,b):
return min(a,b), max(a,b)
main()